【问题标题】:Specify a filename suffix with easy-thumbnails in django在 django 中使用简单的缩略图指定文件名后缀
【发布时间】:2015-09-02 06:59:49
【问题描述】:

我正在使用 django 中的 easy-thumbnails 库来创建缩略图。但是,我不知道如何覆盖缩略图命名过程。目前,lib 正在将缩略图大小附加到文件名,但我想指定一个自定义名称,例如_大。请问这个怎么做?

【问题讨论】:

    标签: django easy-thumbnails


    【解决方案1】:

    django-easy-thumbnails 使用default renaming function。您可以编写自己的命名函数并在设置中将其设置为库应使用的默认命名函数,如此处所述THUMBNAIL_NAMER:

    myapp.utils

    def namer(thumbnailer, prepared_options, source_filename,
              thumbnail_extension, **kwargs):
       # do something and return name
       pass
    

    settings.py

    THUMBNAIL_NAMER = 'myapp.utils.namer'
    

    【讨论】:

    【解决方案2】:

    您可以定义自己的thumbnail-processor 并将其作为最后一行:http://easy-thumbnails.readthedocs.org/en/latest/ref/settings/#easy_thumbnails.conf.Settings.THUMBNAIL_PROCESSORS

    THUMBNAIL_PROCESSORS = (
       'easy_thumbnails.processors.colorspace',
       'easy_thumbnails.processors.autocrop',
       'easy_thumbnails.processors.scale_and_crop',
       'easy_thumbnails.processors.filters',
       'easy_thumbnails.processors.background',
       'yourProject.thumbnail_processors.renaming', #<---- your custom one 
    )
    

    你的处理器文件 (yourProject/thumbnail_processors.py) 看起来像:

    def renaming(image, bang=False, **kwargs):
        """
        rename the filename here and just return the image
        """
        return image
    

    虽然没有测试

    【讨论】:

    • 感谢您的回答,但我不太确定如何“重命名文件名”。该图像是一个 PIL 对象,没有名为 filename 的属性。
    猜你喜欢
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 2012-04-04
    • 1970-01-01
    • 2016-05-10
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多