I had images that were in tiles of the following format:
Each number represents a single tile. I used the following script (with the help of stackoverflow) and stitched the images together. The following is the script that I used to stitch the images together:
from PIL import Image
import os
path_to_file ='tiff-files'
def stich_tile(path_to_file, xx , yy):
images = []
for i in os.listdir(path_to_file):
images.append(i)
if len(images) >= xx*yy:
pass
else:
raise ValueError('not enough images in path_to_file !!!!!!!!!!!')
sq_x = xx
sq_y = yy
img_x = (Image.open(path_to_file+'/'+images[0]).size[0])
img_y = (Image.open(path_to_file+'/'+images[0]).size[1])
img_mode = (Image.open(path_to_file+'/'+images[0]).mode)
new_image = Image.new(img_mode, (img_x*sq_x, img_y*sq_y))
x = 0
y = 0
cnt = 0
for i in images:
with Image.open(path_to_file+'/'+i) as img:
new_image.paste(img, (x,y))
cnt += 1
x += img_x
if cnt == sq_x:
x = 0
y += img_y
cnt = 0
else:
pass
return new_image
stich_tile(path_to_file, 3, 5).save('filename.tiff')
The output saved image looks like the following:
I would like to remove the black image that was created. How do I do that?
here modified script that removes black border from bottom and right of the stitched images... as long as the problem was within the starting images:
import numpy as np
from PIL import Image
import os
# path_to_file ='tiff-files'
# path_to_file ='tiff-files2'
# path_to_file ='tiff-files3'
# path_to_file ='tiff-files5'
# path_to_file ='tiff-files5'
path_to_file ='tiff-files6'
def stich_tile(path_to_file, xx , yy):
images = []
for i in os.listdir(path_to_file):
images.append(i)
images.sort() # sort images alphabetically
# images.sort(key = lambda x: int(x.strip('.tiff').split('-')[1])) ## ---> per path_to_file ='tiff-files3'
images = images[:xx*yy] #-----> riduce lista immagini al numero richiesto
print(images)
print('lenght list of images', len(images), 'x and y requested', xx*yy)
if len(images) >= xx*yy:
pass
else:
# raise ValueError('not enough images in path_to_file !!!!!!!!!!!')
raise ValueError('EXCEPTION not enough images in path_to_file !!!!!!!!!!!', xx*yy,'images needed : ', len(images),'images present !!!')
sq_x = xx
sq_y = yy
img_x = (Image.open(path_to_file+'/'+images[0]).size[0])
img_y = (Image.open(path_to_file+'/'+images[0]).size[1])
img_mode = (Image.open(path_to_file+'/'+images[0]).mode)
print('images[0] size : ', img_x, img_y, img_x*sq_x, img_y*sq_y)
new_image = Image.new(img_mode, (img_x*sq_x, img_y*sq_y))
print('new_image : size :', new_image.size)
x = 0
y = 0
cnt = 0
cnt_cycle = 0
for i in images:
with Image.open(path_to_file+'/'+i) as img:
new_image.paste(img, (x,y))
cnt += 1
cnt_cycle += 1
x += img_x
if cnt == sq_x:
x = 0
y += img_y
cnt = 0
else:
pass
print('count of for i in images cycles', cnt_cycle)
new_image = np.array(new_image)
print(new_image.shape, np.min(new_image), np.max(new_image))
for ar_y in range(new_image.shape[0]-1,0,-1):
res = np.all(new_image[ar_y,:] == (0,0,0))
if res:
new_image = new_image[0:(ar_y),:]
# print('black', ar_y)
else:
print('break at :', ar_y ,' row')
break
print(new_image.shape, np.min(new_image), np.max(new_image))
print(new_image.shape, np.min(new_image), np.max(new_image))
for ar_x in range(new_image.shape[1]-1,0,-1):
res = np.all(new_image[:,ar_x] == (0,0,0))
if res:
new_image = new_image[:,0:(ar_x)]
# print('black', ar_x)
else:
print('break at :', ar_x ,' column')
break
print(new_image.shape, np.min(new_image), np.max(new_image))
new_image = Image.fromarray(new_image)
return new_image
try :
pippo = stich_tile(path_to_file, 3,3)
pippo.show()
# pippo.save('RGB_black_tiff_3X.tiff')
except ValueError as err:
print('stopped', err.args)
could use same approach to remove black border from top/left.
Could be that pillow library has an in built option/function/whatever its called to do the same....
its kind of late here, tested code with 3X3 RGB tiff images with black borders.. let me know if it works
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.