Created
December 15, 2015 15:13
-
-
Save kdeloach/e4db23ee4933a919ac4e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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