Converting OpenCV images to PIL

707 views
Skip to first unread message

Mikey

unread,
Aug 26, 2009, 3:10:49 PM8/26/09
to ctypes-opencv
Hey guys I was working on my python app and was having difficulties
converting the IPLimage to a PIL compatible format. I tried using the
adaptors.py module to convert it, but I get the following error:


Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Documents and Settings\jack\Desktop\RT3000\rtrack2.py",
line 69, in analyze_video
newframe = Ipl2PIL(threshframe)
File "C:\Documents and Settings\jack\Desktop\RT3000\rtrack2.py",
line 50, in Ipl2PIL
orientation # orientation
File "C:\Python26\lib\site-packages\PIL\Image.py", line 1744, in
fromstring
im.fromstring(data, decoder_name, args)
File "C:\Python26\lib\site-packages\PIL\Image.py", line 575, in
fromstring
raise ValueError("not enough image data")
ValueError: not enough image data

Has anyone had any success with this? I know adaptors.py isnt made
for ctypes-opencv but I have attempted to make it compatible. Are
there any other ways to convert from IPL to PIL?
Thanks!

Mikey

unread,
Aug 26, 2009, 4:34:33 PM8/26/09
to ctypes-opencv
On a side note:
I also get the same error when using the built in ipl_to_pil
function...

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Documents and Settings\jack\Desktop\RT3000\rtrack2.py",
line 25, in analyze_video
newframe = ipl_to_pil(threshframe)
File "C:\Python26\lib\site-packages\ctypes_opencv\interfaces.py",
line 70, in ipl_to_pil
'raw', "BGR", im_ipl.widthStep
File "C:\Python26\lib\site-packages\PIL\Image.py", line 1744, in
fromstring
im.fromstring(data, decoder_name, args)
File "C:\Python26\lib\site-packages\PIL\Image.py", line 575, in
fromstring
raise ValueError("not enough image data")
ValueError: not enough image data

And Here is my Code up to that point:

def analyze_video(stuffs):
#cvNamedWindow( filename, CV_WINDOW_AUTOSIZE)
capture = cvCaptureFromAVI (filename)
num_frames = cvGetCaptureProperty(capture,
CV_CAP_PROP_FRAME_COUNT)
print "NUMBER OF FRAMES =", num_frames
frame = cvQueryFrame(capture)

print frame
bwframe = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U ,1)
threshframe = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U ,1)

cvConvertImage(frame, bwframe, 1)
cvThreshold(bwframe, threshframe, 70, 256, CV_THRESH_BINARY)

print "Enter NEW FRAME"
#cvShowImage( filename, threshframe )
newframe = ipl_to_pil(threshframe)

Minh-Tri Pham

unread,
Aug 26, 2009, 5:44:05 PM8/26/09
to ctypes...@googlegroups.com
Hi Mikey,

I've been working on converting between IplImage and PIL.Image for a
couple of days. Nice timing!!

The problem with function ipl_to_pil() is that it can only convert a BGR
IplImage into a RGB PIL.Image. It doesn't work with a grayscale image.
Can you check out the latest revision in the main branch and try
replacing 'ipl_to_pil(threshframe)' with 'threshframe.as_pil_image()'
instead? I'd like to know if the new function works on your end.

Minh-Tri
--
Dr. Minh-Tri Pham
Centre for Multimedia and Network Technology
School of Computer Engineering
Nanyang Technological University
Block N4, #b2c-06
Nanyang Avenue
Singapore
Tel: +65 9171 4243

Michael Kaufman (TA Bio107)

unread,
Aug 27, 2009, 12:24:20 AM8/27/09
to ctypes...@googlegroups.com
That did the trick Minh-Tri! Works like a charm now.  You're the man!

Minh-Tri Pham

unread,
Aug 27, 2009, 12:59:02 AM8/27/09
to ctypes...@googlegroups.com
It was my pleasure.

Minh-Tri

Reply all
Reply to author
Forward
0 new messages