Yin Yang
Yin Yang

Reputation: 1806

PIL unable to open image via absolute path of image - IOError [Errno 2] No such file or directory

I'm trying to open an image created via sorl thubmnail using PIL.

Here is a part of the code

print user.image
print user.image.url
print user.get_images(size='500')
print user.get_images(size='500').url

This outputs

images/users/DSC_0889.JPG
/media/images/users/DSC_0889.JPG
<sorl.thumbnail.images.ImageFile object at 0x11288df10>
/media/cache/f9/ed/f9ed5e89154c42a2aff758b193618b12.jpg

Using PIL to open it results in the following results

Attempt 1:

im = Image.open(user.image)

This works fine.

Attempt 2:

im = Image.open(user.get_images(size='500'))

This gives the error

TypeError at /scripts/user/crop-image/
read() takes exactly 1 argument (2 given)

Traceback:
File "/Users/jaskaran/Desktop/coding/buyingiq/buyingiq/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/jaskaran/Desktop/coding/buyingiq/buyingiq/env/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/Users/jaskaran/Desktop/coding/buyingiq/buyingiq/views/www/user.py" in crop_image
  114.         im = Image.open(user.get_images(size='500'))
File "/Users/jaskaran/Desktop/coding/buyingiq/buyingiq/env/lib/python2.7/site-packages/PIL/Image.py" in open
  2097.     prefix = fp.read(16)

Attempt 3:

 im = Image.open(user.get_images(size='500').url)

This gives the error

IOError at /scripts/user/crop-image/
[Errno 2] No such file or directory: '/media/cache/f9/ed/f9ed5e89154c42a2aff758b193618b12.jpg'

If I point my browser to 127.0.0.1:8000/media/cache/f9/ed/f9ed5e89154c42a2aff758b193618b12.jpg the image is opened, so the image exists.

How can I use PIL to open this image?

Upvotes: 0

Views: 1189

Answers (1)

Yin Yang
Yin Yang

Reputation: 1806

The media folder was outside my main project directory.

Accessing it like settings.MEDIA_ROOT + str(user.get_images(size='500').name) solved the issue.

Upvotes: 2

Related Questions