Skip to content

Instantly share code, notes, and snippets.

@twobob
Last active May 3, 2017 16:08
Show Gist options
  • Save twobob/f5dd8a25195d730801df25bf048c3272 to your computer and use it in GitHub Desktop.
Save twobob/f5dd8a25195d730801df25bf048c3272 to your computer and use it in GitHub Desktop.
create thumbails for all JPG files in a folder as 128 128 JPEG files
import os, sys
import PythonMagick
from os.path import expanduser
import glob
import time
directory = expanduser('~\\pictures\\clarifai\\')
thumbsdirectory = expanduser('~\\pictures\\clarifai\\thumbs\\')
thumbs=glob.glob1(thumbsdirectory, "*.THUMB.JP?")
jpgs=glob.glob1(directory, "*.JPG")
thumbs = [suffix.replace('.THUMB.JPEG', '.JPG') for suffix in thumbs]
pictures = list(set(jpgs)^set(thumbs))
def Resize(image, w, h):
img = PythonMagick.Image(image) # copy
s = "!%sx%s" % (w, h)
img.resize(s)
return img
def Paste(destImage, sourceImage, x1, y1):
op = PythonMagick.CompositeOperator.OverCompositeOp
# a kluge to prevent changing to grayscale - doesn't always happen
type = destImage.type
destImage.composite(sourceImage, x1, y1, op)
destImage.type = type
return destImage
logo = PythonMagick.Image(directory+"overlay\\watermark.png") # make a copy of the watermark overlay - supports transparency
for infile in pictures:
outfile = os.path.splitext(infile)[0] + ".THUMB.JPG"
time.sleep(.1)
try:
progress="Before Open"
im = PythonMagick.Image(directory+infile) # make a copy
progress="After Open"
im = Resize(im, 128, 128)
im = Paste(im, logo, 0, 0)
#im.unsharpmask(.7,0.2,0.4,0.008) # unsharpmask(radius,sigma,amount,threshold)
im.write(thumbsdirectory+outfile)
progress="Complete"
except IOError:
print ("cannot create thumbnail for", directory+infile, thumbsdirectory+outfile, progress)
@twobob
Copy link
Author

twobob commented May 3, 2017

This expects ANOTHER file to be in directory+"overlay\watermark.png" that will be melded over the JPG.
use transparency.

@twobob
Copy link
Author

twobob commented May 3, 2017

Don't want to use ImageMagick?
You could use python directly but YMMV
http://stackoverflow.com/questions/22558976/oserror-decoder-jpeg-not-available-on-windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment