Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “Django创建图像缩略图”;无法识别图像文件";_Python_Django_Python 2.7_Django Models - Fatal编程技术网

Python “Django创建图像缩略图”;无法识别图像文件";

Python “Django创建图像缩略图”;无法识别图像文件";,python,django,python-2.7,django-models,Python,Django,Python 2.7,Django Models,每次用户更新他们的个人资料图片时,我都会尝试以多种不同的大小调整和保存图像的缩略图 full_profile_picture = self.profile_picture logger.debug("Profile Picture 1: " + str(self.profile_picture)) thumbnail_size = (288,288) image_288 = Image.open(full_profile_picture) if image_288.mode not in ('

每次用户更新他们的个人资料图片时,我都会尝试以多种不同的大小调整和保存图像的缩略图

full_profile_picture = self.profile_picture

logger.debug("Profile Picture 1: " + str(self.profile_picture))
thumbnail_size = (288,288)
image_288 = Image.open(full_profile_picture)
if image_288.mode not in ('L', 'RGB'): image_288 = image_288.convert('RGB')
image_288.thumbnail(thumbnail_size, Image.ANTIALIAS)
temp_handle = StringIO()
image_288.save(temp_handle, 'png')
temp_handle.seek(0)
file_name = str(uuid4()) + ".png"
suf = SimpleUploadedFile(file_name, temp_handle.read(), content_type='image/png')
self.profile_picture_288.save(file_name, suf, save=False)



logger.debug("Profile Picture 2: " + str(self.profile_picture))  
thumbnail_size = (216,216)
image_216 = Image.open(full_profile_picture)
if image_216.mode not in ('L', 'RGB'): image_216 = image_216.convert('RGB')
image_216.thumbnail(thumbnail_size, Image.ANTIALIAS)
temp_handle1 = StringIO()
image_216.save(temp_handle1, 'png')
temp_handle1.seek(0)
file_name = str(uuid4()) + ".png"
suf = SimpleUploadedFile(file_name, temp_handle1.read(), content_type='image/png')
self.profile_picture_216.save(file_name, suf, save=False)
出于某种原因,第一个缩略图将保存查找,但第二个缩略图会出现以下错误:

Environment:


Request Method: POST

Django Version: 1.6
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'paintstore',
 'main',

Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/virtualenvs/x/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/mark/projects/django2/userprofile/views.py" in edit_profile
  25.             update_profile_form.save()
File "/usr/local/virtualenvs/x/local/lib/python2.7/site-packages/django/forms/models.py" in save
  437.                              construct=False)
File "/usr/local/virtualenvs/x/local/lib/python2.7/site-packages/django/forms/models.py" in save_instance
  94.         instance.save()
File "/home/mark/projects/django2/core/models.py" in save
  752.                     image_216 = Image.open(full_profile_picture)
File "/usr/local/virtualenvs/x/local/lib/python2.7/site-packages/PIL/Image.py" in open
  2006.     raise IOError("cannot identify image file")

Exception Type: IOError at /profile/update/
Exception Value: cannot identify image file

首先,考虑在打开和使用文件对象时使用除块之外的尝试。至于您的问题,我怀疑您在创建第一个缩略图时错误地保存(可能覆盖)了原始图像。最有可能的情况是,在第二次打开文件之前,原始图像文件头已损坏。可能重复: