如何解决将图像拼接在一起后如何去除黑色空间?
我有以下格式的图块图像:
每个数字代表一个图块。我使用了以下脚本(在 stackoverflow 的帮助下)并将图像拼接在一起。以下是我用来拼接图像的脚本:
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')
输出保存的图像如下所示:
我想删除创建的黑色图像。我该怎么做?
解决方法
这里修改的脚本从拼接图像的底部和右侧移除黑色边框......只要问题出在起始图像中:
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 : ','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,-1):
res = np.all(new_image[ar_y,:] == (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.max(new_image))
print(new_image.shape,np.max(new_image))
for ar_x in range(new_image.shape[1]-1,-1):
res = np.all(new_image[:,ar_x] == (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.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)
可以使用相同的方法从顶部/左侧移除黑色边框。
枕头库可能有一个内置的选项/功能/无论它调用什么来做同样的事情......
这里有点晚了,用带有黑色边框的 3X3 RGB tiff 图像测试代码..让我知道它是否有效
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。