如何使用Python PIL(枕头)将图像保存在特定的文件目录中,而不会由于以下原因而导致KeyError:save_handler = SAVE [format.upper()] [英] How to save images in specific file directories using Python PIL (Pillow) without getting a KeyError due to: save_handler = SAVE[format.upper()]

查看:1452
本文介绍了如何使用Python PIL(枕头)将图像保存在特定的文件目录中,而不会由于以下原因而导致KeyError:save_handler = SAVE [format.upper()]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从元素周期表的较大图像中裁剪特定元素,然后将它们保存在特定的文件目录中,此文件目录位于其他文件夹中,并且此文件夹与程序位于相同的文件目录中我正试图做到这一点.

我看了另一个关于堆栈溢出的已回答问题,该问题与我的问题相似: 如何使用PIL保存图像? ,但是该用户使用了'numpy'.我以前只是在学校里学习过python基础知识,现在正在利用我的空闲时间来学习"tkinter",现在又是"PIL"(枕头),对于这些python模块我是陌生的,并且我正努力抓住这两个令人困惑的文档,还不知道"numpy"是什么或如何使用.

这是我要运行的代码:

#Saving G1 elements as their own image


from PIL import Image


Periodic_Table = Image.open("Periodic Table Bitmap.bmp")
G1_List = ["Hydrogen.bmp","Lithium.bmp","Sodium.bmp",
           "Potassium.bmp","Rubidium.bmp","Caesium.bmp","Francium.bmp"]

starting_coords = (180,86,340,271)
for i in range(7):
    y1 = 86 + (i * 187)
    y2 = 86 + ((i+1)* 187) - 3
    cropped_region = (180,y1,340,y2)
    G1_Element = Periodic_Table.crop(cropped_region)
    G1_Name = G1_List[i]
    G1_Element.save(
        "C:\\Users\\Kids\\Documents\\Robert\\Python Programming\\Periodic Table Quiz\\PIL Programs and Images\\Group 1 Elements"
        , G1_Name)

我还尝试运行相同的代码,其中G1_List中的项目没有扩展名'.bmp',但是图像名称的格式设置如下:

#Saving G1 elements as their own image

from PIL import Image

Periodic_Table = Image.open("Periodic Table Bitmap.bmp")
G1_List = ["Hydrogen","Lithium","Sodium","Potassium","Rubidium","Caesium","Francium"]

starting_coords = (180,86,340,271)
for i in range(7):
    y1 = 86 + (i * 187)
    y2 = 86 + ((i+1)* 187) - 3
    cropped_region = (180,y1,340,y2)
    G1_Element = Periodic_Table.crop(cropped_region)
    G1_Name = G1_List[i]
    G1_Name_Formatted = ("%s" % (G1_Name)) + ".bmp"
    G1_Element.save(
        "C:\\Users\\Kids\\Documents\\Robert\\Python Programming\\Periodic Table Quiz\\PIL Programs and Images\\Group 1 Elements"
        , G1_Name_Formatted)

在两种情况下,我都收到此错误消息:

save_handler = SAVE[format.upper()]
KeyError: 'HYDROGEN.BMP'

在我将链接粘贴到之前的文章中,建议删除."来自'.bmp'的扩展名,因此可以识别为大写形式的扩展名,但这也不起作用.

任何解决方案将不胜感激,最好不使用诸如numpy之类的附加模块,但是,如果必须使用其中任何一个,我将不熟悉它们,并且需要答案中的代码向我解释如果我理解的话,完全可以.

注意:我使用位图图像是因为我在某些python文档中了解到,我计划与PIL(枕头)一起使用的tkinter仅与位图图像兼容:解决方案

您要将文件名作为第二个参数传递给Image.save.

但是,第二个参数是(可选)文件格式-如果指定,则它必须与注册的文件格式匹配,例如GIFBMPPNG,...

您可能想要做的是将路径和图像名称连接起来-无需指定格式.

import os

...

# dir_path can be set outside of your loop
dir_path = "C:\\Users\\Kids\\Documents\\Robert\\Python Programming\\Periodic Table Quiz\\PIL Programs and Images\\Group 1 Elements"
...
G1_Name_Formatted = ("%s" % (G1_Name)) + ".bmp"
file_path = os.path.join( dir_path, G1_Name_Formatted  ) 
G1_Element.save( file_path )

或者如果您要明确指定格式,请将最后一部分更改为:

G1_Element.save( file_path, "BMP" )

I am trying to crop specific elements out of a larger image of the periodic table, then saving them in a specific file directory, this file directory is inside an additional folder, and this folder is in the same file directory as the program that I am trying to do this with.

I have looked at another answered question on stack overflow that has similarities to my problem: How can I save an image with PIL? , however this user used 'numpy'. I have only previously learnt python basics in school and am using my free time to learn 'tkinter' and now 'PIL' (Pillow), I am new to these modules for python and am struggling to grasp the confusing documentation for both, and I also don't know what 'numpy' is or how to use it.

This the code I am trying to run:

#Saving G1 elements as their own image


from PIL import Image


Periodic_Table = Image.open("Periodic Table Bitmap.bmp")
G1_List = ["Hydrogen.bmp","Lithium.bmp","Sodium.bmp",
           "Potassium.bmp","Rubidium.bmp","Caesium.bmp","Francium.bmp"]

starting_coords = (180,86,340,271)
for i in range(7):
    y1 = 86 + (i * 187)
    y2 = 86 + ((i+1)* 187) - 3
    cropped_region = (180,y1,340,y2)
    G1_Element = Periodic_Table.crop(cropped_region)
    G1_Name = G1_List[i]
    G1_Element.save(
        "C:\\Users\\Kids\\Documents\\Robert\\Python Programming\\Periodic Table Quiz\\PIL Programs and Images\\Group 1 Elements"
        , G1_Name)

I have also tried running the same code where the items in the G1_List don't have '.bmp' extensions but the image names are formatted as so:

#Saving G1 elements as their own image

from PIL import Image

Periodic_Table = Image.open("Periodic Table Bitmap.bmp")
G1_List = ["Hydrogen","Lithium","Sodium","Potassium","Rubidium","Caesium","Francium"]

starting_coords = (180,86,340,271)
for i in range(7):
    y1 = 86 + (i * 187)
    y2 = 86 + ((i+1)* 187) - 3
    cropped_region = (180,y1,340,y2)
    G1_Element = Periodic_Table.crop(cropped_region)
    G1_Name = G1_List[i]
    G1_Name_Formatted = ("%s" % (G1_Name)) + ".bmp"
    G1_Element.save(
        "C:\\Users\\Kids\\Documents\\Robert\\Python Programming\\Periodic Table Quiz\\PIL Programs and Images\\Group 1 Elements"
        , G1_Name_Formatted)

In both situations, I am receiving this error message:

save_handler = SAVE[format.upper()]
KeyError: 'HYDROGEN.BMP'

From the post I pasted the link to earlier, it was suggested to remove the '.' from the '.bmp' so the extension could be recognised in upper case, however this also didn't work.

Any solutions would be much appreciated, preferably without the use of additional modules such as 'numpy', however if any of these must be used I will be unfamiliar with them and will need the code in the answer to be explained to me fully, if I am to understand it.

note: I am using Bitmap images because I have understood in certain python documentation that tkinter, which I plan to use with PIL (Pillow), is only compatible with bitmap images: https://pillow.readthedocs.io/en/5.2.x/reference/ImageTk.html

Thanks

解决方案

You are passing the name of the file as the second parameter to Image.save.

However, the second parameter is the (optional) file format - if specified it must match a registered file format, e.g. GIF, BMP, PNG, ...

What you probably wanted to do is to concatenate the path and the image name - no need to specify the format.

import os

...

# dir_path can be set outside of your loop
dir_path = "C:\\Users\\Kids\\Documents\\Robert\\Python Programming\\Periodic Table Quiz\\PIL Programs and Images\\Group 1 Elements"
...
G1_Name_Formatted = ("%s" % (G1_Name)) + ".bmp"
file_path = os.path.join( dir_path, G1_Name_Formatted  ) 
G1_Element.save( file_path )

or if you want to explicitely specify the format change the last part to:

G1_Element.save( file_path, "BMP" )

这篇关于如何使用Python PIL(枕头)将图像保存在特定的文件目录中,而不会由于以下原因而导致KeyError:save_handler = SAVE [format.upper()]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆