dcg*_*oss 5 python django image django-imagekit pillow
我正在使用Pillow,Django和django-imagekit.
我希望能够有一个个人资料图片模型字段(可能使用ProcessedImageField来自imagekit 的类),它将获取任何图像,转换为JPEG,将其裁剪为150x150,并使其文件大小为5KB.
前两个很容易:
profile_picture = imagekit.models.ProcessedImageField(upload_to=get_profile_picture_file_path,
format='JPEG',
processors=[ResizeToFill(height=150, width=150)]
)
Run Code Online (Sandbox Code Playgroud)
但是如何确保文件大小为5KB?我可以使用类似options={'quality': 60}参数的东西ProcessedImageField,但这似乎只相对于原始文件大小(据我所知).
解决方案不必使用django-imagekit,但这是首选.
也许是这样。上传后检查图像的大小并将其删除或在重写save方法中减少更多:
class Images(models.Model):
profile_picture = imagekit.models.ProcessedImageField(upload_to=get_profile_picture_file_path,
format='JPEG',
processors=[ResizeToFill(height=150, width=150)]
)
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
if os.stat(get_profile_picture_file_path + "/" + self.profile_picture.new_name).st_size > max_size:
do_something_further_image_processing_to_decrease_size
super(Images, self).save()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
538 次 |
| 最近记录: |