Skip to content

Instantly share code, notes, and snippets.

Created January 9, 2013 20:58
Show Gist options
  • Save anonymous/4496882 to your computer and use it in GitHub Desktop.
Save anonymous/4496882 to your computer and use it in GitHub Desktop.
This is a bit of Python code that uses PIL to slice very long images into segment sizes of your choice. For example take a 10,000px tall image, and slice it into 10 10,00px tall images.
import Image
from __future__ import division
import math
import os
"""
This is a bit of Python code that uses PIL to slice very long images into segment sizes of your choice.
For example take a 10,000px tall image, and slice it into 10 10,00px tall images.
thanks to the great docs at http://www.packtpub.com/article/python-image-manipulation
"""
def enum(iterable, start = 1):
"""enumerate but with a starting position"""
n = start
for i in iterable:
yield n, i
n += 1
def long_slice(image_path, out_name, outdir, slice_size):
"""slice an image into parts slice_size tall"""
img = Image.open(image_path)
width, height = img.size
upper = 0
slices = int(math.ceil(height/slice_size))
for i,slice in enum(range( slices )):
left = 0
upper = upper
if i == slices:
lower = height
else:
lower = int(i * slice_size)
bbox = (left, upper, width, lower)
working_slice = img.crop(bbox)
upper += slice_size
working_slice.save(os.path.join(outdir, "slice_" + out_name + "_" + str(i)+".png"))
#use it like this
long_slice("variantCaller.png","tallcat", "/tmp", 1000)
@gourneau
Copy link

gourneau commented Jan 9, 2013

whoops this is mine, I was anon by mistake

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