Python Pillow返回错误:“0”;IOError:图像文件被截断(未处理6个字节)";
我将枕头4.1.1与Python 2.7/Django 1.9结合使用 我的网站上有成千上万的用户上传的图片,我用枕头从模板生成缩略图 例如: 在stackoverflow上找到的解决方案不起作用,因为它们都适用于视图而不是模板(例如ImageFile.LOAD\u TRUNCATED\u IMAGES=True) 有没有一种简单的方法来确定哪些图片会产生此错误?如何解决这个错误 以下是完整的回溯:Python Pillow返回错误:“0”;IOError:图像文件被截断(未处理6个字节)";,python,django,python-imaging-library,thumbnails,pillow,Python,Django,Python Imaging Library,Thumbnails,Pillow,我将枕头4.1.1与Python 2.7/Django 1.9结合使用 我的网站上有成千上万的用户上传的图片,我用枕头从模板生成缩略图 例如: 在stackoverflow上找到的解决方案不起作用,因为它们都适用于视图而不是模板(例如ImageFile.LOAD\u TRUNCATED\u IMAGES=True) 有没有一种简单的方法来确定哪些图片会产生此错误?如何解决这个错误 以下是完整的回溯: Internal Server Error: /trip/province-dublin-203
Internal Server Error: /trip/province-dublin-2034
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Terradiem\terradiem\trip\views.py", line 300, in trip
return render(request, 'trip/results.html', qsall)
File "C:\Python27\lib\site-packages\django\shortcuts.py", line 67, in render template_name, context, request=request, using=using)
File "C:\Python27\lib\site-packages\django\template\loader.py", line 97, in render_to_string
return template.render(context, request)
File "C:\Python27\lib\site-packages\django\template\backends\django.py", line 95, in render
return self.template.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 206, in render
return self._render(context)
File "C:\Python27\lib\site-packages\django\test\utils.py", line 92, in instrumented_test_render
return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 992, in render
bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 220, in render
nodelist.append(node.render_annotated(context))
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 220, in render
nodelist.append(node.render_annotated(context))
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 326, in render
return nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 992, in render
bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\sorl\thumbnail\templatetags\thumbnail.py", line 59, in render
return self._render(context)
File "C:\Python27\lib\site-packages\sorl\thumbnail\templatetags\thumbnail.py", line 137, in _render
thumbnail = get_thumbnail(file_, geometry, **options)
File "C:\Python27\lib\site-packages\sorl\thumbnail\shortcuts.py", line 8, in get_thumbnail
return default.backend.get_thumbnail(file_, geometry_string, **options)
File "C:\Python27\lib\site-packages\sorl\thumbnail\base.py", line 125, in get_thumbnail
thumbnail)
File "C:\Python27\lib\site-packages\sorl\thumbnail\base.py", line 157, in _create_thumbnail
image = default.engine.create(source_image, geometry, options)
File "C:\Python27\lib\site-packages\sorl\thumbnail\engines\base.py", line 21, in create
image = self.colorspace(image, geometry, options)
File "C:\Python27\lib\site-packages\sorl\thumbnail\engines\base.py", line 53, in colorspace
return self._colorspace(image, colorspace)
File "C:\Python27\lib\site-packages\sorl\thumbnail\engines\pil_engine.py", line 101, in _colorspace
return image.convert('RGB')
File "C:\Python27\lib\site-packages\PIL\Image.py", line 844, in convert
self.load()
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 226, in load
"(%d bytes not processed)" % len(b))
IOError: image file is truncated (6 bytes not processed)
谢谢好的,我自己的问题得到了答案 正如lepsch所评论的,只需添加
从PIL导入图像,图像文件
ImageFile.LOAD_TRUNCATED_IMAGES=True
尽可能早地在django应用程序中。我将其添加到生成页面的视图中,效果很好。最有可能的是,相关图像的标题包含错误的内容长度信息(可能已损坏)。您可以通过显式捕获和处理该错误来解决此问题,可能同时测试标题中的内容长度信息。只需执行
ImageFile.LOAD\u TRUNCATED\u IMAGES=True
,因为您已经在django应用程序中尽早发现了这一点。
IOError: image file is truncated (6 bytes not processed)
Internal Server Error: /trip/province-dublin-2034
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Python27\lib\site-packages\django\core\handlers\base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Terradiem\terradiem\trip\views.py", line 300, in trip
return render(request, 'trip/results.html', qsall)
File "C:\Python27\lib\site-packages\django\shortcuts.py", line 67, in render template_name, context, request=request, using=using)
File "C:\Python27\lib\site-packages\django\template\loader.py", line 97, in render_to_string
return template.render(context, request)
File "C:\Python27\lib\site-packages\django\template\backends\django.py", line 95, in render
return self.template.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 206, in render
return self._render(context)
File "C:\Python27\lib\site-packages\django\test\utils.py", line 92, in instrumented_test_render
return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 992, in render
bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 220, in render
nodelist.append(node.render_annotated(context))
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 220, in render
nodelist.append(node.render_annotated(context))
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\django\template\defaulttags.py", line 326, in render
return nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 992, in render
bit = node.render_annotated(context)
File "C:\Python27\lib\site-packages\django\template\base.py", line 959, in render_annotated
return self.render(context)
File "C:\Python27\lib\site-packages\sorl\thumbnail\templatetags\thumbnail.py", line 59, in render
return self._render(context)
File "C:\Python27\lib\site-packages\sorl\thumbnail\templatetags\thumbnail.py", line 137, in _render
thumbnail = get_thumbnail(file_, geometry, **options)
File "C:\Python27\lib\site-packages\sorl\thumbnail\shortcuts.py", line 8, in get_thumbnail
return default.backend.get_thumbnail(file_, geometry_string, **options)
File "C:\Python27\lib\site-packages\sorl\thumbnail\base.py", line 125, in get_thumbnail
thumbnail)
File "C:\Python27\lib\site-packages\sorl\thumbnail\base.py", line 157, in _create_thumbnail
image = default.engine.create(source_image, geometry, options)
File "C:\Python27\lib\site-packages\sorl\thumbnail\engines\base.py", line 21, in create
image = self.colorspace(image, geometry, options)
File "C:\Python27\lib\site-packages\sorl\thumbnail\engines\base.py", line 53, in colorspace
return self._colorspace(image, colorspace)
File "C:\Python27\lib\site-packages\sorl\thumbnail\engines\pil_engine.py", line 101, in _colorspace
return image.convert('RGB')
File "C:\Python27\lib\site-packages\PIL\Image.py", line 844, in convert
self.load()
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 226, in load
"(%d bytes not processed)" % len(b))
IOError: image file is truncated (6 bytes not processed)