如何使用 python 获取图像的像素坐标
how to get the pixel coordinate of image using python
所以问题有点改变:
我有这段代码:
import re
from PIL import Image
def rgb_to_hex(rgb_color):
[r, g, b] = rgb_color
assert 0 <= r <= 255
assert 0 <= g <= 255
assert 0 <= b <= 255
r = hex(r).lstrip('0x')
g = hex(g).lstrip('0x')
b = hex(b).lstrip('0x')
r = (2 - len(r)) * '0' + r
g = (2 - len(g)) * '0' + g
b = (2 - len(b)) * '0' + b
hex_color = '#' + r + g + b
return hex_color
img = Image.open('img.png')
pix_val = list(img.getdata())
x, y = img.size
a = 0
for element in pix_val:
element = list(element)
del element[-1]
print(rgb_to_hex(element))
a += 1
if a == x:
a = 0
print("")
这段代码的作用是打开一个图像文件并读取它的数据,然后逐列打印像素或特定行和列的十六进制代码
所以我想要的是我还想打印像素的坐标。
例如我有这个image
所以我想要我正在打印其像素值的像素的坐标。
请帮帮我
感谢您提前回答
你可以试试这个:
import re
from PIL import Image
def rgb_to_hex(rgb_color):
[r, g, b] = rgb_color
assert 0 <= r <= 255
assert 0 <= g <= 255
assert 0 <= b <= 255
r = hex(r).lstrip('0x')
g = hex(g).lstrip('0x')
b = hex(b).lstrip('0x')
r = (2 - len(r)) * '0' + r
g = (2 - len(g)) * '0' + g
b = (2 - len(b)) * '0' + b
hex_color = '#' + r + g + b
return hex_color
img = Image.open('img.png')
pix_val = list(img.getdata())
x, y = img.size
a = 0
for element in pix_val:
element = list(element)
del element[-1]
print(rgb_to_hex(element))
# this line of code here:
print(f"x:{a%x} y:{int(a/x)}")
a += 1
您也可以使用 python 3.6 中引入的 fstring,例如:
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}")
输出:
0 0 #4777b9
0 1 #4878ba
0 2 #4a77ba
0 3 #4a75b9
0 4 #4b73b8
0 5 #4d75ba
...
参考:
- Converting a RGB color tuple to a six digit code, in Python - Stack Overflow
- What’s New In Python 3.6 — Python 3.9.1 documentation
- python - Getting list of pixel values from PIL - Stack Overflow
所以问题有点改变:
我有这段代码:
import re
from PIL import Image
def rgb_to_hex(rgb_color):
[r, g, b] = rgb_color
assert 0 <= r <= 255
assert 0 <= g <= 255
assert 0 <= b <= 255
r = hex(r).lstrip('0x')
g = hex(g).lstrip('0x')
b = hex(b).lstrip('0x')
r = (2 - len(r)) * '0' + r
g = (2 - len(g)) * '0' + g
b = (2 - len(b)) * '0' + b
hex_color = '#' + r + g + b
return hex_color
img = Image.open('img.png')
pix_val = list(img.getdata())
x, y = img.size
a = 0
for element in pix_val:
element = list(element)
del element[-1]
print(rgb_to_hex(element))
a += 1
if a == x:
a = 0
print("")
这段代码的作用是打开一个图像文件并读取它的数据,然后逐列打印像素或特定行和列的十六进制代码
所以我想要的是我还想打印像素的坐标。
例如我有这个image 所以我想要我正在打印其像素值的像素的坐标。
请帮帮我
感谢您提前回答
你可以试试这个:
import re
from PIL import Image
def rgb_to_hex(rgb_color):
[r, g, b] = rgb_color
assert 0 <= r <= 255
assert 0 <= g <= 255
assert 0 <= b <= 255
r = hex(r).lstrip('0x')
g = hex(g).lstrip('0x')
b = hex(b).lstrip('0x')
r = (2 - len(r)) * '0' + r
g = (2 - len(g)) * '0' + g
b = (2 - len(b)) * '0' + b
hex_color = '#' + r + g + b
return hex_color
img = Image.open('img.png')
pix_val = list(img.getdata())
x, y = img.size
a = 0
for element in pix_val:
element = list(element)
del element[-1]
print(rgb_to_hex(element))
# this line of code here:
print(f"x:{a%x} y:{int(a/x)}")
a += 1
您也可以使用 python 3.6 中引入的 fstring,例如:
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}")
输出:
0 0 #4777b9
0 1 #4878ba
0 2 #4a77ba
0 3 #4a75b9
0 4 #4b73b8
0 5 #4d75ba
...
参考:
- Converting a RGB color tuple to a six digit code, in Python - Stack Overflow
- What’s New In Python 3.6 — Python 3.9.1 documentation
- python - Getting list of pixel values from PIL - Stack Overflow