0

Consider:

from PIL import Image

mac = Image.open('example.jpg')

type(mac)

mac.show()

I have displayed some code as an example and was wondering how to open a file such as 'example.jpg' from GitHub.

Here is the group of files I am trying to open from GitHub:

https://i.stack.imgur.com/AXhtm.png

And this is the error I am currently getting

https://i.stack.imgur.com/fMJG7.png

Is there a way I can access these files from Visual Studio Code? Does it have something to do with changing my current directory which is currently set as the following?

https://i.stack.imgur.com/T3LxM.png

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
abcdefg
  • 1
  • 3
  • 1
    Does this answer your question? [How do I read image data from a URL in Python?](https://stackoverflow.com/questions/7391945/how-do-i-read-image-data-from-a-url-in-python) – ldavid Nov 19 '21 at 18:14
  • img = Image.open(BytesIO(response.content)) – abcdefg Nov 19 '21 at 18:16
  • What does this line of code do? – abcdefg Nov 19 '21 at 18:16
  • `response` contains the HTTP response from the web request, and `response.content` will contain the request body (in this case, the file itself). `BytesIO(response.content)` instantiates a file buffer that contains the image, which can be transparently read by `Image.open`. – ldavid Nov 19 '21 at 18:19
  • Say if I was trying to open another file from Github bus is not an image and I would like to iterate through that file to find certain items, would this method also work? – abcdefg Nov 19 '21 at 18:20
  • Obviously without the image.open part – abcdefg Nov 19 '21 at 18:21
  • Yes, the IO buffer can store any file, though you would need to know the exact format to make sense of what is inside of it. Requests can also download files to your local machine, or stream them with `requests.get(..., stream=true)`. – ldavid Nov 19 '21 at 18:44
  • 1
    Please provide the information as text, not as images. Please [edit (change) your question](https://stackoverflow.com/posts/70039195/edit) (but ***without*** "Edit:", "Update:", or similar - the question should appear as if it was written right now). Thanks in advance. – Peter Mortensen Nov 19 '21 at 22:14

0 Answers0