【我的小工具】图片拼接工具

图片拼接工具

点击此链接查看工具说明

主程序源码如下

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)
        # print(fs)
        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))
        # print(self.pic_list)
        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)
            # process.show()
    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)
        # print(fs)
        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))
        # print(vs.shape)
        final = Image.new("RGBA", (vs.shape[1], vs.shape[0]), color=(0, 0, 250))
        # print(final.size)
        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)
        # final.show()

    def array_merge_hs(self):
        vs = np.hstack(tuple(self.pic_array))
        # print(vs.shape)
        final = Image.new("RGBA", (vs.shape[1], vs.shape[0]), color=(0, 0, 250))
        # print(final.size)
        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)
        # final.show()

    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):
            # print(index, item[0][:])
            if sum(item[0][:]) == 255 or np.mean(item[0][:]) == 255:
                list1.append(index)
                # print(index)
        new_merged_img_array = np.delete(merged_img_array, list1, axis=0)
        # print(new_merged_img_array)
        # # Image.new('RGB', img.size)
        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):
            # print(index, item[0][:])
            if sum(item[0][:]) == 255 or np.mean(item[0][:]) == 255:
                list1.append(index)
                # print(index)
        new_merged_img_array = np.delete(merged_img_array, list1, axis=0)
        # print(new_merged_img_array)
        # # Image.new('RGB', img.size)
        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_vs()
    m.array_merge_hs()
    m.remove_hs_pixels()
    # m.remove_vs_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:  # 获取变量的值与单选按钮的value来对比
            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()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值