How to save images in specific file directories using Python PIL (Pillow) without getting a KeyError due to: save_handler = SAVE[format.upper()]

泄露秘密 提交于 2020-01-15 09:41:28

问题


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


回答1:


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" )


来源:https://stackoverflow.com/questions/51268436/how-to-save-images-in-specific-file-directories-using-python-pil-pillow-withou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!