Reputation: 13
I'm looking for a way to modify my code to convert the RGB pixel data into hexadecimal pixel data. Such as converting 37, 4, 201 to #2504C9. Code attached, if anyone knows how to do this, I'd really appreciate it! I've tried several methods from here but so far haven't been able to find much of anything that works.
from PIL import Image
import sys
im = Image.open(r"C:\Users\AChri\Desktop\boio.png")
px = im.load()
w, h = im.size
x = 0
y= 0
while True:
if x >= w:
x = 0
y= y+1
else:
if y >= h:
print('Done!')
sys.exit()
else:
val = px[x, y]
print(px[x, y])
x = x+1
Upvotes: 1
Views: 1320
Reputation: 1178
In px[x,y] you have a tuple (R, G, B), to print it as hex you can just do the following:
print("#{:02X}{:02X}{:02X}".format(*px[x,y]))
Or, to adapt it to your code:
val = "#{:02X}{:02X}{:02X}".format(*px[x,y])
print(val)
Upvotes: 2