python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.19k stars 2.22k forks source link

Accessing URLs #7673

Closed R3dan closed 9 months ago

R3dan commented 9 months ago

Feature Request -- Allow importing images from url

What did you expect to happen?

I expected to be able to show, manipulate etc (standard PIL features) the image

What actually happened?

When I tested this with the latest version I got error:

 Traceback (most recent call last):
  File "{file address}", line 17, in <module>
    with Image.open(url) as img:
         ^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\site-packages\PIL\Image.py", line 3243, in open
    fp = builtins.open(filename, "rb")
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument: '{url}' 

What are your OS, Python and Pillow versions?

with Image.open(url) as img:
    img.load()

What I want

I want to have the ability to access images from url's.

nulano commented 9 months ago

Is there any reason you can't use the method from the tutorial?

from PIL import Image
from urllib.request import urlopen
url = "https://python-pillow.org/assets/images/pillow-logo.png"
img = Image.open(urlopen(url))

I would be wary of adding something like this to Pillow natively, since there are many other configuration options others could then request be also added (headers, user agent, cookies, https parameters, certificates, ...).

R3dan commented 9 months ago

When looking through the docs I had not seen this. Sorry. I understand what you mean in terms of why not to add it and I would agree although also think 2 things: 1) If someone made a PR with the additional in (headers, cookies etc) then you could accept that, with no additional work for you. 2) Surely it could just be done by using the requests module and loading the bytes.

I also tried to find a way to load bytes but couldn't (I have since found out how via the tutorial). I am probably overlooking something, but whats stopping you from importing io with a function to load? Something like this:

import io
...
#class image
@classmethod
def load_from_bytes(cls, bytes):
    return Image.open(io.BytesIO(bytes))

Fyi this is not me complaining. What you have made is really useful and most of this is just me not knowing how to use it (my problem). It would just seem that a lot of useful tasks (opening from url or bytes) could be easier to find and do to both new programmers (who might not know of how well documented things are) or just people new to PIL.

hugovk commented 9 months ago
  1. If someone made a PR with the additional in (headers, cookies etc) then you could accept that, with no additional work for you.

There's always additional work.

We need to review, make sure it's properly tested and documented, continue maintaining it "forever", deal with bug reports from people having network and proxy issues etc.

2. Surely it could just be done by using the requests module and loading the bytes.

Pillow has no Python dependencies, we'd have to think carefully about adding any new ones.

It's probably better for the user to deal with the network using their client of choice (stdlib, requests, urllib3, httpx, etc.).

R3dan commented 9 months ago

I see your point, I just feel certain things could be made easier. I'm happy to close if you are, I just ask that you consider it when making future versions etc.

radarhere commented 9 months ago

It would just seem that a lot of useful tasks (opening from url or bytes) could be easier to find and do to both new programmers (who might not know of how well documented things are)

You seem to have an idea that dedicated methods are a better form of communication than documentation. I question this, as there are many methods in Image.py, so that seems like a somewhat daunting approach to take.

On the other hand, searching for 'url' in the docs quickly leads you to the tutorial - https://pillow.readthedocs.io/en/stable/search.html?q=url

I just ask that you consider it when making future versions etc.

I think we have considered it. If a stampede of users arrive tomorrow demanding this feature, then yes, we will reconsider this, but even at the moment, when data is fetched from outside of Pillow, we still get users coming to us when there is a 403 error (#7419) or if an asset is hidden behind a login system (#6589 or #4359). Incorporating URL fetching into Pillow wouldn't help with convincing users that this is not the fault our software, and I don't think it keeps in mind separation of concerns.

R3dan commented 9 months ago

I see your point, we both have our opinions and that's fine. This was after all a request not a demand. Thank you for the help.