Python Pixel Position: Get Image Coordinates

It is easy to access the pixel positions of an image using the PIL library in Python.

Firstly, you need to install the PIL library, which can be done using the following command:

pip install pillow

Next, you can use the following code to obtain the position of a specific pixel in the image.

from PIL import Image

# 打开图片
image = Image.open('image.jpg')

# 获取像素点位置
pixel = image.getpixel((x, y))

# 输出像素点位置
print(pixel)

In this case, ‘image.jpg’ is the name of the image file to be processed, and (x, y) is the coordinates of the pixel to be retrieved. The getpixel() method returns a tuple containing RGB values, for example (255, 255, 255) represents a white pixel.

Note: The origin of the coordinates is in the top left corner of the image, with the x-axis increasing horizontally to the right and the y-axis increasing vertically downwards.

bannerAds