How can I get the color of a pixel using python3

Asked

Viewed 262 times

1

I would like to know how I can, using python, get the color of a pixel from the web? I’m trying to make a bot and I need it to collect the color of the web page and not an image of the following coordinates:

Coordinates - (774,503)

I’ve tried the package imagegrab but does not work because only exite to python2

I hope I have clarified my problem any information this about the problem please comment!

  • 2

    Are you sure you need to access coordinates 774,503 of a 81x22 image?

  • @Andersoncarloswoss On the web these are the coordinates that was just the image to show where I wanted to take the color from in this yellow case!

  • Take some of the code you’ve tried. Is the image on a website? Do you already have the image URL? Without this information you can’t answer.

  • @jsbueno edited my post warps to try to exclaim all doubts!

  • Which operating system?

  • Hi, Deadsec, would it not be feasible, you perform a screen printscreen on time with python, and then simply pick up the pixel ? Sorry I didn’t post an answer, is that I’m at work. :)

  • @Davimello I ended up discovering that I could not do the bot as I wanted because the python can not get the color of the pixel because the layer suprior is white !

Show 2 more comments

1 answer

0

import pyautogui

pixelColor = pyautogui.screenshot().getpixel((510, 687))

#converte RGB para HEX (desnecessário se você precisa apenas do código RGB)
pixelColor = '#%02x%02x%02x' % (pixelColor)

print(pixelColor)

Where 510 is the x pixel position
Where is the 687 is the y pixel position

You can also create 2 variables before the 3rd line:

x = 510
y = 687

And change the values from within the getpixel() function to (x, y)

pixelColor = pyautogui.screenshot().getpixel((x, y))

To get the x and y coordinates from where the mouse is positioned, use:

pyautogui.position()
  • The pyautogui documentation can be found at the following link https://pyautogui.readthedocs.io/en/latest/. It has the Installation section with the command to install in each OS

Browser other questions tagged

You are not signed in. Login or sign up in order to post.