Answers for "how to get the color of a pixel in python"

2

python get pixel values from image

from PIL import Image

img = Image.open('example.png')
imgWidth, imgHeight = img.size
img = img.convert("RGBA")
imgdata = img.getdata()

x_pos = 0
y_pos = 1

pixel_value = []
x = []
y = []

for item in imgdata:
    if (x_pos) == imgWidth:
        x_pos = 1
        y_pos += 1
    else:
        x_pos += 1

    if item[3] != 0:
        pixel_value.append(item[2])
        x.append(x_pos)
        y.append(y_pos)

pixel_value, x, y = zip(*sorted(zip(pixel_value, x, y), reverse=True))

print(f'{pixel_value}n{x}n{y}')
Posted by: Guest on October-04-2020
0

how to get RGB value from pixel in screen live python

import PIL.ImageGrab
rgb = PIL.ImageGrab.grab().load()[x,y]
Posted by: Guest on August-19-2020
0

python get pixel color

from PIL import Image

im = Image.open('dead_parrot.jpg') # Can be many different formats.
pix = im.load()
print im.size  # Get the width and hight of the image for iterating over
print pix[x,y]  # Get the RGBA Value of the a pixel of an image
pix[x,y] = value  # Set the RGBA Value of the image (tuple)
im.save('alive_parrot.png')  # Save the modified pixels as .png
Posted by: Guest on August-14-2021

Code answers related to "how to get the color of a pixel in python"

Python Answers by Framework

Browse Popular Code Answers by Language