0

I am trying to download images with Python and I found some good answers in Downloading a picture via urllib and python , but when I try to use easygui to type in a url the image just opens in my browser instead of downloading the image

Here is my code:

import easygui as eg
import os
import urllib

url = eg.enterbox('Enter url: ', 'URL Choice')
name = eg.enterbox('Enter name: ', 'Name')

def DownloadImage(URL, name):
    try:
        image = urllib.urlopen(URL)
        if image.headers.maintype == 'image':
            buf = image.read()
            path = os.getcwd() + 'C:\Users\USER\ImageGrabber\Pics'
            file_path = '%s%s' % (path,name+'.jpg')
            downloaded_image = file(file_path, 'wb')
            downloaded_image.write(buf)
            image.close()
        else:
            return False
    except:
        print 'Cannot access file'
        return False
    return True

DownloadImage(url,name)

I have used the type function and the result from the boxes are strings, so I do not know why it will not work

Edit: forgot to include variables

Community
  • 1
  • 1
Ian Baker
  • 33
  • 1
  • 4
  • I hope you provided the image _type_ to your filename. Simply downloading binary data doesn't help if your computer can't tell if it's in PNG or JPEG format. – Akshat Mahajan May 03 '16 at 15:57
  • I can't reproduce the error I tried to download this image `https://www.gravatar.com/avatar/775d6701040d31eb0a22479dec666b27?s=32&d=identicon&r=PG` and it seems to work just fine – Rafael Almeida May 03 '16 at 16:00
  • @AkshatMahajan yes I have, I just forgot to put that in my post. Another thing I should have mentioned is that the code skips to the except statement every time. – Ian Baker May 03 '16 at 16:01
  • 1
    @RafaelAlmeida I assume you changed the line `path = os.getcwd() + 'C:\Users\USER\ImageGrabber\Pics'` since there is no way that would work on any computer as far as I can tell. – Tadhg McDonald-Jensen May 03 '16 at 16:03
  • @Ian When I put a wrong link or a wrong path it does print `Cannot access file` – Rafael Almeida May 03 '16 at 16:03
  • @RafaelAlmeida did you put the link in instead of using easygui? When I directly put in the url for the url variable it works fine but when using easygui it skips to the except statement. – Ian Baker May 03 '16 at 16:08
  • 1
    wow I just noticed the catch all `except`, @IanBaker can I suggest removing that or adding `traceback.print_exc()` in it so you can at least see what the error that it gives you? – Tadhg McDonald-Jensen May 03 '16 at 16:09
  • @TadhgMcDonald-Jensen Thanks to you I found my problem. I was adding a complete file path (starting from C:) when it just needed a file path from where the script was located, which is one up from the Pics folder. – Ian Baker May 03 '16 at 16:20

1 Answers1

0

Your target path is wrong os.getcwd() gives you the current working directory and you are appending an absolute directory.

So let's say your script is in C:\myscript.py, os.getcwd() will return C:\ and you are appending to that C:\Users\USER\ImageGrabber\Pics, the result will be C:\C:\Users\USER\ImageGrabber\Pics which is an invalid path therefore going to the exception.

path='C:\Users\USER\ImageGrabber\Pics\' 
# The last '\' is important

that should work

edit

I cannot test because I'm running linux but the directory may need to be like this

path='C:/Users/USER/ImageGrabber/Pics/'
Rafael Almeida
  • 5,142
  • 2
  • 20
  • 33