Python PIL无法通过映像的绝对路径打开映像-IOError[Errno 2]没有这样的文件或目录
我正在尝试打开一个使用PIL创建的图像 这是代码的一部分Python PIL无法通过映像的绝对路径打开映像-IOError[Errno 2]没有这样的文件或目录,python,python-imaging-library,sorl-thumbnail,Python,Python Imaging Library,Sorl Thumbnail,我正在尝试打开一个使用PIL创建的图像 这是代码的一部分 print user.image print user.image.url print user.get_images(size='500') print user.get_images(size='500').url 这个输出 images/users/DSC_0889.JPG /media/images/users/DSC_0889.JPG <sorl.thumbnail.images.ImageFile object at
print user.image
print user.image.url
print user.get_images(size='500')
print user.get_images(size='500').url
这个输出
images/users/DSC_0889.JPG
/media/images/users/DSC_0889.JPG
<sorl.thumbnail.images.ImageFile object at 0x11288df10>
/media/cache/f9/ed/f9ed5e89154c42a2aff758b193618b12.jpg
这个很好用
尝试2:
im = Image.open(user.get_images(size='500'))
这就产生了错误
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)
IOError at /scripts/user/crop-image/
[Errno 2] No such file or directory: '/media/cache/f9/ed/f9ed5e89154c42a2aff758b193618b12.jpg'
尝试3:
im = Image.open(user.image)
im = Image.open(user.get_images(size='500').url)
这就产生了错误
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)
IOError at /scripts/user/crop-image/
[Errno 2] No such file or directory: '/media/cache/f9/ed/f9ed5e89154c42a2aff758b193618b12.jpg'
如果我将浏览器指向127.0.0.1:8000/media/cache/f9/ed/f9ed5e89154c42a2aff758b193618b12.jpg
图像将打开,因此图像存在
如何使用PIL打开此图像?媒体文件夹位于我的主项目目录之外
像
settings.MEDIA\u ROOT+str(user.get\u images(size='500').name)一样访问它
解决了这个问题。您需要使用.name
user.image.name
@BurhanKhalid Usinguser.get_images(size='500')。name
在/scripts/user/crop image/[Errno 2]没有这样的文件或目录:u'cache/fb/37/fb37faba63b618df7ee0394d0422d93.jpg'您应该使用文件的路径;不是url。可能是通过os.path.join(os.path.abspath(uuu file_uuu),user.get_images(size='500').url)
@klasske我试过了,它在/scripts/user/crop image/[Errno 2]处给出了相同的错误IOError没有这样的文件或目录:'/media/cache/fb/37/fb37faba63b618df7ee0394d0422d93.jpg'