hardikvasa / google-images-download

Python Script to download hundreds of images from 'Google Images'. It is a ready-to-run code!
MIT License
8.58k stars 2.11k forks source link

Single image download errors #323

Open eyjho opened 4 years ago

eyjho commented 4 years ago

Hello, this program has been incredibly useful, but I've gotten stuck while trying to download a single image in a Python script. I'm still relatively new to Python and these programs so forgive me if it's a silly question. The command (below) seems to ignore the single image argument entirely and simply proceed to download 100 images using the keywords. Having the keywords argument seems unnecessary to download an image from an url, but I get an error if the argument is not included (UnboundLocalError: local variable 'search_keyword' referenced before assignment).

from google_images_download import google_images_download
response = google_images_download.googleimagesdownload()
image_url = 'https://upload.wikimedia.org/wikipedia/commons/f/f1/Jack_Russell_Terrier_1.jpg'
response.download({"keywords":"jack russel terrier", "single_image": image_url})

To debug, I attempted to implement the example from the documentation. Although I am using a Python script, only a command line example is available using the single_image argument googleimagesdownload --keywords "baloons" --single_image <URL of the images>

To which I get the following errors:

(base) C:\Users\Eugene>googleimagesdownload --keywords "jack russel" --single_image 'https://upload.wikimedia.org/wikipedia/commons/f/f1/Jack_Russell_Terrier_1.jpg'
Traceback (most recent call last):
  File "c:\users\eugene\anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\eugene\anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\Eugene\anaconda3\Scripts\googleimagesdownload.exe\__main__.py", line 7, in <module>
  File "c:\users\eugene\anaconda3\lib\site-packages\google_images_download\google_images_download.py", line 1134, in main
    response.single_image(arguments['single_image'])
  File "c:\users\eugene\anaconda3\lib\site-packages\google_images_download\google_images_download.py", line 416, in single_image
    response = urlopen(req, None, 10)
  File "c:\users\eugene\anaconda3\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "c:\users\eugene\anaconda3\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
  File "c:\users\eugene\anaconda3\lib\urllib\request.py", line 548, in _open
    'unknown_open', req)
  File "c:\users\eugene\anaconda3\lib\urllib\request.py", line 503, in _call_chain
    result = func(*args)
  File "c:\users\eugene\anaconda3\lib\urllib\request.py", line 1389, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib.error.URLError: <urlopen error unknown url type: 'https>

Since there seems to be some issues with the url, I tried some variants:

(base) C:\Users\Eugene>googleimagesdownload --keywords "jack russel" --single_image 'upload.wikimedia.org/wikipedia/commons/f/f1/Jack_Russell_Terrier_1.jpg'
...
ValueError: unknown url type: "'upload.wikimedia.org/wikipedia/commons/f/f1/Jack_Russell_Terrier_1.jpg'"

(base) C:\Users\Eugene>googleimagesdownload --keywords "jack russel" --single_image 'https://upload.wikimedia.org/wikipedia/commons/f/f1/Jack_Russell_Terrier_1.jpg'
...
urllib.error.URLError: <urlopen error unknown url type: 'https>

I checked the requests module in Python, and it seems to be working:

import requests
image_url = 'https://upload.wikimedia.org/wikipedia/commons/f/f1/Jack_Russell_Terrier_1.jpg'
r = requests.get(image_url, allow_redirects=True)

So I'll just be using this as a substitute, but would be great to use the google_images_download program feature.

Joeclinton1 commented 4 years ago

Interesting. I personally don't know why google images downloads needs a single image option as the requests module is a much better option for this case, as it is a more supported module.

Anyway, the single image argument does not seem to work with the downloads module instead use the single_image module. The code below should work:

from google_images_download import google_images_download
response = google_images_download.googleimagesdownload()
image_url = 'https://upload.wikimedia.org/wikipedia/commons/f/f1/Jack_Russell_Terrier_1.jpg'
response.single_image(image_url)

As for the command line, it will automatically use the single image module. The mistake you have, is that you have used single quotes, which will be parsed as part of the url. This made it think your url begun with 'https:// , hence the error message.

To fix that just use double quotes or no quotes at all like in the code below: googleimagesdownload --keywords "jack russel" --single_image "upload.wikimedia.org/wikipedia/commons/f/f1/Jack_Russell_Terrier_1.jpg"

FireRoadRepository commented 4 years ago

Hi.

I am a student studying Computer Science in Coventry University. I am interested to contribute to this project by attempting to enhance the download_page(), download_extended_page(), and single_image() functions.

tectanagrand commented 2 years ago

Hello, i ve tested single image download and it works. I assume there's nothing wrong with internet connection. But somehow i try to download multiple image from code from here and it stucks in 'evaluating'. i havent re-read all of the code and what 'evaluating' does. Could you help me, is it something wrong happen during this process? thankyou in advance

btw this is my commandline working on the package

C:\Users\ASUS TUF GAMING\Desktop\image_downloader>python -m image_downloader.py

Item no.: 1 --> Item name = The smartphone also features an in display fingerprint sensor.
Evaluating...

and this is the image_downloader script

from google_images_download import google_images_download
response = google_images_download.googleimagesdownload()

search_queries = [

'The smartphone also features an in display fingerprint sensor.',
'The pop up selfie camera is placed aligning with the rear cameras.',
'''In terms of storage Vivo V15 Pro could offer
up to 6GB of RAM and 128GB of onboard storage.''',
'The smartphone could be fuelled by a 3 700mAh battery.',
]

def downloadimages(query):

    arguments = {"keywords": query,
                "format": "jpg",
                "limit":4,
                "print_urls":True,
                "size": "medium",
                "aspect_ratio":"panoramic"}
    try:
        response.download(arguments)

    except FileNotFoundError:
        arguments = {"keywords": query,
                    "format": "jpg",
                    "limit":4,
                    "print_urls":True,
                    "size": "medium"}

        try:

            response.download(arguments)
        except:
            pass

for query in search_queries:
    downloadimages(query)
    print()

i took it unedited from the source to test if it works

NicolasGrosjean commented 2 years ago

Hi @tectanagrand,

If you install _google_imagesdownload using pip install google_images_download, you have a quite old version (21st May 2019 according this page) which can maybe explain your issue.

By installing the Joeclinton1's fork (thanks to the commands given in the first message of #298 ), I have no issue with your script :

Spoiler of the messages in the console ``` python -m image_downloader.py Item no.: 1 --> Item name = The smartphone also features an in display fingerprint sensor. Evaluating... Starting Download... Image URL: https://www.tremplin-numerique.org/wp-content/uploads/2020/11/Comment-fonctionne-la-numerisation-dempreintes-digitales-sur-lecran.jpg Completed Image ====> 1.comment-fonctionne-la-numerisation-dempreintes-digitales-sur-lecran.jpg Image URL: https://fdn.gsmarena.com/imgroot/news/21/01/apple-iphone-13-in-display-fingerprint-reader/-1220x526/gsmarena_001.jpg Completed Image ====> 2.gsmarena_001.jpg Image URL: https://media.oscarmini.com/wp-content/uploads/2019/02/05030731/OnePlus-6Treal.jpg Completed Image ====> 3.oneplus-6treal.jpg Image URL: https://www.xda-developers.com/files/2020/03/Image-810x298_c.jpg Completed Image ====> 4.image-810x298_c.jpg Errors: 0 Item no.: 1 --> Item name = The pop up selfie camera is placed aligning with the rear cameras. Evaluating... Starting Download... Image URL: https://www.gizchina.com/mrf4u/statics/i/ps/www.gizchina.com/wp-content/uploads/images/2019/03/vivo-x27-pro.jpg?width=1200&enable=upscale Completed Image ====> 1.vivo-x27-pro.jpg Image URL: https://fdn.gsmarena.com/imgroot/news/19/10/motorola-one-popup-camera/-728x314/gsmarena_000.jpg Completed Image ====> 2.gsmarena_000.jpg Image URL: https://fdn.gsmarena.com/imgroot/news/19/12/huawei-p-smart-pro-official/-727/gsmarena_005.jpg Completed Image ====> 3.gsmarena_005.jpg Image URL: https://www.igeekphone.com/wp-content/uploads/2019/03/Oppo-Reno-Render-igeekphone-2.jpg Completed Image ====> 4.oppo-reno-render-igeekphone-2.jpg Errors: 0 Item no.: 1 --> Item name = In terms of storage Vivo V15 Pro could offer up to 6GB of RAM and 128GB of onboard storage. Evaluating... Starting Download... Image URL: https://www.mobigyaan.com/wp-content/uploads/2019/02/Vivo-V15-Pro-Hands-on-Images-8.jpg Completed Image ====> 1.vivo-v15-pro-hands-on-images-8.jpg Image URL: https://img.etimg.com/photo/msid-68076593,quality-100/vivo-launches-v15-pro-with-the-worlds-first-32mp-pop-up-selfie-camera-at-rs-28990.jpg Completed Image ====> 2.vivo-launches-v15-pro-with-the-worlds-first-32mp-pop-up-selfie-camera-at-rs-28990.jpg Image URL: https://www.xda-developers.com/files/2019/02/vivo-v15-pro-hands-on-feat.jpg Completed Image ====> 3.vivo-v15-pro-hands-on-feat.jpg Image URL: https://s3bo.cashify.in/gpro/uploads/2019/10/17143046/Vivo-V15-Pro1.jpg?p=gp4&s=gp Completed Image ====> 4.vivo-v15-pro1.jpg Errors: 0 Item no.: 1 --> Item name = The smartphone could be fuelled by a 3 700mAh battery. Evaluating... Starting Download... Image URL: https://m.media-amazon.com/images/S/aplus-media/vc/67de2b1f-d9d7-452c-9b67-3be647259768._CR0,0,970,300_PT0_SX970__.jpg Completed Image ====> 1.67de2b1f-d9d7-452c-9b67-3be647259768._cr0,0,970,300_pt0_sx970__.jpg Image URL: https://static1.makeuseofimages.com/wordpress/wp-content/uploads/2019/02/smartphone-battery.jpg?q=50&fit=contain&w=750&h=375&dpr=1.5 Completed Image ====> 2.smartphone-battery.jpg Image URL: https://cdn.vox-cdn.com/thumbor/2-SfBXtPLW129GXDVTxmxll3R9s=/0x86:2040x1106/fit-in/1200x600/cdn.vox-cdn.com/uploads/chorus_asset/file/14608160/energizer_unit_vsavov7.jpg Completed Image ====> 3.energizer_unit_vsavov7.jpg Image URL: https://www.reliancedigital.in/wp-content/uploads/2018/12/Feature_1.jpg Completed Image ====> 4.feature_1.jpg Errors: 0 ```
tectanagrand commented 2 years ago

thankyou for your recommendation @NicolasGrosjean, sorry for the late reply. i have tried the fork, but somehow it is still stuck in "Evaluating...". I'm confused with the problem because there isn't any error logs or warnings. Do you have any idea to troubleshoot the problem? Thanks in advance.

NicolasGrosjean commented 2 years ago

If your Python version is 2.X, try with a 3.X.

Except this idea, the only one thing I see is to run a debugger (for example with VScode or PyCharm) to see what it is the failing line of code.