Python 有没有一种方法可以在不生成thumnail的情况下使用sorl缩略图url?

Python 有没有一种方法可以在不生成thumnail的情况下使用sorl缩略图url?,python,django,thumbnails,sorl-thumbnail,Python,Django,Thumbnails,Sorl Thumbnail,我想生成一个thumnail列表。我选择了sorl缩略图,因为它似乎被大量使用和开发 我的问题是模板标记“缩略图”:它正确生成缩略图图像和缩略图对象,但我只需要url 我不介意缩略图是否还没有生成/准备好,只关心模板的生成。 我可以通过自定义视图提供我的缩略图,这不是问题,因为这些缩略图只对少数人可用 因此,为了简化,我想将url生成与缩略图生成本身分离 有没有可能用sorl缩略图?如果没有,你知道另一个项目可以吗 如果有些东西已经可以写,我宁愿不写自己的 另外,我会将缩略图存储在磁盘上。我在D

我想生成一个thumnail列表。我选择了sorl缩略图,因为它似乎被大量使用和开发

我的问题是模板标记“缩略图”:它正确生成缩略图图像和缩略图对象,但我只需要url

我不介意缩略图是否还没有生成/准备好,只关心模板的生成。 我可以通过自定义视图提供我的缩略图,这不是问题,因为这些缩略图只对少数人可用

因此,为了简化,我想将url生成与缩略图生成本身分离

有没有可能用sorl缩略图?如果没有,你知道另一个项目可以吗

如果有些东西已经可以写,我宁愿不写自己的


另外,我会将缩略图存储在磁盘上。

我在Django的早期经历中遇到了完全相同的问题。我要求所有生成的缩略图都存储在磁盘上。我们在使用PIL的部署服务器上也遇到了一个问题(当时Python3有几个问题)

我们找不到任何可以帮助我们的东西,所以我们最终得到了以下结果。为了避免PIL,我们还从命令行使用imagemagick

其想法是创建一个定制标签,要求提供一个缩略图图像。如果不存在,标记将创建缩略图并将其存储在磁盘上以供以后使用(在具有样式名称的子目录中)。因此,只有第一次有人访问页面时才会出现延迟。这个想法来自Drupal

此外,缩略图尺寸是特定的,因此我们在项目设置中对其进行了硬编码,但更改此设置应该不会太困难

坦率地说,我不确定这是否是最好的解决方案,但它仍然有效。另外,这也是我早期使用Python编写的代码之一,所以请对它温和一点(目前我完全了解,例如,
os.join
等,但还没有时间改进和测试;但我对您的建设性意见也非常感兴趣)

首先,以下是设置:

IMAGE_STYLES = {
    'thumbnail_style': {
        'type': 'thumbnail',  # maintain a max of one of the two dimensions, depending on aspect ratio
        'size': (150, 1000)
    },
    'thumbnail_crop_style': {
        'type': 'thumbnail-crop',  # maintain both dimensions by cropping the remaining
        'size': (150, 150)
    },
    'thumbnail_upscale_style': {
        'type': 'thumbnail-upscale',  # same as first, but allow upscaling
        'size': (150, 150)
    },
}
这是自定义标记:

from django import template
from django.template.defaultfilters import stringfilter
from django.conf import settings
from subprocess import check_output, call
import os


register = template.Library()


@register.filter
@stringfilter
def image_style(url, style):
    """ Return the url of different image style
    Construct appropriately if not exist

    Assumptions:
    - Works only for Linux OS; double slashes are anyway ignored
    - MEDIA_URL is local (url is in form MEDIA_URL.. (eg /media/..)

    :param url: An image url
    :param style: Specify style to return image
    :return: image url of specified style
    """
    path_file_name = settings.BASE_DIR + url
    style_url_path = '/'.join((os.path.dirname(url), style))
    style_url = '/'.join((style_url_path, os.path.basename(url)))
    style_path = settings.BASE_DIR + style_url_path
    style_path_file_name = settings.BASE_DIR + style_url
    style_def = settings.IMAGE_STYLES[style]

    if not os.path.exists(style_path_file_name):
        if not os.path.exists(style_path):
            os.makedirs(style_path)
        by = chr(120)   # x
        plus = chr(43)  # +
        source_size_str = str(check_output(['identify', path_file_name])).split(' ')[2]
        source_size_array = source_size_str.split(by)
        source_size_x = int(source_size_array[0])
        source_size_y = int(source_size_array[1])
        target_size_x = style_def['size'][0]
        target_size_y = style_def['size'][1]
        target_size_str = str(target_size_x) + by + str(target_size_y)
        if style_def['type'] == 'thumbnail':
            if target_size_x > source_size_x and target_size_y > source_size_y:
                target_size_str = source_size_str
            call(['convert', path_file_name, '-thumbnail', target_size_str, '-antialias', style_path_file_name])
        elif style_def['type'] == 'thumbnail-upscale':
            call(['convert', path_file_name, '-thumbnail', target_size_str, '-antialias', style_path_file_name])
        elif style_def['type'] == 'thumbnail-crop':
            source_ratio = float(source_size_x) / float(source_size_y)
            target_ratio = float(target_size_x) / float(target_size_y)
            if source_ratio > target_ratio:  # crop vertically
                crop_target_size_x = int(source_size_y * target_ratio)
                crop_target_size_y = source_size_y
                offset = (source_size_x - crop_target_size_x) / 2
                crop_size_str = str(crop_target_size_x) + by + str(crop_target_size_y) + plus + str(offset) + plus + '0'
            else:  # crop horizontally
                crop_target_size_x = source_size_x
                crop_target_size_y = int(source_size_x / target_ratio)
                offset = (source_size_y - crop_target_size_y) / 2
                crop_size_str = str(crop_target_size_x) + by + str(crop_target_size_y) + plus + '0' + plus + str(offset)
            call(['convert', path_file_name, '-crop', crop_size_str, style_path_file_name])
            call(['convert', style_path_file_name, '-thumbnail', target_size_str, '-antialias', style_path_file_name])
    return style_url
以下是如何在ntemplate中使用:

<img src="{{ image_field.url|image_style:'my_style' }}">


在视图内部或通过信号生成缩略图,并直接在模板中使用url。在视图中生成缩略图需要花费太长时间。我更喜欢快速的html回答,然后浏览器会通过GET请求请求图像。然后使用异步任务生成图像。如果没有人请求,我不想生成图像。真的,我不介意图像是否需要时间生成,但我希望快速生成模板。我的问题是:有没有办法做到这一点?不是如何避免它。如果没有带有sorl缩略图的普通代码的缩略图,就无法生成url,您的另一个选择是覆盖当前实现并添加此类行为。这是一段非常复杂的代码。不幸的是,它与sorl缩略图具有相同的缺陷:如果在模板的循环中使用它,则生成缩略图将花费很长时间。此外,生成将是顺序的,并且将比并行请求生成花费更多的时间。