vendredi 14 novembre 2014

python - Django création de miniatures d'images "ne peut pas identifier le fichier image" - Stack Overflow


I am trying to resize and save thumbnails of an image in multiple different sizes every time a user updates their profile picture.


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)

For some reason, the 1st thumbnail will save find, however the second gives me this error:


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


I am trying to resize and save thumbnails of an image in multiple different sizes every time a user updates their profile picture.


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)

For some reason, the 1st thumbnail will save find, however the second gives me this error:


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

0 commentaires:

Enregistrer un commentaire