[英]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中,您可以使用以下步骤:
convert your list to numpy array 将您的列表转换为numpy数组
import numpy as np 将numpy导入为np
myarray = np.asarray(mylist) myarray = np.asarray(mylist)
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: 我会这样使用
bash
和ImageMagick
:
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 processing 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.