【发布时间】:2020-07-19 11:03:53
【问题描述】:
我刚刚有四个星期的 Python 经验。使用 Tkinter 创建一个工具,将新的公司徽标粘贴到现有图像之上。
下面的方法是,获取给定目录中的所有图像并将新徽标粘贴到初始级别。现有图像、编辑图像、x 位置、y 位置、图像预览和少量数据存储在全局实例 self.images_all_arr 中。
def get_img_copy(self):
self.images_all_arr = []
existing_img_fldr = self.input_frame.input_frame_data['existing_img_folder']
for file in os.listdir(existing_img_fldr):
img_old = Image.open(os.path.join(existing_img_fldr, file))
img_new_copy = img_old.copy()
self.pasteImage(img_new_copy, initpaste=True) #process to paste new logo.
view_new_img = ImageTk.PhotoImage(img_new_copy)
fname, fext = file.split('.')
formObj = {
"fname": fname,
"fext": fext,
"img_old": img_old,
"img_new": img_new_copy,
"img_new_view": view_new_img,
"add_logo": 1,
"is_default": 1,
"is_opacityImg": 0,
"pos_x": self.defult_x.get(),
"pos_y": self.defult_y.get()
}
self.images_all_arr.append(formObj)
在 Tkinter 屏幕中预览每个图像后,根据需要对位置 x 和 y 进行一些调整(更新列表 self.images_all_arr 中的 pos_x 和 pos_y)。
好吧,一旦完成。需要保存编辑后的图像。下面的方法保存图像,迭代列表 self.images_all_arr 并调用 save 方法为 img['img_new'].save(dir_output) 因为 img['img_new '] 已更新图像。
def generate_imgae(self):
if len(self.images_all_arr):
dir_output = 'xxxxx'
for img in self.images_all_arr:
print(img['img_new'])
img['img_new'].save(dir_output)
print('completed..')
但它返回以下错误,
Tkinter 回调异常 回溯(最近一次通话最后): 文件“C:\Program Files (x86)\Python38-32\lib\site-packages\PIL\Image.py”,第 2138 行,保存 格式=扩展[分机] 键错误:'' 上述异常是以下异常的直接原因: 回溯(最近一次通话最后): 调用中的文件“C:\Program Files (x86)\Python38-32\lib\tkinter_init_.py”,第 1883 行 返回 self.func(*args) 文件“C:\Users\662828\WORKSPACE\AAA_python_projects\AMI_automation_poc2\position_and_images.py”,第 241 行,在 generate_imgae img['img_new'].save(dir_output) 文件“C:\Program Files (x86)\Python38-32\lib\site-packages\PIL\Image.py”,第 2140 行,保存 raise ValueError("unknown file extension: {}".format(ext)) from e ValueError:未知文件扩展名:
【问题讨论】:
标签: python-3.x python-imaging-library os.path