【发布时间】:2016-10-26 05:10:35
【问题描述】:
我有一个生成随机十六进制代码的脚本,我想生成一个图像,生成的颜色作为图像的填充。我查看了 pylab,但无法获得我想要的结果。有人可以帮我弄清楚如何做到这一点吗?
【问题讨论】:
-
是什么阻止了您生成 rgb 颜色。
我有一个生成随机十六进制代码的脚本,我想生成一个图像,生成的颜色作为图像的填充。我查看了 pylab,但无法获得我想要的结果。有人可以帮我弄清楚如何做到这一点吗?
【问题讨论】:
请参阅https://pypi.python.org/pypi/Pillow 以创建图像文件。
from PIL import Image
webhexcolor = "#4878A8"
im = Image.new("RGB", (100,100), webhexcolor)
im.save( "color.png")
【讨论】:
如果你觉得这有用,请点赞:
#Hex to RGB
def hex_to_rgb(value):
value = value.lstrip('#')
lv = len(value)
return tuple(int(value[i:i+lv//3], 16) for i in range(0, lv, lv//3))
print(hex_to_rgb("#0022ff"))
#https://www.codespeedy.com/create-random-hex-color-code-in-python/
import random
import cv2
def generate_random_hex():
random_number = random.randint(0,16777215)
hex_number = str(hex(random_number))
hex_number ='#'+ hex_number[2:]
print('A Random Hex Color Code is :',hex_number)
return hex_number
width1, height1 = 300, 300
random_hex_code=generate_random_hex()
random_rgb_code=hex_to_rgb(random_hex_code)
image = create_blank(width1, height1, rgb_color=random_rgb_code)
cv2.imwrite('random'+random_hex_code+'.jpg', image)
【讨论】: