0

The user enters a state. I would like my function to find the corresponding image and display it. As you can see I only placed the file name in the dictionary. I would need to tell the program to go the the specific file the image is in somehow.

def picture(pictures_of_flowers,state_name):
    # This part of the code I don't know how to do.
    ...


pictures_of_flowers = {
    'Alabama': 'Alabama.jpg'
    ,'Alaska': 'Alaska.jpg'
    ,'Arizona':"Arizona.jpg"
    ,'Arkansas':"arkansas.jfif"
    ,'California':"california.jpg"
    ,'Colorado':"colarado.jpg"
    ,'Connecticut': "connecticut.jpg"
    ,'Delaware':"delaware.jpg"
    ,'Florida':"lorida.jpg"
    ,'Georgia':"georgia.jpg"
    ,'Hawaii':"hawaii.jpg"
    ,'Idaho':"idaho.jpg"
    ,'Illinois': "illinois.jpg"
    ,'Indiana':"indiana.jpg"
    ,'Iowa':"iowa.jpg"
    ,'Kansas':"kansas.jpg"
    ,'Kentucky':"kentucky.jpg"
    ,'Louisiana':"louisiana.jpg"
    ,'Maine':"maine.jpg"
    ,'Maryland':"maryland.jpg"
    ,'Massachusetts':"massachusetts.jpg"
    ,'Michigan':"michigan.jpg"
    ,'Minnesota':"minnesota.jpg"
    ,'Mississippi':"mississipi.jpg"
    ,'Missouri':"missouri.jpg"
    ,'Montana':"montana.jpg"
    ,'Nebraska':"nebraska.jpg"
    ,'Nevada':"nevada.jpg"
    ,'New Hampshire':"new hampshire.jpg"
    ,'New Jersey':"new jersey.jpg"
    ,'New Mexico':"new mexico.jpg"
    ,'New York':"new york.jpg"
    ,'North Carolina':"north carolina.jpg"
    ,'North Dakota':"north dakota.jpg"
    ,'Ohio':"ohio.jpg"
    ,'Oklahoma':"oklahoma.jpg"
    ,'Oregon':"oregon.jpg"
    ,'Pennsylvania':"pennslvania.jpg"
    ,'Rhode Island':"rhodeisland.jpg"
    ,'South Carolina':"south carolina.jpg"
    ,'South Dakota':"south dakota.jpg"
    ,'Tennessee':"tennessee.jpg"
    ,'Texas':"texas.jpg"
    ,'Utah':"utah.jpg"
    ,'Vermont':"vermont.jpg"
    ,'Virginia':"viginia.jpg"
    ,'Washington':"washington.jpg"
    ,'West Virginia':"west virginia.jpg"
    ,'Wisconsin':"wisconsin.jpg"
    ,'Wyoming':"wyoming.jpg"
}

state_name = input("\nEnter STATE Name: ")
picture(pictures_of_flowers,state_name)
martineau
  • 119,623
  • 25
  • 170
  • 301
user3316598
  • 163
  • 1
  • 7
  • Does this answer your question? [Showing an image from console in Python](https://stackoverflow.com/questions/1413540/showing-an-image-from-console-in-python) – 001 Feb 02 '22 at 22:31
  • no, I want to avoid having to write out the whole location of the file in the dictionary. – user3316598 Feb 02 '22 at 22:34
  • Do you know the directory they are in or do you want to search the hard drive for the images? – 001 Feb 02 '22 at 22:36
  • 1
    `directory = 'C:/some path/'` and `directory + pictures_of_flowers`? Or `pathlib.Path('C:/some path/') / pictures_of_flowers`? – Grismar Feb 02 '22 at 22:37
  • I know the directory – user3316598 Feb 03 '22 at 01:52

1 Answers1

1

Here an example:

from PIL import Image

def picture(pictures_of_flowers, state_name):
    if state_name in pictures_of_flowers:  # State found
            with Image.open(pictures_of_flowers[state_name]) as img:
                    img.show()


pictures_of_flowers = {
'Alabama': 'Alabama.jpg'
,'Alaska': 'Alaska.jpg'
,'Arizona':"Arizona.jpg"
,'Arkansas':"arkansas.jfif"
,'California':"california.jpg"
,'Colorado':"colarado.jpg"
,'Connecticut': "connecticut.jpg"
,'Delaware':"delaware.jpg"
,'Florida':"lorida.jpg"
,'Georgia':"georgia.jpg"
,'Hawaii':"hawaii.jpg"
,'Idaho':"idaho.jpg"
,'Illinois': "illinois.jpg"
,'Indiana':"indiana.jpg"
,'Iowa':"iowa.jpg"
,'Kansas':"kansas.jpg"
,'Kentucky':"kentucky.jpg"
,'Louisiana':"louisiana.jpg"
,'Maine':"maine.jpg"
,'Maryland':"maryland.jpg"
,'Massachusetts':"massachusetts.jpg"
,'Michigan':"michigan.jpg"
,'Minnesota':"minnesota.jpg"
,'Mississippi':"mississipi.jpg"
,'Missouri':"missouri.jpg"
,'Montana':"montana.jpg"
,'Nebraska':"nebraska.jpg"
,'Nevada':"nevada.jpg"
,'New Hampshire':"new hampshire.jpg"
,'New Jersey':"new jersey.jpg"
,'New Mexico':"new mexico.jpg"
,'New York':"new york.jpg"
,'North Carolina':"north carolina.jpg"
,'North Dakota':"north dakota.jpg"
,'Ohio':"ohio.jpg"
,'Oklahoma':"oklahoma.jpg"
,'Oregon':"oregon.jpg"
,'Pennsylvania':"pennslvania.jpg"
,'Rhode Island':"rhodeisland.jpg"
,'South Carolina':"south carolina.jpg"
,'South Dakota':"south dakota.jpg"
,'Tennessee':"tennessee.jpg"
,'Texas':"texas.jpg"
,'Utah':"utah.jpg"
,'Vermont':"vermont.jpg"
,'Virginia':"viginia.jpg"
,'Washington':"washington.jpg"
,'West Virginia':"west virginia.jpg"
,'Wisconsin':"wisconsin.jpg"
,'Wyoming':"wyoming.jpg"
}

state_name = input("\nEnter STATE Name: ")
picture(pictures_of_flowers,state_name)
martineau
  • 119,623
  • 25
  • 170
  • 301
Nabil
  • 1,130
  • 5
  • 11
  • While this code may answer the question, it would be better to include some context, explaining _how_ it works and _when_ to use it. Code-only answers are not useful in the long run. – martineau Feb 03 '22 at 01:13
  • trouble is that the value in my dictionary does not contain the whole file path. I am wanting to know how I get the program to get that file from the file path ex. "C:\Users\ablev\OneDrive\Desktop\SDEV 300 6384 Building Secure Python Programs\State Images\missouri.jpg" – user3316598 Feb 03 '22 at 01:51
  • 1
    You can store the full path of the directory in a variable then use the function join in the module os.path to get the full path of the image – Nabil Feb 03 '22 at 02:10
  • got it, thanks so much! – user3316598 Feb 03 '22 at 02:31