Skip to content

Instantly share code, notes, and snippets.

@alexlib
Last active May 16, 2021 14:04
Show Gist options
  • Save alexlib/ef7df7bfdb3dba1698f4 to your computer and use it in GitHub Desktop.
Save alexlib/ef7df7bfdb3dba1698f4 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
from PIL import Image
import os
import glob
import numpy as np
def crop(im, height, width):
# im = Image.open(infile)
imgwidth, imgheight = im.size
rows = np.int(imgheight/height)
cols = np.int(imgwidth/width)
for i in range(rows):
for j in range(cols):
# 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 = '.'
basename = 'Cam-*.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(f'file no = {filenum}') # keep the numbers for the future
print(f'name is {infile}')
im = Image.open(infile)
imgwidth, imgheight = im.size
print(('Image size is: %d x %d ' % (imgwidth, imgheight)))
height = np.int(imgheight/2)
width = np.int(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)))
@linux-devil
Copy link

This gives images in grayscale , what if I need colored images

@vaghawan
Copy link

@linux-devil, just replace img=Image.new('L', (width,height), 255) with img=Image.new('RGB', (width,height), 255). You will get the color images.

@atzin-em
Copy link

atzin-em commented Mar 7, 2020

do you have code for stitching it back together?

@alexlib
Copy link
Author

alexlib commented Mar 7, 2020

do you have code for stitching it back together?

@atzin-em Not sure why would you like to stick back together - the original image im is the one which is full size already. Just use it.

@atzin-em
Copy link

atzin-em commented Mar 8, 2020

I need to edit and replace certain parts of the split up image and then put it back together.

@alexlib
Copy link
Author

alexlib commented Mar 10, 2020

I need to edit and replace certain parts of the split up image and then put it back together.

this is usually done on the image itself, no need to split it into separate files, just use slicing and numpy. For instance:

I = numpy.asarray(PIL.Image.open('test.jpg'))
 I[:512,:512] *= 2
im = PIL.Image.fromarray(numpy.uint8(I))

@moesadoun
Copy link

what if I want to split it into [ m ] by [ n ] what do I have to change?

@anjanaouseph
Copy link

image

img=Image.new('RGB', (int(width),int(height)), 255)
Shouldn't we convert the width and height to integer type? Without that, it's showing an error.
Image.new function accepts the pixel sizes so I think it has to be integers.

@alexlib
Copy link
Author

alexlib commented May 13, 2020

Thanks for the message. I have updated the code. Please:

  1. use Python 3 (preferable Conda distribution)
  2. Install numpy, pillow (replaces PIL)
  3. run with a demo file included Cam-0001.tif

you should get 4 images cam1.10000, cam2.10000 and so on.

@alexlib
Copy link
Author

alexlib commented May 13, 2020

@alexlib
Copy link
Author

alexlib commented May 13, 2020

image

img=Image.new('RGB', (int(width),int(height)), 255)
Shouldn't we convert the width and height to integer type? Without that, it's showing an error.
Image.new function accepts the pixel sizes so I think it has to be integers.

please send a link to your image - I cannot debug it with my images.

@anjanaouseph
Copy link

image
img=Image.new('RGB', (int(width),int(height)), 255)
Shouldn't we convert the width and height to integer type? Without that, it's showing an error.
Image.new function accepts the pixel sizes so I think it has to be integers.

please send a link to your image - I cannot debug it with my images.
your code works fine on my image. No issues, I got 4 images from the original one. I just wanted to notify you about that error. Thanks a lot for the code.

@alexlib
Copy link
Author

alexlib commented May 13, 2020

Thanks. in the new version I think I fixed it with

        height = np.int(imgheight/2)
        width = np.int(imgwidth/2)

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