Image resizing with django?

前端 未结 12 1554
温柔的废话
温柔的废话 2020-12-23 18:06

I\'m new to Django (and Python) and I have been trying to work out a few things myself, before jumping into using other people\'s apps. I\'m having trouble understanding whe

12条回答
  •  孤城傲影
    2020-12-23 18:36

    Another alternative is to use Imagemagick directly, if you want to avoid several difficulties with Pillow and Python 3 such as this one.

    from subprocess import call
    call(['convert', img_path_file_name, '-thumbnail', target_size_str, '-antialias', style_path_file_name])
    

    You could call this on model save, or on a template tag to generate an one-off manipulated copy of the original file in a file-caching fashion, or even a celery task.

    You can get an example of how I have used this in one of my projects:

    • main function
    • relevant settings
    • templatetag
    • how to use

提交回复
热议问题