2

I am trying to download many images from a list of URLs.
When I use this code on just one image, it works fine.

img = Image.open(requests.get(url_dict[key], stream = True).raw)
        img.save(f'images/{file_name}.jpg')

When I run it through the for loop below, it downloads a bunch of empty files with no extension.
Why?
How do I fix this?

for key in url_dict:
    file_name = key.replace(' ', '_')
    img = Image.open(requests.get(url_dict[key], stream = True).raw)
    img.save(f'images/{file_name}.jpg')

I am expecting to get a folder full of images that actually contain data.

Squidly
  • 23
  • 5

1 Answers1

1

Try using the format of the pillow image to create the file.

I've tried this code with different URLs and worked ok for me.

import requests
from PIL import Image

url_dict = {"river":"https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Escudo_del_C_A_River_Plate.svg/1200px-Escudo_del_C_A_River_Plate.svg.png","river1":"https://play-lh.googleusercontent.com/LS7e70lkgIyJHxBWKr1YY5BRytD7Aw2th-dk8K66kU-c-fkr5e2Yo3Eh2RK9vFanYh8"}

for key in url_dict:
    file_name = key.replace(' ', '_')
    img = Image.open(requests.get(url_dict[key], stream = True).raw)
    img.save(f'images/{file_name}.{img.format.lower()}')
Pedro Rocha
  • 1,373
  • 1
  • 3
  • 14
  • Using this code still exports a bunch of empty files with no extension for me. – Squidly Feb 21 '23 at 01:39
  • even with the example url_dict in the answer? – Pedro Rocha Feb 21 '23 at 01:43
  • No, sorry overlooked that. I just tried with your dict and it works. Here is an example of the urls in my dict. Sorry cant share them all. https://cdn.shopify.com/s/files/1/0704/3268/6367/products/preview_d97e853f-ab59-4c48-a4ef-ed4d9c16cdb0.jpg?v=1676777680 – Squidly Feb 21 '23 at 01:56
  • It's working for me. Make sure the URLs in your dict contains `https://....`and the `images` folder exists – Pedro Rocha Feb 21 '23 at 02:01
  • These example urls dont seem to be working. They do have https and .jpg. The images folder exists. https://cdn.shopify.com/s/files/1/0704/3268/6367/products/preview_dece2ca5-c06c-45f7-b655-c6b7f87853bf.jpg https://cdn.shopify.com/s/files/1/0704/3268/6367/products/preview_31c4e954-1b97-480b-85f8-7f6c1d2a56c2.jpg – Squidly Feb 21 '23 at 02:06
  • 1
    Those urls are working ok for me. As .jpg and .pgn too. I can't replicate your error. My PIL version is '9.4.0' and my requests version is '2.28.2'. What are you using as keys for your dict? I guess the error could be there – Pedro Rocha Feb 21 '23 at 02:16
  • Ughh THANK YOU! You solved it for me. It was my keys. I completely overlooked that all the keys had a colon in them which was causing the issue. Dumb mistake. Thanks so much Pedro! Super helpful! – Squidly Feb 21 '23 at 02:20