这是我的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)
获取缩略图但在我的情况下,图像甚至还没有保存。
答案 0 :(得分:15)
要执行此操作,您应该向当前ImageField
模型添加新的UserImages
以保留缩略图,然后覆盖您的save
方法,以便在完整后创建并保存缩略图图像已保存。
我已经从我的一个项目中调整了以下代码片段,我确信这将完全符合您的需要:
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
答案 1 :(得分:14)
基于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
答案 2 :(得分:0)
如果您不想从头开始实施解决方案,我建议您使用名为sorl-thumbnail的django应用
答案 3 :(得分:0)
答案 4 :(得分:0)
我是根据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)
my.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()
答案 5 :(得分:0)
还有超级简单的方法
在型号中:
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)