Skip to content

Instantly share code, notes, and snippets.

@miyanda2
Forked from dellsystem/process.py
Created May 21, 2018 12:32
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 miyanda2/14fa682db9186d3c5a978e89b6dd6fbe to your computer and use it in GitHub Desktop.
Save miyanda2/14fa682db9186d3c5a978e89b6dd6fbe to your computer and use it in GitHub Desktop.
Converting and resizing images (pyramidal tiff)
#!/usr/bin/env python
import sys, os
from PIL import Image
import math
# Does all the image processing
# Just give it a directory of images in .tif/.tiff format that are not processed
# It will find the lowest max zoom, and resize all larger images to that zoom level
# Then it will convert all the images to pyramidal tiff format, and put them into a subdirectory
# Then it will copy everything from that subdirectory to /mnt/images/dir
# Run it as su (add this later)
def main(argv):
# Handle the command line arguments
if len(argv) < 2:
print 'Usage: ./process.py directory'
return 1
# Catch the argument
directory = argv[1]
# Create a directory called "processed" within that directory
# If that directory already exists, fail
if os.path.isdir(directory + '/processed'):
print 'There already is a processed directory! Delete it and try again.'
return 1
else:
os.system('mkdir ' + directory + '/processed')
# Now get the contents of the directory
files = os.listdir(directory)
# Try to sort it
files.sort()
# Store the zooms of the files in a list
# Use another list to store filenames (same indices etc)
max_zoom_list = []
filename_list = []
for filename in files:
try:
this_max_zoom = get_max_zoom(directory + '/' + filename)
print 'file: ' + filename + ' has max zoom of: ' + str(this_max_zoom - 1) + ' (' + str(this_max_zoom) + ' zoom levels)'
max_zoom_list.append(this_max_zoom)
filename_list.append(filename)
except IOError:
pass
# Now get the absolute lowest max zoom
lowest_max_zoom = min(max_zoom_list)
# Now figure out which files have a zoom larger than that
for i in range(len(filename_list)):
input_file = directory + '/' + filename_list[i]
output_file = directory + '/processed/' + directory + '_' + str(i+1) + '.tif'
# If the image needs to be resized
if max_zoom_list[i] > lowest_max_zoom:
print filename_list[i] + ' needs to be resized, resizing and converting now'
# Resize this image to the proper size ... prepend resized_
resized_file = directory + '/resized_' + filename_list[i]
resize_image(lowest_max_zoom, input_file, resized_file)
# Now convert it
os.system('vips im_vips2tiff ' + resized_file + ' ' + output_file + ':jpeg:75,tile:256x256,pyramid')
else:
# Just convert this image directly (the right size)
print filename_list[i] + ' does not need to be resized, converting'
os.system('vips im_vips2tiff ' + input_file + ' ' + output_file + ':jpeg:75,tile:256x256,pyramid')
# Now print out the max_zoom this document has
print "This document has a max zoom of: " + str(lowest_max_zoom)
# Calculate the maximum zoom of an image given its filepath
def get_max_zoom(filepath):
# First, find the largest dimension of the image
image = Image.open(filepath)
width = image.size[0]
height = image.size[1]
largest_dim = width if width > height else height
# Now figure out the number of zooms
zoom_levels = math.ceil(math.log((largest_dim + 1) / (257.0), 2)) + 1
return int(zoom_levels)
# Resize an image to the desired zoom
def resize_image(desired_zoom, input_file, output_file):
# Figure out the maximum dimensions we can give it with this zoom
max_dim = (2**(desired_zoom-1) * 257) - 1
# Now get the image dimensions (again ...)
image = Image.open(input_file)
width = image.size[0]
height = image.size[1]
if width > height:
largest_dim = width
width_largest = True
else:
largest_dim = height
width_largest = False
# Now figure out the new dimensions
if width_largest:
# imagemagick will figure out the aspect ratio stuff
new_dimensions = str(max_dim) + 'x' + str(height)
else:
new_dimensions = str(width) + 'x' + str(max_dim)
# Now do the resizing
os.system( 'convert -resize ' + new_dimensions + ' ' + input_file + ' ' + output_file)
if __name__ == "__main__":
sys.exit(main(sys.argv))
#!/usr/bin/env python
import sys, os
from PIL import Image
# Command-line python script for resizing images that are above a certain width/height
# Pass it the maximum height, and it will resize all images above that to that height
# Typically, the maximum height will be some power of 2 (2048, 4096 etc)
# Handle the command-line arguments
if len(sys.argv) < 3:
print 'Usage: ./resize directory max_height'
sys.exit(1)
# Catch the arguments
directory = sys.argv[1]
max_height = int(sys.argv[2])
print max_height
# Create a directory called "resized" within that directory
# All the images, resized or not, will be copied there
os.system('mkdir ' + directory + '/resized')
# Now get the contents of the directory
files = os.listdir(directory)
for filename in files:
# Only do anything for image files (e.g. ignore the resized directory)
try:
image = Image.open(directory + '/' + filename)
image_height = image.size[1]
print image_height
# If the image is not too large, just copy it into the resized directory
if image_height <= max_height:
print filename + ' has height ' + str(image_height) + ' and does not need to be resized'
os.system('cp ' + directory + '/' + filename + ' ' + directory + '/' + 'resized/' + filename)
else:
# Image is too large. First calculate the percentage change
# Int division * 100 should be right
percentage = (max_height * 100) / image_height
print filename + ' needs to be resized ' + str(percentage) + '%'
os.system('convert ' + directory + '/' + filename + ' -resize ' + str(percentage) + '% ' + directory + '/resized/' + filename)
except IOError:
pass
#!/usr/bin/env bash
# A bash script for converting all the files in a directory to pyramidal tiff
if [ $# -lt 2 ]; then
echo 'Usage: tile <directory> <extension>'
else
echo 'Looking for all files in directory: $1/'
FILES=$1/*.$2
i=1
for file in $FILES
do
echo "Processing $file file ..."
convert="vips im_vips2tiff $file $1_$i.tif:jpeg:75,tile:256x256,pyramid"
echo $convert
echo `$convert`
i=$(( $i + 1 )) # ...bash
# break
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment