utils update; use imagemagick to thumbnails
This commit is contained in:
1 parent
26c0534372
commit
911146321e
3 files changed
+53
-14
No files matched your search
@@ -42,7 +42,7 @@ def create_event(doc):
|
||||
|
||||
doc['id'] = doc['id'] if 'id' in doc else url_encode_text(doc['title'])
|
||||
|
||||
url_base = 'https://static.shburg.org/art/image/event-{id}-{img}-{size}{ext}'
|
||||
url_base = 'https://static.shburg.org/art/images/event-{id}-{img}-{size}{ext}'
|
||||
images_dir = '/Users/tmshv/Dropbox/Dev/Hud School/Static/images'
|
||||
sizes = settings.event_image_sizes
|
||||
|
||||
@@ -58,8 +58,11 @@ def create_event(doc):
|
||||
url_fn = lambda size, ext: url_base.format(id=doc['id'], img=img_id, size=size, ext=ext.lower())
|
||||
|
||||
image = create_image(img_path, sizes, url_fn, images_dir)
|
||||
images.append(image)
|
||||
img.set('src', image['data']['big']['url'])
|
||||
if image:
|
||||
images.append(image)
|
||||
img.set('src', image['data']['big']['url'])
|
||||
else:
|
||||
print('fail: ', doc['folder'], src)
|
||||
|
||||
doc['post'] = lxml.html.tostring(post_html).decode('utf-8')
|
||||
doc['images'] = images
|
||||
|
||||
@@ -4,13 +4,16 @@ from PIL import Image
|
||||
|
||||
from utils.hash import file_hash
|
||||
from utils.image import resize
|
||||
from utils.image.resize import optimize, thumbnail
|
||||
from utils.image.resize import optimize, thumbnail, orient
|
||||
|
||||
|
||||
def read_image(src):
|
||||
try:
|
||||
return Image.open(src)
|
||||
except FileNotFoundError:
|
||||
i = Image.open(src)
|
||||
return i
|
||||
# return orient(i)
|
||||
except Exception as e:
|
||||
print(src, e)
|
||||
return None
|
||||
|
||||
|
||||
@@ -33,12 +36,19 @@ def create_image(file, sizes, url_fn, output_dir, skip_processing=False):
|
||||
for size in sizes:
|
||||
size_name, width, height = size
|
||||
|
||||
image_width, image_height = image.size
|
||||
if image_height > image_width:
|
||||
width, height = height, width
|
||||
|
||||
image_url = url_fn(size_name, ext)
|
||||
image_filename = os.path.basename(image_url)
|
||||
d = os.path.join(output_dir, image_filename)
|
||||
local_image_path = os.path.join(output_dir, image_filename)
|
||||
|
||||
if not skip_processing:
|
||||
thumb = optimize(image, d, quality=90) if size_name == 'original' else thumbnail(image, d, (width, height))
|
||||
if size_name == 'original':
|
||||
thumb = optimize(file, local_image_path, quality=90)
|
||||
else:
|
||||
thumb = thumbnail(file, local_image_path, (width, height))
|
||||
width, height = thumb.size
|
||||
|
||||
result[size_name] = {
|
||||
|
||||
@@ -2,14 +2,23 @@ import os
|
||||
|
||||
from subprocess import call
|
||||
|
||||
from PIL import Image
|
||||
|
||||
def thumbnail(image, dest, size, ext=None):
|
||||
image.thumbnail(size)
|
||||
image.save(override_extension(dest, ext))
|
||||
return image
|
||||
import utils.image
|
||||
|
||||
|
||||
def optimize(image, dest, quality=85, ext=None):
|
||||
def thumbnail(src, dest, size, ext=None):
|
||||
image_magick_resize(src, dest, size, quality=85)
|
||||
return utils.image.read_image(dest)
|
||||
|
||||
# image = read_image(src)
|
||||
# image.thumbnail(size)
|
||||
# image.save(override_extension(dest, ext))
|
||||
# return image
|
||||
|
||||
|
||||
def optimize(src, dest, quality=85, ext=None):
|
||||
image = utils.image.read_image(src)
|
||||
image.save(override_extension(dest, ext), quality=quality)
|
||||
return image
|
||||
|
||||
@@ -21,9 +30,26 @@ def override_extension(dest, ext=None):
|
||||
return dest
|
||||
|
||||
|
||||
def orient(image):
|
||||
orientation = 0x0112
|
||||
rotations = {
|
||||
3: Image.ROTATE_180,
|
||||
6: Image.ROTATE_270,
|
||||
8: Image.ROTATE_90
|
||||
}
|
||||
|
||||
if hasattr(image, '_getexif'):
|
||||
exif = image._getexif()
|
||||
if exif is not None:
|
||||
o = exif[orientation]
|
||||
if o in rotations:
|
||||
return image.transpose(rotations[o])
|
||||
return image
|
||||
|
||||
|
||||
def image_magick_resize(input_file, output_file, size, quality=85):
|
||||
s = '%dx%d' % size
|
||||
call(['convert', input_file, '-resize', s, '-quality', str(quality), output_file])
|
||||
call(['convert', input_file, '-strip', '-auto-orient', '-resize', s, '-quality', str(quality), output_file])
|
||||
|
||||
|
||||
def image_magick_pdf_to_img(input_file, output_file):
|
||||
|
||||
Reference in new issue
Block a user