要使用python合并高度小于2450像素的图像,请使用tkinter

2024-03-29 15:37:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个程序来合并图像,我想升级这个程序,使图像的总高度应小于2450像素,并保存每个合并的图像

import os
import tkinter.ttk as ttk
import  tkinter.messagebox as msgbox
from tkinter import *    # __all__
from tkinter import filedialog
from PIL import  Image

# add file
def add_file():
files = filedialog.askopenfilenames(title="이미지 파일을 선택하세요", \
    filetypes =(('jpg 파일', '*.jpg'), ('모든 파일', '*.*')), \
    initialdir='D:/jh/사업/프로그램/파이썬 이미지 합치기 프로그램/image')
    
# file list that user can select
for file in files:
    list_file.insert(END, file)



# file frame(file add, selected file delete)
file_frame = Frame(root)
file_frame.pack(fill='x', padx=5, pady=5)

btn_add_file = Button(file_frame, padx=5, pady=5, width=12, text='파일추가', command=add_file)
btn_add_file.pack(side='left')

btn_del_file = Button(file_frame, padx=5, pady=5, width=12, text='선택삭제', command=del_file)
btn_del_file.pack(side='right')

# list frame
list_frame = Frame(root)
list_frame.pack(fill='both', padx=5, pady=5)

scrollbar = Scrollbar(list_frame)
scrollbar.pack(side='right', fill='y')

list_file = Listbox(list_frame, selectmode='extended', height=12, yscrollcommand=scrollbar.set)
list_file.pack(side='left', fill='both', expand=True)
scrollbar.config(command=list_file.yview)

.....

 dest_path = os.path.join(txt_dest_path.get(), txt_file.get())   
 # txt_file get values after to input entry for file name. ex) desk 
 result_img.save(dest_path)   #save result_img to dest_path
 msgbox.showinfo('알림', '작업이 완료되었습니다.') 

在这种编码中,列表文件是Listbox,有几个宽度相同但高度不同的图像。我想合并此图像,但合并图像的高度不应超过2450像素

例如,列表_文件是a0、a1、a2、a3、a4、a5、a6。列表_文件中的图像高度为200、1500、2400、100、300、500、1600。然后列出文件应为a0<-(a0+a1),a1<-(a2)、a2<-(a3、a4、a5)、a3<-(a6) [170024009001600]

我想得到输入值并保存列表文件的每个图像。例如,输入值为desk,文件名应为desk 001.jpg、desk 002.jpg、desk 003.jpg、desk 004.jpg等


Tags: 文件path图像importadd高度tkinterfill
1条回答
网友
1楼 · 发布于 2024-03-29 15:37:44

您可以使用Image.new(...)创建合并图像,然后使用Image.paste(...)将所需图像复制到合并图像中:

def merge_images(imagelist, width, height, seqno):
    if imagelist:
        # create the merged image
        merged_image = Image.new('RGB', (width, height), (0,0,0))
        y = 0
        for image in imagelist:
            merged_image.paste(image, (0, y))
            y += image.height
        # save the merged image
        dest_path = os.path.join(txt_dest_path.get(), 'desk%03d.jpg' % seqno)
        merged_image.save(dest_path)
        seqno += 1
    return seqno

def merge():
    MAX_HEIGHT = 2450
    merge_list = []
    height = 0
    width = None
    seqno = 1
    for file in list_file.get(0, 'end'):
        image = Image.open(file)
        if width is None:
            width = image.width
        if height+image.height <= MAX_HEIGHT:
            merge_list.append(image)
            height += image.height
        else:
            seqno = merge_images(merge_list, width, height, seqno)
            merge_list = [image]
            height = image.height

    merge_images(merge_list, width, height, seqno)

相关问题 更多 >