使用 python,tkinter 合并条件小于 2450 像素的图像

我做了一个合并图像的程序,我想升级这个程序,使图像的总高度应小于 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('알림', '작업이 완료되었습니다.') 

在这个编码中,list_file 是 Listbox 并且有几个相同宽度但不同高度的图像。我想合并这张图片,但合并后的图片高度不能超过 2450 像素。


例如,list_file 是 a0、a1、a2、a3、a4、a5、a6。list_file 中图片的高度为 200, 1500, 2400, 100, 300, 500, 1600。那么 list_file 应该是 a0 <-(a0+a1), a1<-(a2), a2<-(a3, a4, a5) , a3<-(a6) [1700, 2400, 900, 1600]


我想获取条目值并保存 list_file 的每个图像。例如输入值为desk,文件名应为desk 001.jpg、desk 002.jpg、desk 003.jpg、desk 004.jpg等。


蝴蝶不菲
浏览 122回答 1
1回答

慕桂英546537

您可以使用Image.new(...)创建合并图像,然后使用Image.paste(...)将所需图像复制到合并图像中:def merge_images(imagelist, width, height, seqno):&nbsp; &nbsp; if imagelist:&nbsp; &nbsp; &nbsp; &nbsp; # create the merged image&nbsp; &nbsp; &nbsp; &nbsp; merged_image = Image.new('RGB', (width, height), (0,0,0))&nbsp; &nbsp; &nbsp; &nbsp; y = 0&nbsp; &nbsp; &nbsp; &nbsp; for image in imagelist:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; merged_image.paste(image, (0, y))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y += image.height&nbsp; &nbsp; &nbsp; &nbsp; # save the merged image&nbsp; &nbsp; &nbsp; &nbsp; dest_path = os.path.join(txt_dest_path.get(), 'desk%03d.jpg' % seqno)&nbsp; &nbsp; &nbsp; &nbsp; merged_image.save(dest_path)&nbsp; &nbsp; &nbsp; &nbsp; seqno += 1&nbsp; &nbsp; return seqnodef merge():&nbsp; &nbsp; MAX_HEIGHT = 2450&nbsp; &nbsp; merge_list = []&nbsp; &nbsp; height = 0&nbsp; &nbsp; width = None&nbsp; &nbsp; seqno = 1&nbsp; &nbsp; for file in list_file.get(0, 'end'):&nbsp; &nbsp; &nbsp; &nbsp; image = Image.open(file)&nbsp; &nbsp; &nbsp; &nbsp; if width is None:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width = image.width&nbsp; &nbsp; &nbsp; &nbsp; if height+image.height <= MAX_HEIGHT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; merge_list.append(image)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height += image.height&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; seqno = merge_images(merge_list, width, height, seqno)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; merge_list = [image]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height = image.height&nbsp; &nbsp; merge_images(merge_list, width, height, seqno)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python