0

I am trying to download an image from a webpage using a combination of the Splinter browser library (with ChromeDriver) and the Pillow library. I am using an instance of a Chrome browser to login to the website and navigate to the page with the image on it, and then using the browser.find_by_tag() method to locate the image.

I then try to use the Image.save() method of Pillow, but it throws this error:

C:\Users\my.name\Documents\Development\Python-Scripts>my_script.py
Traceback (most recent call last):
  File "C:\Users\my.name\Documents\Development\Python-Scripts\my_script.p
y", line 31, in <module>
    my_function()
  File "C:\Users\my.name\Documents\Development\Python-Scripts\my_script.p
y", line 19, in my_function
    farmer_image = Image.save(farmer_image_brobj)
AttributeError: 'module' object has no attribute 'save'

And my code:

from splinter import Browser
from PIL import Image

def my_function():
    with Browser('chrome') as browser: 
        url = "http://www.blah/login.aspx"
        url_two = "http://www.blah/ImagePageParent.aspx"

        browser.visit(url)
        browser.fill('un', 'blah')
        browser.fill('pw', 'blah')
        browser.find_by_name('btn').click()
        browser.visit(url_two)
        browser.find_link_by_partial_href('partial_href').click() 

        farmer_image_brobj = browser.find_by_tag('img')
        farmer_image = Image.save(farmer_image_brobj)

        browser.visit(url_two)
        browser.quit()

my_function()
Good ol' Gin
  • 59
  • 1
  • 4
  • 14

1 Answers1

2

From effbot example, you are calling it the wrong way. Instead of

    farmer_image_brobj = browser.find_by_tag('img')
    farmer_image = Image.save(farmer_image_brobj)

You should use the save() method on the instance of Image. So first, open the image:

farmer_imag = Image.open(...) #something doing with farmer_image_brobj

for instance, using requests, you could do something like:

r = requests.get('http://example.com/image.jpg')
farmer_image = Image.open(StringIO(r.content))

then save it by giving the path:

farmer_image.save('my_image.png')
fredtantini
  • 15,966
  • 8
  • 49
  • 55
  • Thanks, forgive me being a noob at this, but when you say "# something doing with farmer_image_brobj" what should I be doing with it? – Good ol' Gin Oct 15 '14 at 09:48
  • @DrBrown From `farmer_image_brobj`, you should retrieve the path (usually in the src attribute).. Then, you have to _get_ the content of the image (like the example using requests). – fredtantini Oct 15 '14 at 10:20
  • It worked thank you! Had to change the imports to import io and the StringIO to io.BytesIO. – Good ol' Gin Oct 15 '14 at 11:50