【问题标题】:How to create a PNG image with HTML hex color fill?如何使用 HTML 十六进制颜色填充创建 PNG 图像?
【发布时间】:2016-10-26 05:10:35
【问题描述】:

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

【问题讨论】:

  • 是什么阻止了您生成 rgb 颜色。

标签: python image colors


【解决方案1】:

请参阅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")

【讨论】:

  • 这似乎更像是一个评论,而不是一个答案。
  • 提供一个允许 OP 解决他们的问题的图像库在我看来算是帮助,但我已经添加了代码。
【解决方案2】:

如果你觉得这有用,请点赞:

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

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 2020-07-20
    • 2016-06-08
    • 1970-01-01
    • 2012-05-01
    • 2014-08-07
    • 2014-06-12
    • 1970-01-01
    • 2014-08-19
    相关资源
    最近更新 更多