3

I'm using python 2.7 and pycharm is my editor. What i'm trying to do is have python go to a site and download an image from that site and save it to my directory. Currently I have no errors but i don't think its downloading because the file is not showing in my directory.

import random
import urllib2

def download_web_image(url):
    name = random.randrange(1,1000)
    full_name = str(name) + ".jpg"
    urllib2.Request(url, full_name)

download_web_image("www.example.com/page1/picture.jpg")
ForceBru
  • 43,482
  • 10
  • 63
  • 98
jumpman8947
  • 427
  • 2
  • 7
  • 17

3 Answers3

3

This will do the trick. The rest can stay the same, just edit your function to include the two lines I have added.

def download_web_image(url):
    name = random.randrange(1,1000)
    full_name = str(name) + ".jpg"
    request = urllib2.Request(url)
    img = urllib2.urlopen(request).read()
    with open (full_name, 'w') as f: f.write(img)

Edit 1:

Exact code as requested in comments.

import urllib2

def download_web_image(url):
    request = urllib2.Request(url)
    img = urllib2.urlopen(request).read()
    with open ('test.jpg', 'w') as f: f.write(img)

download_web_image("http://upload.wikimedia.org/wikipedia/commons/8/8c/JPEG_example_JPG_RIP_025.jpg")
fsimkovic
  • 1,078
  • 2
  • 9
  • 21
  • Try with the image [here](http://upload.wikimedia.org/wikipedia/commons/8/8c/JPEG_example_JPG_RIP_025.jpg) because I get a weird distortion. – Malik Brahimi May 27 '15 at 19:41
  • @MalikBrahimi What do you mean? Works perfectly fine for me! – fsimkovic May 27 '15 at 19:44
  • Thanks, this does indeed do the trick. One problem i have is that my tested image comes out distorted and green, so its probably something with the image itself, i'm going to try other images to see if it works. – jumpman8947 May 27 '15 at 19:45
  • @jumpman8947 Odd, it works for me, looks like it is supposed to. Just to confirm, brown leaves, grey stone, bit of green grass top left and bottom right. – fsimkovic May 27 '15 at 19:48
  • @Felix_Sim It still is distorted, both blocks of code. – Malik Brahimi May 27 '15 at 19:52
  • @MalikBrahimi Then I do not know. I cannot reproduce any distortion or colour problem regardless of how many times I am trying. Maybe it's the OS. I'm on OS X Yosemite 10.10.3 with Python 2.7.6. – fsimkovic May 27 '15 at 19:54
  • I'm on Windows 8.1 though I doubt it's the OS. I mean we're reading the data and writing it. – Malik Brahimi May 27 '15 at 20:03
  • Felix_Sim, i run your exact code- it run with no problem- but where i see the download file? – newGIS Jun 01 '16 at 05:53
2

You are simply creating a Request but you are not downloading the image. Try the following instead:

urllib.urlretrieve(url, os.path.join(os.getcwd(), full_name)) # download and save image
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
1

Or try the requests library:

import requests

image = requests.get("www.example.com/page1/picture.jpg")
with open('picture.jpg', 'wb') as f:
    f.write(image.content)