Skip to content

Instantly share code, notes, and snippets.

@sveetch
Created August 23, 2017 14:23
Show Gist options
  • Save sveetch/5b03c2fb76f6ad42408ec83ae61c5597 to your computer and use it in GitHub Desktop.
Save sveetch/5b03c2fb76f6ad42408ec83ae61c5597 to your computer and use it in GitHub Desktop.
Batch image optimisation, resizing for thumb and html listing
# -*- coding: utf-8 -*-
import os, io
# Some HTML template to produce for Django to list images
ALBUM_TPL = u"""<div class="item">
<img src="{{% static 'images/album/{thumb}' %}}"
alt="{name}">
</div>
"""
# 'image_listing.txt' file may come from a simple 'ls -1'
# Be careful file only contains the files you want, else you
# may need to filter them
with io.open('album_listing.txt', 'r', encoding="utf-8") as fp:
album_list = filter(None, fp.read().split("\n"))
html = ''
for item in album_list:
# Name formatting may largely differ from your needs
name = os.path.splitext(item)[0].replace('-', u' ')[3:]
html += ALBUM_TPL.format(thumb=item, name=name)
print(html)
# Sample of script to batch images, you may adapt it to your needs
#
# Require Mogrify tool from imagemagick
# Have to be used from directory where live images, '-path' is used to give destination of thumbs
#
# A good stackoverflow thread for more samples:
# https://stackoverflow.com/questions/14304480/batch-resize-images-and-output-images-to-new-folder-with-imagemagick
mogrify -resize 400x240 -quality 65 -path ../ *.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment