[Python] PIL.UnidentifiedImageError for some image URL, but not others

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to open an image at a URL using Pillow, but it only works for some URL. In my code below, URL1 works but not URL2.

    import requests
    from PIL import Image

    url1 = "https://picsum.photos/200"
    # url2 = "https://www.electrical-forensics.com/MajorAppliances/GE/Small/GE_2018_Stackable_Washer_Dryer_Tag.jpg"

    image = Image.open(requests.get(url, stream=True, verify=False).raw)


    If I run the code with URL2, it gives me an error:

    PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x...>


    I am able to open both URLs with my browser so I'm really curious as to why. Are there any methods that will work on both?

    Thanks

    Continue reading...

Compartilhe esta Página