Skip to content

Instantly share code, notes, and snippets.

@ghukill
Last active July 18, 2017 20:40
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 ghukill/54bf255c831fc00b5b9930acd5600d24 to your computer and use it in GitHub Desktop.
Save ghukill/54bf255c831fc00b5b9930acd5600d24 to your computer and use it in GitHub Desktop.
# small scrip to split pages when the desired midpoint drifts over the course of set of images
# requires imagemagick, specifically "convert" command
import os
import sys
def split_images(files, start_percentage, end_percentage, start_page, end_page):
# determine percentage bump
steps = len(files) - 1
percentage_bump = float(end_percentage - start_percentage) / float(steps)
print "incrementing each crop by %s" % percentage_bump
# loop through files and fire
for i,filename in enumerate(files):
# DEBUG: range of pages to operate on
if i >= start_page and i < end_page:
print "splitting %s" % filename
# determine offset
if i == 0:
percentage_offset = 0
else:
percentage_offset = float(i * percentage_bump)
print "firing crop with percentage_offset %s" % percentage_offset
# crop
lcmd = 'convert %s' % filename + ' -gravity west -crop %s' % (start_percentage + percentage_offset) + 'x100%' + ' %s_0.jpg' % filename.split('.')[0]
print lcmd
os.system(lcmd)
rcmd = 'convert %s' % filename + ' -gravity east -crop %s' % (100 - (start_percentage + percentage_offset)) + 'x100%' + ' %s_1.jpg' % filename.split('.')[0]
print rcmd
os.system(rcmd)
if __name__ == '__main__':
images = [i for i in os.listdir('.') if i.endswith('.jpg')]
'''
expects arguments:
- starting percentage
- ending percentage
- start page (counting from zero)
- end page
'''
split_images(images, float(sys.argv[1]), float(sys.argv[2]), int(sys.argv[3]), int(sys.argv[4]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment