在 python 中将图像拼接在一起
stitch images together in python
我正在尝试将大约 50 张图像(均采用相同的 287x287 尺寸)拼接在一起。具体来说,顶行应该有25张图片,底行应该有25张图片,每两张图片之间也有一个小距离。
我在尝试过程中遇到了两个困难:
第一个问题是一个文件夹中有 25 张图像,名称为 'prefix-70',...,'prefix-94'
,而其他 25 张图像位于另一个名称相同的文件夹中 'prefix-70',...,'prefix-94'
。不知道怎么让他们在Python中不冲突。
第二个问题是我写了下面的代码来读取一个文件夹图像形成一行但它输出一列。
#!/usr/bin/python3.0
#encoding=utf-8
import numpy as np
from PIL import Image
import glob,os
if __name__=='__main__':
#prefix=input('Input the prefix of images:')
prefix = 'prefix'
files=glob.glob(prefix+'-*')
num=len(files)
filename_lens=[len(x) for x in files] #length of the files
min_len=min(filename_lens) #minimal length of filenames
max_len=max(filename_lens) #maximal length of filenames
if min_len==max_len:#the last number of each filename has the same length
files=sorted(files) #sort the files in ascending order
else:
index=[0 for x in range(num)]
for i in range(num):
filename=files[i]
start=filename.rfind('-')+1
end=filename.rfind('.')
file_no=int(filename[start:end])
index[i]=file_no
index=sorted(index)
files=[prefix+'-'+str(x)+'.png' for x in index]
print(files[0])
baseimg=Image.open(files[0])
sz=baseimg.size
basemat=np.atleast_2d(baseimg)
for i in range(1,num):
file=files[i]
im=Image.open(file)
im=im.resize(sz,Image.ANTIALIAS)
mat=np.atleast_2d(im)
print(file)
basemat=np.append(basemat,mat,axis=0)
final_img=Image.fromarray(basemat)
final_img.save('merged.png')
我想我走错路了...
我怎样才能正确缝合它们?任何建议表示赞赏。
安装ImageMagick,然后告诉它你的两个目录在哪里。
#!/usr/bin/python3
##=========================================================
## required ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##
## imagemagick.org/script/download.php
##
##=========================================================
## libs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import subprocess as sp
##=========================================================
## vars ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
offset = 2 ## pixel gap between images
color = '#000000' ## background color to fill gaps
dir1 = '/home/me/Pictures/topRow/'
dir2 = '/home/me/Pictures/bottomRow/'
## note: windows dirs use double backslashes
## 'C:\Users\me\Pictures\topRow\'
##=========================================================
## script ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
row1args = ['convert', '+smush', offset, '-background', color, dir1 + '*.png', 'row1.png']
row2args = ['convert', '+smush', offset, '-background', color, dir2 + '*.png', 'row2.png']
merge = ['convert', '-smush', offset, '-background', color, 'row*.png', 'merged.png']
##=========================================================
## main ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sp .call(row1args)
sp .call(row2args)
sp .call(merge)
##=========================================================
## eof ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
试试这个(评论中的解释):
from PIL import Image
from os import listdir, path
space_between_row = 10
new_image_path = 'result.jpg'
im_dirs = ['images/1', 'images/2']
# get sorted list of images
im_path_list = [[path.join(p, f) for f in sorted(listdir(p))] for p in im_dirs]
# open images and calculate total widths and heights
im_list = []
total_width = 0
total_height = 0
for path_list in im_path_list:
images = list(map(Image.open, path_list))
widths, heights = zip(*(i.size for i in images))
total_width = max(total_width, sum(widths))
total_height += max(heights)
im_list.append(images)
# concat images
new_im = Image.new('RGB', (total_width, total_height))
y_offset = 0
for images in im_list:
x_offset = 0
max_height = 0
for im in images:
new_im.paste(im, (x_offset, y_offset))
x_offset += im.size[0]
max_height = max(im.size[1], max_height)
y_offset = y_offset + max_height + space_between_row
# show and save
new_im.show()
new_im.save(new_image_path)
我正在尝试将大约 50 张图像(均采用相同的 287x287 尺寸)拼接在一起。具体来说,顶行应该有25张图片,底行应该有25张图片,每两张图片之间也有一个小距离。
我在尝试过程中遇到了两个困难:
第一个问题是一个文件夹中有 25 张图像,名称为 'prefix-70',...,'prefix-94'
,而其他 25 张图像位于另一个名称相同的文件夹中 'prefix-70',...,'prefix-94'
。不知道怎么让他们在Python中不冲突。
第二个问题是我写了下面的代码来读取一个文件夹图像形成一行但它输出一列。
#!/usr/bin/python3.0
#encoding=utf-8
import numpy as np
from PIL import Image
import glob,os
if __name__=='__main__':
#prefix=input('Input the prefix of images:')
prefix = 'prefix'
files=glob.glob(prefix+'-*')
num=len(files)
filename_lens=[len(x) for x in files] #length of the files
min_len=min(filename_lens) #minimal length of filenames
max_len=max(filename_lens) #maximal length of filenames
if min_len==max_len:#the last number of each filename has the same length
files=sorted(files) #sort the files in ascending order
else:
index=[0 for x in range(num)]
for i in range(num):
filename=files[i]
start=filename.rfind('-')+1
end=filename.rfind('.')
file_no=int(filename[start:end])
index[i]=file_no
index=sorted(index)
files=[prefix+'-'+str(x)+'.png' for x in index]
print(files[0])
baseimg=Image.open(files[0])
sz=baseimg.size
basemat=np.atleast_2d(baseimg)
for i in range(1,num):
file=files[i]
im=Image.open(file)
im=im.resize(sz,Image.ANTIALIAS)
mat=np.atleast_2d(im)
print(file)
basemat=np.append(basemat,mat,axis=0)
final_img=Image.fromarray(basemat)
final_img.save('merged.png')
我想我走错路了... 我怎样才能正确缝合它们?任何建议表示赞赏。
安装ImageMagick,然后告诉它你的两个目录在哪里。
#!/usr/bin/python3
##=========================================================
## required ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##
## imagemagick.org/script/download.php
##
##=========================================================
## libs ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import subprocess as sp
##=========================================================
## vars ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
offset = 2 ## pixel gap between images
color = '#000000' ## background color to fill gaps
dir1 = '/home/me/Pictures/topRow/'
dir2 = '/home/me/Pictures/bottomRow/'
## note: windows dirs use double backslashes
## 'C:\Users\me\Pictures\topRow\'
##=========================================================
## script ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
row1args = ['convert', '+smush', offset, '-background', color, dir1 + '*.png', 'row1.png']
row2args = ['convert', '+smush', offset, '-background', color, dir2 + '*.png', 'row2.png']
merge = ['convert', '-smush', offset, '-background', color, 'row*.png', 'merged.png']
##=========================================================
## main ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sp .call(row1args)
sp .call(row2args)
sp .call(merge)
##=========================================================
## eof ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
试试这个(评论中的解释):
from PIL import Image
from os import listdir, path
space_between_row = 10
new_image_path = 'result.jpg'
im_dirs = ['images/1', 'images/2']
# get sorted list of images
im_path_list = [[path.join(p, f) for f in sorted(listdir(p))] for p in im_dirs]
# open images and calculate total widths and heights
im_list = []
total_width = 0
total_height = 0
for path_list in im_path_list:
images = list(map(Image.open, path_list))
widths, heights = zip(*(i.size for i in images))
total_width = max(total_width, sum(widths))
total_height += max(heights)
im_list.append(images)
# concat images
new_im = Image.new('RGB', (total_width, total_height))
y_offset = 0
for images in im_list:
x_offset = 0
max_height = 0
for im in images:
new_im.paste(im, (x_offset, y_offset))
x_offset += im.size[0]
max_height = max(im.size[1], max_height)
y_offset = y_offset + max_height + space_between_row
# show and save
new_im.show()
new_im.save(new_image_path)