Search code examples
pythonimage-processingpython-imaging-library

opening a jpg file in python


from PIL import Image
import cv2 #EDIT, this line added

im = Image.open('withmed.jpg',0)
im.show('image',img)
k = cv2.waitkey(0)
if k == 27:
    cv2.destroyAll windows

I am trying to open a jpg image which is saved in desktop. but on running this piece of code an error is popping out

Traceback (most recent call last):
  File "/home/anusha/aswathy.py", line 5, in <module>
    cv2.imshow('image',img)
    error: /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp:269: 
    error: (-215) size.width>0 && size.height>0 in function imshow

Solution

  • Instead of:

    im = Image.open('withmed.jpg',0)
    

    try:

    im = Image.open('withmed.jpg')
    

    If that doesn't work, put print(im) afterwards and tell us what it says.