Question

Hello everyone I'm trying to open a image that i have downloaded through a link. I searched on the site and found something very useful and implemented that into my code.

*if* __name__ == "__main__":
    import urllib
    droste = urllib.urlopen("http://is.gd/cHqT")
    with open("droste.png", "wb") as imgFile:
        imgFile.write(droste.read())
    print "Got it!"
    droste = Image.open("droste.png")
    while droste:
        droste.show()
        droste = reveal(droste)
        if droste:
            open("droste.png", "wb").write(droste)
            droste = Image.open("droste.png")

The error occurs on the 7th line "droste = Image.open("droste.png")". I'm getting a IOError: cannot identify image file. I know the image has been downloaded because the codes runs great until that particular line and the line print "Got it!" actually confirms that its been downloaded. I don't know if I need to specify the path of the image file in the parameter in the open instead the of image name. Or maybe I need to check the path of the file. Please help.

Was it helpful?

Solution

Your code is functional. The problem is how you are running it. You mentioned in your comments that you are using PythonAnywhere. PythonAnywhere is not set up to do anything graphical. It will download the image into the correct directory, but PIL will not function correctly with PythonAnywhere.

Try the following code to test this.

import urllib

if __name__ == "__main__":
    droste = urllib.urlopen("http://is.gd/cHqT")
    with open("droste.png", "wb") as imgFile:
        imgFile.write(droste.read())
    print "Got it!"

    print "Now lets test if it really exists..."
    try:
        with open("droste.png", "rb") as imgFile:
            pass
        print "There were no errors so the file exists"
    except:
        print "ERROR: image was not saved properly!"

If you start up a BASH session with PythonAnywhere, you will see that the file droste.png exists, and you can download it to your computer and view it. Your program is OK.

If you really want to use your program, or get serious about python programming. You really should install Python locally to your computer. If you want to keep your code in the cloud, then use dropbox, github, or bitbucket. PythonAnywhere has uses, but normally you will just want to have python on your computer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top