1

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

robert__meadows
  • 23
  • 1
  • 1
  • 4

1 Answers1

2

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" )
Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
  • Thanks Mike, I amended my program and it worked exactly how I wanted it to, although I do have one more question. When these images are created they cant be listed or stored in the order that they appear in their group as caesium is first alphabetically and they were all created on the same date, at the same time and cant be correctly listed by date created either. Do you have any ideas how I could have them listed in the file in group, or atomic number order? – robert__meadows Jul 10 '18 at 21:26
  • @robert__meadows If you don't want it as the leading part of your file name (e.g. ``ordernumber_Hydrogen.bmp``), there aren't many options. You could change the file creation date afterwards, see this answer: https://stackoverflow.com/a/4996407/4349415 Please consider to mark my answer as accepted if it helped solving your issue. – Mike Scotty Jul 11 '18 at 06:50
  • Thanks again Mike, that has helped, but looking at the options you gave I won't bother trying to order them as when I open them I can just use the file name rather than the order they are listed in. Also, I am new to stack overflow with and don't know how to mark a comment as accepted. – robert__meadows Jul 11 '18 at 11:02