图像拼接的改造
按横向、竖向的指定高宽,横向、竖向指定个数,修正不能凑成一整张的问题,保存成多张图的可能
原帖
https://blog.csdn.net/ahaotata/article/details/84027000
import PIL.Image as Image #pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pillow
import os
IMAGES_PATH = 'E:\\下载\\idm\\img\\' # 图片集地址
IMAGES_FORMAT = ['.jpg', '.JPG'] # 图片格式,也可以填加其他格式
IMAGE_VSIZE = 118 # 每张小图片的大小
IMAGE_HSIZE = 256 # 每张小图片的大小
IMAGE_ROW = 4 # 图片间隔,也就是合并成一张图后,一共有几行
IMAGE_COLUMN = 4 # 图片间隔,也就是合并成一张图后,一共有几列
IMAGE_SAVE_PATH = 'E:\\下载\\idm\\img\\final' # 图片转换后的地址
start=1
# 获取图片集地址下的所有图片名称
image_names = [name for name in os.listdir(IMAGES_PATH) for item in IMAGES_FORMAT if
os.path.splitext(name)[1] == item]
print(image_names)
# 简单的对于参数的设定和实际图片集的大小进行数量判断
#if len(image_names) != IMAGE_ROW * IMAGE_COLUMN:
# raise ValueError("合成图片的参数和要求的数量不能匹配!")
# 定义图像拼接函数
def image_compose(start):
while int(start)*IMAGE_COLUMN*IMAGE_ROW<=len(image_names):
to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_HSIZE, IMAGE_ROW * IMAGE_VSIZE)) #创建一个新图
# 循环遍历,把每张图片按顺序粘贴到对应位置上
for y in range(1, IMAGE_ROW + 1):
for x in range(1, IMAGE_COLUMN + 1):
try:
from_image = Image.open(IMAGES_PATH + image_names[(start-1)*IMAGE_COLUMN*IMAGE_ROW+IMAGE_COLUMN * (y - 1) + x - 1]).resize((IMAGE_HSIZE, IMAGE_VSIZE),Image.ANTIALIAS)
to_image.paste(from_image, ((x - 1) * IMAGE_HSIZE, (y - 1) * IMAGE_VSIZE))
except Exception as e:
pass
raise e
to_image.save(IMAGE_SAVE_PATH+str(start)+'.jpg')
start+=1
return # 保存新图
image_compose(start) #调用函数
发现个别的图片顺序不正常
[‘张10.JPG’, ‘张11.JPG’, ‘张12.JPG’, ‘张13.JPG’, ‘张5.JPG’, ‘张6.JPG’, ‘张7.JPG’, ‘张8.JPG’, ‘张9.JPG’, ‘张一.JPG’, ‘张三.JPG’, ‘张二.JPG’, ‘张四.JPG’]
并且只支持jpg格式,再进行改造,把所有图片格式加进去,参照WORD中支持的图片格式加入的,不要问为啥。
[‘Afinal3.bmp’, ‘final1.jpg’, ‘final2.jpg’, ‘final3.jpg’, ‘final4.jpg’, ‘张5.JPG’, ‘张6.JPG’, ‘张7.JPG’, ‘张8.JPG’, ‘张9.JPG’, ‘张10.JPG’, ‘张11.JPG’, ‘张12.JPG’, ‘张13.JPG’, ‘张一.JPG’, ‘张三.JPG’, ‘张二.JPG’, ‘张四.JPG’]
这样就包进去了
import PIL.Image as Image #pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pillow
import os
from natsort import natsorted #自然排序
IMAGES_PATH = 'd:\\1\\1\\' # 图片集地址
#IMAGES_FORMAT = ['.emf|.wmf|.jpg|.jpeg|.jfif|.jpe|.png|.bmp|.dib|.rle|.gif|.emz|.wmz|.tiff|.tiff|.svg|.ico'] # 图片格式,也可以填加其他格式
IMAGES_FORMAT = '.emf|.wmf|.jpg|.jpeg|.jfif|.jpe|.png|.bmp|.dib|.rle|.gif|.emz|.wmz|.tiff|.tiff|.svg|.ico'
IMAGE_VSIZE = 236 # 每张小图片的大小
IMAGE_HSIZE = 512 # 每张小图片的大小
IMAGE_ROW = 2 # 图片间隔,也就是合并成一张图后,一共有几行
IMAGE_COLUMN = 2 # 图片间隔,也就是合并成一张图后,一共有几列
IMAGE_SAVE_PATH = 'd:\\1\\1\\final' # 图片转换后的地址
start=1
# 获取图片集地址下的所有图片名称
#image_names = [name for name in os.listdir(IMAGES_PATH) for item in IMAGES_FORMAT if
# os.path.splitext(name)[1] == item]
image_names = [name for name in os.listdir(IMAGES_PATH) if os.path.splitext(name)[1].lower() in IMAGES_FORMAT]
image_names=natsorted(image_names)
print(image_names)
# 简单的对于参数的设定和实际图片集的大小进行数量判断
#if len(image_names) != IMAGE_ROW * IMAGE_COLUMN:
# raise ValueError("合成图片的参数和要求的数量不能匹配!")
# 定义图像拼接函数
def image_compose(start):
while int(start)*IMAGE_COLUMN*IMAGE_ROW<=len(image_names):
to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_HSIZE, IMAGE_ROW * IMAGE_VSIZE)) #创建一个新图
# 循环遍历,把每张图片按顺序粘贴到对应位置上
for y in range(1, IMAGE_ROW + 1):
for x in range(1, IMAGE_COLUMN + 1):
try:
from_image = Image.open(IMAGES_PATH + image_names[(start-1)*IMAGE_COLUMN*IMAGE_ROW+IMAGE_COLUMN * (y - 1) + x - 1]).resize((IMAGE_HSIZE, IMAGE_VSIZE),Image.ANTIALIAS)
to_image.paste(from_image, ((x - 1) * IMAGE_HSIZE, (y - 1) * IMAGE_VSIZE))
except Exception as e:
pass
raise e
to_image.save(IMAGE_SAVE_PATH+str(start)+'.jpg')
start+=1
return # 保存新图
image_compose(start) #调用函数