Python image save error - raise ValueError("unknown file extension: {}".format(ext)) from e ValueError: unknown file extension:
Python image save error - raise ValueError("unknown file extension: {}".format(ext)) from e ValueError: unknown file extension:
我在 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[=28 中的 pos_x 和 pos_y =]) 视需要而定。
好吧,一旦完成。需要保存编辑好的图像。下面的方法保存图像,迭代列表 self.images_all_arr 并调用保存方法 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..')
但是returns下面的错误,
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files (x86)\Python38-32\lib\site-packages\PIL\Image.py", line 2138, in save
format = EXTENSION[ext]
KeyError: ''
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python38-32\lib\tkinter_init_.py", line 1883, in call
return self.func(*args)
File "C:\Users2828\WORKSPACE\AAA_python_projects\AMI_automation_poc2\position_and_images.py", line 241, in generate_imgae
img['img_new'].save(dir_output)
File "C:\Program Files (x86)\Python38-32\lib\site-packages\PIL\Image.py", line 2140, in save
raise ValueError("unknown file extension: {}".format(ext)) from e
ValueError: unknown file extension:
dir_output
不包含文件扩展名,它只是 xxxxx
。您需要指定所需的图像文件格式。该错误通过“未知文件格式”告诉我们这一点。
基本上,您需要在文件名中包含扩展名,或者将其作为 image.save
中的下一个参数传递。您可以查看文档 here
例如
image.save('image.png')
或
image.save('image', 'png')
下面的代码解决了我的问题。通过将图像的确切目录、文件名和扩展名作为参数提供给方法 image.save()。 opfile 的结果是,C:\Users\WORKSPACE\AAA_python_projects\test\valid.png.
def generate_imgae(self):
if len(self.images_all_arr):
dir_output = r"C:\Users\WORKSPACE\AAA_python_projects\test"
if not os.path.isdir(dir_output):
os.mkdir(dir_output)
for img in self.images_all_arr:
opfile = os.path.join(dir_output, '{}.{}'.format(img['fname'], img['fext'] ))
img['img_new'].save(opfile)
print('completed..')
谢谢@dantechguy
我在 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[=28 中的 pos_x 和 pos_y =]) 视需要而定。
好吧,一旦完成。需要保存编辑好的图像。下面的方法保存图像,迭代列表 self.images_all_arr 并调用保存方法 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..')
但是returns下面的错误,
Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files (x86)\Python38-32\lib\site-packages\PIL\Image.py", line 2138, in save format = EXTENSION[ext] KeyError: '' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Program Files (x86)\Python38-32\lib\tkinter_init_.py", line 1883, in call return self.func(*args) File "C:\Users2828\WORKSPACE\AAA_python_projects\AMI_automation_poc2\position_and_images.py", line 241, in generate_imgae img['img_new'].save(dir_output) File "C:\Program Files (x86)\Python38-32\lib\site-packages\PIL\Image.py", line 2140, in save raise ValueError("unknown file extension: {}".format(ext)) from e ValueError: unknown file extension:
dir_output
不包含文件扩展名,它只是 xxxxx
。您需要指定所需的图像文件格式。该错误通过“未知文件格式”告诉我们这一点。
基本上,您需要在文件名中包含扩展名,或者将其作为 image.save
中的下一个参数传递。您可以查看文档 here
例如
image.save('image.png')
或
image.save('image', 'png')
下面的代码解决了我的问题。通过将图像的确切目录、文件名和扩展名作为参数提供给方法 image.save()。 opfile 的结果是,C:\Users\WORKSPACE\AAA_python_projects\test\valid.png.
def generate_imgae(self):
if len(self.images_all_arr):
dir_output = r"C:\Users\WORKSPACE\AAA_python_projects\test"
if not os.path.isdir(dir_output):
os.mkdir(dir_output)
for img in self.images_all_arr:
opfile = os.path.join(dir_output, '{}.{}'.format(img['fname'], img['fext'] ))
img['img_new'].save(opfile)
print('completed..')
谢谢@dantechguy