0

Is there a way to get RGB values at given coordinates from an image without having to save it to the disk? So is it possible to just enter the URL of images with a Python library and read their RGB data (or is saving then deleting them the best way)?

Nesa
  • 2,895
  • 2
  • 12
  • 19
  • This really depends on the situation, please be more specific about the image's source and which libraries you wish to use to accomplish the task you have in mind. You have Python tagged but then mention "images in a browser", you'll need to provide more information to clear up that ambiguity. – Hunter Smith May 15 '18 at 15:22
  • @IsaacSmith I edited it, it doesn't matter which Python libraries I have to use if they can be downloaded for free. – Nesa May 15 '18 at 15:29
  • 2
    This question is VERY similar to [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#7391991) . In that thread there are some good suggestions on how to grab the image in both Python 2.7 and Python 3.x. – bfris May 15 '18 at 15:47

2 Answers2

2

Depending on the nature of what you're trying to accomplish, there is no need to save the image to a file. You haven't said which imaging library you'd like to use. PIL (pillow these days) is a very popular choice. But there are others.

Many people prefer to use Requests for managing URL downloads. But it's an extra module to install. You can also give urllib.request.urlopen a try, which is part of the standard library.

Here's a short piece of code that will use PIL (pillow) plus either urllib or Requests in Python3:

Edit: original answer did not demonstrate how to access pixel data. Code updated.

from PIL import Image
from io import BytesIO

# BBC Test Card F image from 1967
url = 'https://upload.wikimedia.org/wikipedia/en/5/52/Testcard_F.jpg'

# load the image with urllib + BytesIO
import urllib.request

response = urllib.request.urlopen(url)
img = Image.open(BytesIO(response.read()))

# load the image with requests
import requests

response = requests.get(url)
img = Image.open(BytesIO(response.content))

# it is very likely every image you come across will already be RGB
# so next line is usually not needed
#img = img.convert('RGB')

r, g, b = img.getpixel((1, 1))

print(r, g, b)

# if you already use NumPy, you can convert the image to a NumPy array
# and access pixels that way

import numpy as np
npimg = np.array(img) # 3 dim array first to dims are pixel x,y
                      # last dim is color
r, g, b = npimg[1,1,:]
print(r, g, b)        # r = npimg[1,1,0], g = npimg[1,1,1], b = npimg[1,1,2]
bfris
  • 5,272
  • 1
  • 20
  • 37
0

Typically, at least for the case of finding an image online, it is easier to save the image to disk and then load the image into your program.

The alternative is requesting the image from the site itself, which is totally possible, but a little bit more work. The requests or urllib module can help there. http://docs.python-requests.org/en/master/ - related questions: How do I read image data from a URL in Python? How to download image using requests

Requesting the image from the site itself might be a better option if you are already web-crawling for the image, have a list with tons and tons of image urls you don't have to try and find, or have to deal with images at links changing (or general dynamically-provided images)

If you first have to find the images, and copy the url of (just the image), or otherwise mess with the request for each individual image, you might as well just r-click and save unless HD memory space is an issue, (imo)

I suppose you might be able to try and find where images are stored when your browser loads them, but that's a lot more work, and possibly unreliable.

You also may be able to try and "separate" the image from the current "screenshot" in memory. Another "lot of work and unreliable" method. (after edits)

DoubleDouble
  • 328
  • 2
  • 12