简体   繁体   English

如何将存储在文本文件中的十六进制颜色(或RGB,sRGB)列表转换为图像

[英]How can I convert a list of hex colors (or RGB, sRGB) stored in a text file into an Image

I have a list of approx. 我有一个清单。 1000 hex colors which I would like to convert into an image with (eg a grid of squares or rectangles) filled with these colors. 我想将1000种十六进制颜色转换为带有这些颜色的图像(例如正方形或长方形的网格)。 Is there an easy way to achieve this in Imagemagick (or any other software: eg Processing/Python). 是否有一种简单的方法可以在Imagemagick(或任何其他软件:Processing / Python)中实现此目的。

Thanks for your help 谢谢你的帮助

Code is in python You can use the following steps: 代码在python中,您可以使用以下步骤:

  1. convert your list to numpy array 将您的列表转换为numpy数组

    import numpy as np 将numpy导入为np

    myarray = np.asarray(mylist) myarray = np.asarray(mylist)

  2. Use scipy to save the numpy array you have just created 使用scipy保存刚刚创建的numpy数组

    from scipy.misc import toimage 从scipy.misc导入到图像

    rgb = scipy.misc.toimage(myarray) rgb = scipy.misc.toimage(myarray)

    toimage(rgb).show() toimage(rgb).show()

Note: Scipy requres PIL to be installed pre-hand. 注意:Scipy需要预先安装PIL。

Another Solution without Scipy is as follows, but you need to modify according to your needs. 另一个没有Scipy的解决方案如下,但是您需要根据需要进行修改。 you will need PIL here: 您将在这里需要PIL:

import Image
import numpy as np

data = np.random.random((500,500))

#Rescale to 0-255 and convert to uint8
rescaled = (255.0 / data.max() * (data - data.min())).astype(np.uint8)

im = Image.fromarray(rescaled)
im.save('testing.png')

I would use bash and ImageMagick like this: 我会这样使用bashImageMagick

while read h; do convert xc:"$h" miff:- ; done < colours | montage -geometry +0+0 miff:- result.png

So, if your file colours looks like this: 因此,如果您的文件colours如下所示:

#000000
#ffffff
#ff0000
#00ff00
#0000ff
pink
yellow
navy
rgb(128,128,128)
rgb(64,64,64)
rgb(200,200,200)

you will get this: 你会得到这个:

在此处输入图片说明

If you want the squares bigger than their current size of 1x1, just change the convert command to specify the size of the square, to say 10x10: 如果要使正方形大于其当前大小1x1,只需更改convert命令以指定正方形的大小即可,例如10x10:

while read h; do 
   convert -size 10x10 xc:"$h" miff:- 
done < colours | montage -geometry +0+0 miff:- result.png

You've tagged this with the tag, so here is the Processing solution: 您已使用标记对此进行了标记,因此这是处理解决方案:

Step 1: Load the file. 步骤1:加载文件。 You can use the loadStrings() functions for this. 您可以为此使用loadStrings()函数。 This gives you an array of String values, which in your case will hold your hex values. 这为您提供了一个String值数组,在您的情况下,它将保存您的十六进制值。 More info can be found in the reference . 参考资料中可以找到更多信息。

Step 2: Loop through those hex values. 步骤2:遍历那些十六进制值。 Use a regular for loop or an enhanced for loop. 使用常规的for循环或增强的for循环。

Step 3: Convert each hex String into an int color using the unhex() function. 步骤3:使用unhex()函数将每个十六进制String转换为int颜色。 This gives you an int that can be passed into any color function, such as fill() . 这为您提供了一个可以传递给任何颜色函数的int ,例如fill() More info can be found in the reference . 参考资料中可以找到更多信息。

Step 4: Use those colors to draw your image. 步骤4:使用这些颜色绘制图像。 You haven't said how the lines in the file map to a coordinate on-screen, so you'll have to do that mapping. 您还没有说过文件中的行如何映射到屏幕上的坐标,因此您必须进行映射。 Then just change the fill color and draw a rectangle at that coordinate. 然后只需更改填充颜色并在该坐标处绘制一个矩形即可。

It's hard to answer general "how do I do this" type questions other than to point you to the reference and tell you to break your problem down into smaller steps and just try something. 除了将您指向参考并告诉您将问题分解为更小的步骤并尝试尝试之外,很难回答一般的“我该怎么做”类型的问题。 Then if you get stuck on one of those specific steps, you can ask a more specific "I tried X, expected Y, but got Z instead" type question, which is more what Stack Overflow was designed for. 然后,如果您受困于这些特定步骤之一,则可以提出一个更具体的“我尝试过X,期望Y,但是却改为Z”类型的问题,这更多是Stack Overflow设计的目的。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PIL image.convert(“RGB”) 是否将图像转换为 sRGB 或 Adob​​eRGB? - Does PIL image.convert(“RGB”) convert images to sRGB or AdobeRGB? 将 RGB 颜色的熊猫数据框转换为十六进制 - convert a pandas dataframe of RGB colors to Hex 如何使用python将CSV文件转换为RGB图像 - How do I convert a CSV file to an RGB image using python 如何在 Python 中将 RGB 图像转换为灰度? - How can I convert an RGB image into grayscale in Python? 如何将 RGB 深度图像转换为点云? - How can I convert RGB depth image to Point Cloud? 如何将十六进制三元组转换为RGB元组并返回? - How do I convert a hex triplet to an RGB tuple and back? 如何检查 RGB colors 列表中的所有项目是否在没有循环的图像中? - How to check if all items in list of RGB colors is in an image without a loop? 如何使用melspectogram将wav文件转换为RGB图像? - How to convert a wav file into RGB image with melspectogram? TypeError: write() 参数必须是 str,而不是 list - 如何将列表转换为要存储的文本文件? - TypeError: write() argument must be str, not list - How do I convert a list into a text file to be stored? 如何使用 Python 有效地将像素数组 colors 转换为图像? - How can I convert an array of pixel colors into an image efficiently with Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM