【问题标题】:Python image save error - raise ValueError("unknown file extension: {}".format(ext)) from e ValueError: unknown file extension:Python 图像保存错误 - 从 e ValueError 引发 ValueError("unknown file extension: {}".format(ext)): unknown file extension:
【发布时间】: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


    【解决方案1】:

    dir_output 不包含文件扩展名,它只是xxxxx。您需要指定所需的图像文件格式。错误通过说“未知文件格式”告诉我们这一点。

    基本上,您要么需要在文件名中包含扩展名,要么将其作为image.save 中的下一个参数传递。您可以查看文档here

    例如。

    image.save('image.png')

    image.save('image', 'png')

    【讨论】:

      【解决方案2】:

      下面的代码解决了我的问题。通过将图像的确切目录、文件名和扩展名作为参数提供给 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

      【讨论】:

        猜你喜欢
        • 2022-10-21
        • 2020-08-15
        • 2019-03-21
        • 1970-01-01
        • 1970-01-01
        • 2020-09-17
        • 1970-01-01
        • 2022-07-23
        • 2018-01-26
        相关资源
        最近更新 更多