python - Is there a way to use sorl-thumbnail url without generating the thumnail? -
i want generate thumnail list. opted sorl-thumbnail because seems largely used , developped.
my problem template tag "thumbnail": generates thumbnail image , thumbnail object correctly want url.
i don't mind if thumbnail image not generated/ready, template generation concerns me. can serve thumbnails through custom view, not problem available few people.
so simplify want de-couple url generation thumbnail generation itself.
is possible sorl-thumbnail? if not, know project can?
i prefer not writing own if can it.
p.s. store thumbnails on disk.
i faced same issue in django experience. had requirement generated thumbnails stored on disk. had issue on deployment server pil (back several problems python 3).
we couldn't find us, ended following. ended being imagemagick command line avoid pil.
the idea create custom tag asking thumbnailed image. if not exist, tag create thumbnail , store on disk later use (in subdir name of style). therefore there delay first time visits page. idea comes drupal.
also, thumbnail dimensions specific hard-coded them on project settings, shouldn't difficult change this.
frankly, not sure if best solution, still works. 1 of codes in python gentle (right aware eg. os.join
etc, no time improve , test yet; interested in constructive opinion).
first, here settings:
image_styles = { 'thumbnail_style': { 'type': 'thumbnail', # maintain max of 1 of 2 dimensions, depending on aspect ratio 'size': (150, 1000) }, 'thumbnail_crop_style': { 'type': 'thumbnail-crop', # maintain both dimensions cropping remaining 'size': (150, 150) }, 'thumbnail_upscale_style': { 'type': 'thumbnail-upscale', # same first, allow upscaling 'size': (150, 150) }, }
this custom tag:
from django import template django.template.defaultfilters import stringfilter django.conf import settings subprocess import check_output, call import os register = template.library() @register.filter @stringfilter def image_style(url, style): """ return url of different image style construct appropriately if not exist assumptions: - works linux os; double slashes anyway ignored - media_url local (url in form media_url.. (eg /media/..) :param url: image url :param style: specify style 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) = 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) + + str(target_size_y) if style_def['type'] == 'thumbnail': if target_size_x > source_size_x , 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) + + 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) + + 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
this how use in ntemplate:
<img src="{{ image_field.url|image_style:'my_style' }}">
Comments
Post a Comment