[英]Get Hex Color Code & Coordinate of The Pixel in A Image
imagine i have a 3x3 image & numbers as it's pixel想象一下,我有一个 3x3 的图像和数字,因为它是像素
123
456
789
using python i want value of each pixel in hex-color code line by line, like using above example as a image if a run the script i should get the output something like:使用 python 我希望逐行使用十六进制颜色代码中的每个像素的值,例如使用上面的示例作为图像,如果运行脚本我应该得到 output 类似:
1st pixel (which is 1) - hex color code
2nd pixel (which is 2) - hex color code
3rd pixel (which is 3) - hex color code
so please help me how can i achieve this output Note: My image in most cases will not be more than 100 pixels所以请帮助我如何实现这个 output 注意:我的图像在大多数情况下不会超过 100 像素
I am Using Python3 & Debian based Linux Distro我正在使用基于 Python3 和 Debian 的 Linux 发行版
Thanks for answering in advance感谢您提前回答
Edit: What I Tried is编辑:我尝试的是
from PIL import Image
img = Image.open('img.png')
pixels = img.load()
width, height = img.size
for x in range(width):
for y in range(height):
r, g, b = pixels[x, y]
# in case your image has an alpha channel
# r, g, b, a = pixels[x, y]
print(x, y, f"#{r:02x}{g:02x}{b:02x}")
But this is not giving me correct values但这并没有给我正确的价值观
If you want to have iteration along the x-axis, you can write like:如果要沿 x 轴进行迭代,可以这样写:
from PIL import Image
img = Image.open('img.png')
pixels = img.load()
width, height = img.size
for y in range(height): # this row
for x in range(width): # and this row was exchanged
r, g, b = pixels[x, y]
# in case your image has an alpha channel
# r, g, b, a = pixels[x, y]
print(x, y, f"#{r:02x}{g:02x}{b:02x}")
Thinking of the for
statement, x and y are going to change like (x,y) = (0,0), (0,1),... in your initial code.考虑
for
语句,x 和 y 将在您的初始代码中像 (x,y) = (0,0), (0,1),... 一样发生变化。 You want to first iterate over x and then iterate over y;你想先迭代 x 然后迭代 y; you can write iteration over y, wrapping iteration over x.
您可以在 y 上编写迭代,在 x 上包装迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.