Aswathy
Aswathy

Reputation: 37

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

Upvotes: 3

Views: 34680

Answers (2)

Travis_Dudeson
Travis_Dudeson

Reputation: 101

As well as PIL, image io, OpenCV and skimage are also powerful tools, with different applications each. Especially if you want to work with the image and use machine learning, imageio is efficient. For image io:

import imageio.v3 as iio
im = iio.imread('withmed.png')

And for skimage:

from skimage import io
im = io.imread('withmed.png')

and for OpenCV:

import cv2 as cv
cv.imread('withmed.png')

One important thing to know is that the types of output are different, while the output of opencv, skimage and imageio are numpy arrays, the output of PIL is an image class file: "PIL.JpegImagePlugin.JpegImageFile".

Upvotes: 0

Hugo
Hugo

Reputation: 29314

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.

Upvotes: 3

Related Questions