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()
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')