Django / PIL - 上传图像时保存缩略图版本
Posted
技术标签:
【中文标题】Django / PIL - 上传图像时保存缩略图版本【英文标题】:Django / PIL - save thumbnail version right when image is uploaded 【发布时间】:2014-07-18 07:21:22 【问题描述】:这是我的forms.py:
class UploadImageForm(forms.ModelForm):
class Meta:
model = UserImages
fields = ['photo']
这是我的models.py:
class UserImages(models.Model):
user = models.ForeignKey(User)
photo = models.ImageField(upload_to=get_file_path)
这是我的观点:
def uploadImageView(request):
if request.method == 'POST':
form = UploadImageForm(request.POST, request.FILES)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.save()
return redirect('/')
else:
form = UploadImageForm()
return render(request, 'uploadImagePage.html', 'uploadImageForm': form)
但这只会保存正在上传的图像。如何保存图像的缩略图版本以及图像的缩略图版本具有完全相同的名称,除了后面带有单词“thumbail”?
我阅读的教程说我可以做到
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS)
要获取缩略图,但在我的情况下,图像甚至还没有保存。
【问题讨论】:
【参考方案1】:基于 xjtian 的回答。这适用于 Python 3:
import os.path
from PIL import Image
from io import BytesIO
from django.core.files.base import ContentFile
from .my_app_settings import THUMB_SIZE
class Photo(models.Model):
photo = models.ImageField(upload_to='photos')
thumbnail = models.ImageField(upload_to='thumbs', editable=False)
def save(self, *args, **kwargs):
if not self.make_thumbnail():
# set to a default thumbnail
raise Exception('Could not create thumbnail - is the file type valid?')
super(Photo, self).save(*args, **kwargs)
def make_thumbnail(self):
image = Image.open(self.photo)
image.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
thumb_name, thumb_extension = os.path.splitext(self.photo.name)
thumb_extension = thumb_extension.lower()
thumb_filename = thumb_name + '_thumb' + thumb_extension
if thumb_extension in ['.jpg', '.jpeg']:
FTYPE = 'JPEG'
elif thumb_extension == '.gif':
FTYPE = 'GIF'
elif thumb_extension == '.png':
FTYPE = 'PNG'
else:
return False # Unrecognized file type
# Save thumbnail to in-memory file as StringIO
temp_thumb = BytesIO()
image.save(temp_thumb, FTYPE)
temp_thumb.seek(0)
# set save=False, otherwise it will run in an infinite loop
self.thumbnail.save(thumb_filename, ContentFile(temp_thumb.read()), save=False)
temp_thumb.close()
return True
【讨论】:
这会在尝试在管理应用程序中编辑照片实例而不重新上传图像(django 1.11,python 3.6)时抛出ValueError: seek of closed file
,这很方便,因为您不需要重新生成如果图像未更改,则为缩略图。考虑到这一点,可以在调用self.make_thumbnail()
之前添加if not self.photo.closed:
这是否意味着缩略图会在 每次 调用 save 时生成,即使 photo
没有更改(例如,如果模型有额外的字段并且您想要更新那些)?【参考方案2】:
为此,您应该在当前的UserImages
模型中添加一个新的ImageField
来保存缩略图,然后在保存完整图像后覆盖您的save
方法来创建和保存缩略图。
我已经从我的一个项目中改编了以下 sn-p 代码,我很确定这将完全满足您的需要:
from cStringIO import StringIO
import os
from django.db import models
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage as storage
from PIL import Image
# Thumbnail size tuple defined in an app-specific settings module - e.g. (400, 400)
from app.settings import THUMB_SIZE
class Photo(models.Model):
"""
Photo model with automatically generated thumbnail.
"""
photo = models.ImageField(upload_to='photos')
thumbnail = models.ImageField(upload_to='thumbs', editable=False)
def save(self, *args, **kwargs):
"""
Make and save the thumbnail for the photo here.
"""
super(Photo, self).save(*args, **kwargs)
if not self.make_thumbnail():
raise Exception('Could not create thumbnail - is the file type valid?')
def make_thumbnail(self):
"""
Create and save the thumbnail for the photo (simple resize with PIL).
"""
fh = storage.open(self.photo.name, 'r')
try:
image = Image.open(fh)
except:
return False
image.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
fh.close()
# Path to save to, name, and extension
thumb_name, thumb_extension = os.path.splitext(self.photo.name)
thumb_extension = thumb_extension.lower()
thumb_filename = thumb_name + '_thumb' + thumb_extension
if thumb_extension in ['.jpg', '.jpeg']:
FTYPE = 'JPEG'
elif thumb_extension == '.gif':
FTYPE = 'GIF'
elif thumb_extension == '.png':
FTYPE = 'PNG'
else:
return False # Unrecognized file type
# Save thumbnail to in-memory file as StringIO
temp_thumb = StringIO()
image.save(temp_thumb, FTYPE)
temp_thumb.seek(0)
# Load a ContentFile into the thumbnail field so it gets saved
self.thumbnail.save(thumb_filename, ContentFile(temp_thumb.read()), save=True)
temp_thumb.close()
return True
【讨论】:
在make_thumbnail
中抛出异常而不是返回False 不是更好吗?这样,您就可以知道异常的确切原因,而不是获得通用的异常消息
如果这条记录的非图像字段被更新,make_thumbnail方法会在编辑时被调用吗?
我建立在您的回复之上,这对我很有帮助。从一个错误到另一个错误,我质疑我的理智。我什至想过要一加仑汽油烧掉我的电脑。【参考方案3】:
我是根据 ziiiro 的回答写的。 它适用于 Django2.2.1。 需要保存图像字段路径的过程。
models.py
from django.db import models
from my.images import make_thumbnail
class Image(models.Model):
image = models.ImageField(upload_to='')
thumbnail = models.ImageField(upload_to='', editable=False)
icon = models.ImageField(upload_to='', editable=False)
def save(self, *args, **kwargs):
# save for image
super(Image, self).save(*args, **kwargs)
make_thumbnail(self.thumbnail, self.image, (200, 200), 'thumb')
make_thumbnail(self.icon, self.image, (100, 100), 'icon')
# save for thumbnail and icon
super(Image, self).save(*args, **kwargs)
我的.images.py
from django.core.files.base import ContentFile
import os.path
from PIL import Image
from io import BytesIO
def make_thumbnail(dst_image_field, src_image_field, size, name_suffix, sep='_'):
"""
make thumbnail image and field from source image field
@example
thumbnail(self.thumbnail, self.image, (200, 200), 'thumb')
"""
# create thumbnail image
image = Image.open(src_image_field)
image.thumbnail(size, Image.ANTIALIAS)
# build file name for dst
dst_path, dst_ext = os.path.splitext(src_image_field.name)
dst_ext = dst_ext.lower()
dst_fname = dst_path + sep + name_suffix + dst_ext
# check extension
if dst_ext in ['.jpg', '.jpeg']:
filetype = 'JPEG'
elif dst_ext == '.gif':
filetype = 'GIF'
elif dst_ext == '.png':
filetype = 'PNG'
else:
raise RuntimeError('unrecognized file type of "%s"' % dst_ext)
# Save thumbnail to in-memory file as StringIO
dst_bytes = BytesIO()
image.save(dst_bytes, filetype)
dst_bytes.seek(0)
# set save=False, otherwise it will run in an infinite loop
dst_image_field.save(dst_fname, ContentFile(dst_bytes.read()), save=False)
dst_bytes.close()
【讨论】:
这一直有效,直到您离开基本媒体根目录,IE,upload_to='my_sub_folder'。我最终得到了在 mediaroot/my_sub_folder/my_subfolder/ 中创建的缩略图。将dst_name, dst_ext
行更新为:dst_name, dst_ext = os.path.splitext(os.path.basename(src_image_field.name))
并将 dst_fname 行更新为 dst_fname = ''.format(dst_name, sep, name_suffix, dst_ext)
更正了我的问题。【参考方案4】:
如果您不想从头开始实施解决方案,我建议您使用名为 sorl-thumbnail 的 django 应用程序
【讨论】:
我将使用它作为备份,但我正在寻找一种从头开始实施解决方案的方法。我认为这应该不难,我只是找不到合适的 PIL / Pillow 文档。【参考方案5】:可能你忘记保存文件了:
im.save(file + ".thumbnail", "JPEG")
见http://effbot.org/imagingbook/image.htm#examples
【讨论】:
对,但问题是,我是什么?如果您查看我的代码,我没有指定 im。我做instance.save(和instance = form.save)。所以我没有一个 im 开始,因为我无法打开文件(在你喜欢的例子中,他们首先做了 im = Image.open(infile))。如何打开一个我还不知道位置的文件,因为我还没有保存它?我想同时保存图像和图像的缩略图版本。 您应该在保存模型实例后制作缩略图。像这样保存一个实例:obj = instance.save()
。之后,您可以获得这样的图像实例:im = Image.open(obj.photo.path)
。但最好重写类UploadImageForm的save方法并处理tumbnail。【参考方案6】:
有超级简单的方法,
在模型中:
def upload_thumb_dir(self, filename):
path = f'path_to_thumb.jpg'
return path
thumb = ProcessedImageField(upload_to=upload_thumb_dir,
processors=[ResizeToFill(192, 108)],
max_length= 255,
format='JPEG',
default='preview.jpg',
options='quality': 80,
null=True, blank=True
)
# ...
def save(self, *args, **kwargs):
self.thumb = self.rawfile.file
super(VersionPreviews, self).save(*args, **kwargs)
【讨论】:
以上是关于Django / PIL - 上传图像时保存缩略图版本的主要内容,如果未能解决你的问题,请参考以下文章