Skip to content

Instantly share code, notes, and snippets.

@kdeloach
Created December 15, 2015 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdeloach/e4db23ee4933a919ac4e to your computer and use it in GitHub Desktop.
Save kdeloach/e4db23ee4933a919ac4e to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
import sys
import os.path
import subprocess
from PIL import Image, ImageOps
def thumbnail(src_path, dst_path, width, height, **options):
"""
Generate a thumbnail image.
"""
image = Image.open(src_path)
thumb = ImageOps.fit(
image, (width, height), method=Image.ANTIALIAS)
thumb.save(dst_path, **options)
def to_png(src_path):
"""
Use `gdal_translate` to convert source image to PNG.
"""
dst_path = '{}.jpg'.format(src_path)
if not os.path.isfile(dst_path):
subprocess.call(['gdal_translate', src_path, dst_path, '-of', 'JPEG'])
return dst_path
tiff_path = sys.argv[1]
tiff_path = os.path.expanduser(tiff_path)
print('Path: {}'.format(tiff_path))
output_path = sys.argv[2]
image = Image.open(tiff_path)
w, h = image.size
print('Width: {} Height: {}'.format(w, h))
png_path = to_png(tiff_path)
print('Saving to {}'.format(output_path))
thumbnail(png_path, output_path, w, h, quality=0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment