Python - 使用HTML十六进制颜色填充创建PNG图像

时间:2016-06-23 18:08:24

标签: python html colors hex fill

我有一个生成随机十六进制代码的脚本,从中我想生成一个图像,生成的颜色作为图像的填充。我看了pylab,但无法得到我想要的结果。有人可以帮我弄清楚如何做到这一点吗?

2 个答案:

答案 0 :(得分:2)

请参阅https://pypi.python.org/pypi/Pillow以创建图像文件。

关注https://pillow.readthedocs.io/en/3.2.x/reference/ImageDraw.html#example-draw-a-gray-cross-over-an-image

from PIL import Image
webhexcolor = "#4878A8"
im = Image.new("RGB", (100,100), webhexcolor)
im.save( "color.png")

答案 1 :(得分:0)

如果觉得有用,请投票:

#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)