图片拼接工具
点击此链接查看工具说明
主程序源码如下
import os
import shutil
from PIL import Image
import numpy as np
class MergePic(object):
def __init__(self, pic_root_path):
self.pic_root_path = pic_root_path
self.pic_list = []
self.pic_array = []
self.pic_object_list = []
self.pic_array_shape0 = []
self.pic_array_shape1 = []
if not(os.path.exists(os.path.join(self.pic_root_path, 'copyfile'))):
os.mkdir(os.path.join(self.pic_root_path, 'copyfile'))
if not(os.path.exists(os.path.join(self.pic_root_path, 'result'))):
os.mkdir(os.path.join(self.pic_root_path, 'result'))
def get_pictures_list(self):
fs = os.listdir(self.pic_root_path)
for img in fs:
if os.path.isfile(os.path.join(self.pic_root_path, img)):
self.pic_list.append(os.path.join(self.pic_root_path, img))
shutil.copy(os.path.join(self.pic_root_path, img),
os.path.join(os.path.join(self.pic_root_path, 'copyfile'), img))
return self.pic_list
def get_pictures_array(self):
for i in self.pic_list:
im = Image.open(os.path.join(self.pic_root_path, i))
self.pic_object_list.append(im)
self.images_array = np.array(im)
self.pic_array_shape0.append(self.images_array.shape[0])
self.pic_array_shape1.append(self.images_array.shape[1])
self.pic_array.append(self.images_array)
return self.pic_array, self.pic_object_list, self.images_array, self.pic_array_shape0, self.pic_array_shape1
def reshape_pic(self):
for n in range(len(self.pic_object_list)):
process = Image.new('RGBA', (max(self.pic_array_shape1), max(self.pic_array_shape0)), color=(0, 0, 0))
process.paste(self.pic_object_list[n], (0, 0))
process.save('%s' % self.pic_list[n], 'PNG', optimize=False)
def reshape_list(self):
self.pic_list.clear()
self.pic_object_list.clear()
self.pic_array_shape0.clear()
self.pic_array_shape1.clear()
self.pic_array.clear()
fs = os.listdir(self.pic_root_path)
for img in fs:
if os.path.isfile(os.path.join(self.pic_root_path, img)):
self.pic_list.append(os.path.join(self.pic_root_path, img))
for i in self.pic_list:
im = Image.open(os.path.join(self.pic_root_path, i))
self.pic_object_list.append(im)
self.images_array = np.array(im)
self.pic_array_shape0.append(self.images_array.shape[0])
self.pic_array_shape1.append(self.images_array.shape[1])
self.pic_array.append(self.images_array)
return self.pic_array, self.pic_object_list, self.images_array, self.pic_array_shape0, self.pic_array_shape1
def array_merge_vs(self):
vs = np.vstack(tuple(self.pic_array))
final = Image.new("RGBA", (vs.shape[1], vs.shape[0]), color=(0, 0, 250))
for k in range(len(self.pic_object_list)):
final.paste(self.pic_object_list[k], box=(0, self.images_array.shape[0]*k))
final.save('%s\\result\\合并后的图片_v.png' % self.pic_root_path, 'PNG', optimize=False)
def array_merge_hs(self):
vs = np.hstack(tuple(self.pic_array))
final = Image.new("RGBA", (vs.shape[1], vs.shape[0]), color=(0, 0, 250))
for k in range(len(self.pic_object_list)):
final.paste(self.pic_object_list[k], box=(self.images_array.shape[1] * k, 0))
final.save('%s\\result\\合并后的图片_h.png' % self.pic_root_path, 'PNG', optimize=False)
def remove_vs_pixels(self):
list1 = []
merged_img = Image.open('%s\\result\\合并后的图片_v.png' % self.pic_root_path)
merged_img_array = np.array(merged_img)
for index, item in enumerate(merged_img_array):
if sum(item[0][:]) == 255 or np.mean(item[0][:]) == 255:
list1.append(index)
new_merged_img_array = np.delete(merged_img_array, list1, axis=0)
newImg = Image.fromarray(new_merged_img_array)
newImg.save('%s\\result\\合并后的图片_v.png' % self.pic_root_path, "PNG")
newImg.show()
for i in os.listdir(self.pic_root_path):
if os.path.isfile(os.path.join(self.pic_root_path, i)):
shutil.copy(os.path.join(os.path.join(self.pic_root_path, "copyfile"), i),
os.path.join(self.pic_root_path, i))
shutil.rmtree(os.path.join(os.path.join(self.pic_root_path, "copyfile")))
def remove_hs_pixels(self):
list1 = []
merged_img = Image.open('%s\\result\\合并后的图片_h.png' % self.pic_root_path)
merged_img_rotato = merged_img.transpose(Image.ROTATE_90)
merged_img_array = np.array(merged_img_rotato)
for index, item in enumerate(merged_img_array):
if sum(item[0][:]) == 255 or np.mean(item[0][:]) == 255:
list1.append(index)
new_merged_img_array = np.delete(merged_img_array, list1, axis=0)
newImg = Image.fromarray(new_merged_img_array)
merged_img_rotato2 = newImg.transpose(Image.ROTATE_270)
merged_img_rotato2.save('%s\\result\\合并后的图片_h.png' % self.pic_root_path, "PNG")
merged_img_rotato2.show()
for i in os.listdir(self.pic_root_path):
if os.path.isfile(os.path.join(self.pic_root_path, i)):
shutil.copy(os.path.join(os.path.join(self.pic_root_path, "copyfile"), i),
os.path.join(self.pic_root_path, i))
shutil.rmtree(os.path.join(os.path.join(self.pic_root_path, "copyfile")))
if __name__ == '__main__':
path = r'H:\python_study\图片合并\TTT'
m = MergePic(path)
m.get_pictures_list()
m.get_pictures_array()
m.reshape_pic()
m.reshape_list()
m.array_merge_hs()
m.remove_hs_pixels()
主界面程序源码如下
import webbrowser
from tkinter import *
from main import *
import easygui as g
import tkinter.messagebox
class MyGUI(object):
def __init__(self, init_window_name):
self.init_window_name = init_window_name
def set_init_window(self):
self.init_window_name.title("图片拼接工具")
self.init_window_name.geometry('320x150+700+100')
self.init_window_name.attributes("-alpha", 0.9)
first_button = Button(self.init_window_name, text="选择图片所在文件夹", command=self.select_radiobutton)
first_button.grid(row=2, column=4, rowspan=1, columnspan=3, sticky=E, pady=20, padx=60)
label = Label(self.init_window_name, text="拼接方式:")
label.grid(row=2, column=0, columnspan=2, sticky=W)
self.v = tkinter.IntVar()
radio_button1 = Radiobutton(self.init_window_name, text="垂直拼接", fg='blue', variable=self.v, value=1)
radio_button1.grid(row=2, column=2, sticky=W)
radio_button2 = Radiobutton(self.init_window_name, text="水平拼接", fg='blue', variable=self.v, value=0)
radio_button2.grid(row=3, column=2, sticky=W)
second_button = Button(self.init_window_name, text="查看使用说明",command=self.open_help_web)
second_button.grid(row=3, column=4, rowspan=1, columnspan=3, sticky=W,pady=10, padx=60, ipadx=18)
def select_radiobutton(self):
if self.v.get() == 0:
print('水平拼接被选中')
path = g.diropenbox()
m = MergePic(path)
m.get_pictures_list()
m.get_pictures_array()
m.reshape_pic()
m.reshape_list()
m.array_merge_hs()
m.remove_hs_pixels()
elif self.v.get() == 1:
print('垂直拼接被选中')
path = g.diropenbox()
m = MergePic(path)
m.get_pictures_list()
m.get_pictures_array()
m.reshape_pic()
m.reshape_list()
m.array_merge_vs()
m.remove_vs_pixels()
def open_help_web(self):
webbrowser.open_new_tab("http://124.70.7.51/source/tool.html")
if __name__ == '__main__':
w = Tk()
window = MyGUI(w)
window.set_init_window()
w.mainloop()