Help with my photo opening script

This is the place for queries that don't fit in any of the other categories.

Help with my photo opening script

Postby w1zzerd » Wed Mar 13, 2013 2:18 am

So the purpose of this code is just to understand python a bit more.

The goal of this code is to type in the file name and if its found then open the file. The file type will be images png or jpg.

I know the code isnt the pretties but its just practice.

Here is the code. Thanks in advance.

Code: Select all
import sys
import os
import Image

while True:
    print "What photo would you like to see?"
    photo = raw_input("> ")
   
    exist = os.path.isfile(photo)
   
    if exist == False:
        print "That file does not exist"
       
    elif exist == True:
        print "I found your file!"
        Image.open(photo)
        print "would you like to continue?"
        cont = raw_input("(y/n) ")
        if cont == "n":
            break
        else:
            print " "
Current Languages: Javascript, HTML5
Languages I'm learning: Python, C++
w1zzerd
 
Posts: 8
Joined: Thu Feb 21, 2013 8:48 pm

Re: Help with my photo opening script

Postby Mekire » Wed Mar 13, 2013 3:33 am

Well theoretically if you just want to open the file with the default image viewer, this should do it:
Code: Select all
myphoto = Image.open(photo)
myphoto.show()
Unfortunately on windows (and possibly other operating systems), the temporary file is apparently deleted before you can view it and you will be told that the image wasn't found.

One solution I found that worked for me was using this instead:
Code: Select all
import webbrowser #at beginning of file

webbrowser.open(photo)
This still uses the default image viewer (not the webbrowser as one would assume it would), but it is of course a terrible solution really. Honestly I recommend you work with some sort of gui toolkit/api (like Tkinter for instance) but this may unfortunately be above your current skill level.

PS: In the future include a little something about what error you are getting (traceback if applicable) or what result it is you expected to see and didn't see.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Help with my photo opening script

Postby w1zzerd » Wed Mar 13, 2013 4:28 am

Mekire wrote:Well theoretically if you just want to open the file with the default image viewer, this should do it:
Code: Select all
myphoto = Image.open(photo)
myphoto.show()
Unfortunately on windows (and possibly other operating systems), the temporary file is apparently deleted before you can view it and you will be told that the image wasn't found.

One solution I found that worked for me was using this instead:
Code: Select all
import webbrowser #at beginning of file

webbrowser.open(photo)
This still uses the default image viewer (not the webbrowser as one would assume it would), but it is of course a terrible solution really. Honestly I recommend you work with some sort of gui toolkit/api (like Tkinter for instance) but this may unfortunately be above your current skill level.

PS: In the future include a little something about what error you are getting (traceback if applicable) or what result it is you expected to see and didn't see.

-Mek


Hey Mek thanks a lot that helped me.
I used webbrowser and it worked like a charm.

Currently everything works as I want it to work. User gets prompted to type in a file to open, if the file exists then the file opens and the user gets asked if he wants to open another file.

If the file does not exist then the user gets asked if he wants to try another file name.

I have been looking at Tkinter. Currently I have been working a lot with CGI scripts manipulating HTML user input and am about to get into Django. Although I'm learning and taking a few classes on python, I keep hearing that the best way to learn is through just jumping in and coding some small scripts that I'm interested in so I jumped right in.

Thanks again for your help.
Current Languages: Javascript, HTML5
Languages I'm learning: Python, C++
w1zzerd
 
Posts: 8
Joined: Thu Feb 21, 2013 8:48 pm


Return to General Coding Help

Who is online

Users browsing this forum: buran and 7 guests