-
-
Save doppiomacchiatto/8540132efb7fcf5fa0f6cf8261da5910 to your computer and use it in GitHub Desktop.
Python code using PIL to split the image into 4 quarters. useful for the four-view image splitter 3D-PTV project on http://www.openptv.net
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
import Image | |
import os | |
import glob | |
def crop(im,height,width): | |
# im = Image.open(infile) | |
imgwidth, imgheight = im.size | |
for i in range(imgheight//height): | |
for j in range(imgwidth//width): | |
# print (i,j) | |
box = (j*width, i*height, (j+1)*width, (i+1)*height) | |
yield im.crop(box) | |
if __name__=='__main__': | |
# change the path and the base name of the image files | |
imgdir = '../test_splitter/img' | |
basename = 'img-*.tif' | |
filelist = glob.glob(os.path.join(imgdir,basename)) | |
for filenum,infile in enumerate(filelist): | |
# infile='/Users/alex/Documents/PTV/test_splitter/cal/Camera 1-1-9.tif' | |
print filenum # keep the numbers as we change them here | |
print infile | |
im = Image.open(infile) | |
imgwidth, imgheight = im.size | |
print 'Image size is: %d x %d ' % (imgwidth, imgheight) | |
height = imgheight/2 | |
width = imgwidth/2 | |
start_num = 0 | |
for k,piece in enumerate(crop(im,height,width),start_num): | |
# print k | |
# print piece | |
img=Image.new('L', (width,height), 255) | |
# print img | |
img.paste(piece) | |
path = os.path.join("cam%d_1%05d.tif" % (int(k+1),filenum)) | |
img.save(path) | |
os.rename(path,os.path.join("cam%d.1%05d" % (int(k+1),filenum))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment