Skip to content

Instantly share code, notes, and snippets.

@lozhn
Last active December 21, 2015 03:18
Show Gist options
  • Save lozhn/6240771 to your computer and use it in GitHub Desktop.
Save lozhn/6240771 to your computer and use it in GitHub Desktop.
Converts all images in a directory with a 600px height pattern. Also script makes 120px height thumbnails. Images name pattern 00.jpg, 01.jpg, ... All thubnails will have the same names but with suffix '_s'(00_s.jpg, 01_s.jpg, ... required http://wand-py.org/ Usage: $ python img-to-pattern.py <path>
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, sys, wand
from wand.image import Image
if len(sys.argv) > 1 : _path = sys.argv[1]
else : _path = './'
os.chdir(_path)
pattern = '{:0>2}'
numb = 0
orig_names = {}
f = open('names.txt', 'w')
for item in os.listdir('.'):
if item[-3:].lower() in ['jpg', 'png']:
name = pattern.format(numb) + '.jpg'
orig_names[item.decode('utf-8')] = name
f.write('%s - %s\n' % (name[:-4], item[:-4]))
os.rename(item, name)
with wand.image.Image(filename = name) as img:
img.transform(resize = 'x600')
img.compression_quality = 85
img.save(filename = name)
img.transform(resize = 'x120')
img.save(filename = name[:-4] + '_s.jpg')
numb += 1
#print 'img %s done' % name
f.close()
print 'names.txt is written'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment