Created
January 9, 2013 20:58
-
-
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.
This file contains 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
whoops this is mine, I was anon by mistake