django 波形变化图像信号

i2loujxw  于 2023-03-31  发布在  Go
关注(0)|答案(1)|浏览(152)

这是我裁剪原始图像的信号。它在上传时工作得很好,但当我在'wagtail.images.views.images. edit'中更改图像时不起作用。
找到了解决方案:更新

/wagtail.hooks.py/

@receiver(post_save, sender=get_image_model())
def resize_images_on_upload_or_edit(sender, instance, **kwargs):
    if (instance.width and instance.height) > 2000 or instance.file_size > 500_000:
        try:
            if get_object_or_404(sender, pk=instance.pk):
                croped_image = instance.get_rendition('max-2000x2000|jpegquality-80')
                croped_file = croped_image.file.path
                if os.path.exists(croped_file):
                    instance.width = croped_image.width
                    instance.height = croped_image.height
                    instance.file_size = os.path.getsize(croped_file)
                    original_file = instance.file.path
                    os.remove(original_file)
                    instance.save()
                    try:
                        os.rename(croped_file, original_file)
                    except FileExistsError as e:
                        os.replace(croped_file, original_file)
                        print( e)
        except IOError as e:
            print(e)
bkhjykvo

bkhjykvo1#

如果您希望代码在编辑现有图像时运行,而不仅仅是在创建图像时运行,则应删除if created:检查。

相关问题