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

Question:

I have a list of approx. 1000 hex colors which I would like to convert into an image with (e.g. a grid of squares or rectangles) filled with these colors. Is there an easy way to achieve this in Imagemagick (or any other software: e.g. Processing/Python).

Thanks for your help

Asked By: digit

||

Answers:

Code is in python
You can use the following steps:

  1. convert your list to numpy array
    import numpy as np 

    myarray = np.asarray(mylist)
  1. Use scipy to save the numpy array you have just created
    from scipy.misc import toimage

    rgb = scipy.misc.toimage(myarray)

    toimage(rgb).show()

Note: Scipy requres PIL to be installed pre-hand.

Another Solution without Scipy is as follows, but you need to modify according to your needs. you will need PIL here:

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')
Answered By: Saurav

You’ve tagged this with the tag, so here is the Processing solution:

Step 1: Load the file. You can use the loadStrings() functions for this. This gives you an array of String values, which in your case will hold your hex values. More info can be found in the reference.

Step 2: Loop through those hex values. Use a regular for loop or an enhanced for loop.

Step 3: Convert each hex String into an int color using the unhex() function. This gives you an int that can be passed into any color function, such as fill(). More info can be found in the reference.

Step 4: Use those colors to draw your image. 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. Good luck.

Answered By: Kevin Workman

I would use bash and ImageMagick like this:

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:

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

you will get this:

enter image description here

If you want the squares bigger than their current size of 1×1, just change the convert command to specify the size of the square, to say 10×10:

while read h; do 
   convert -size 10x10 xc:"$h" miff:- 
done < colours | montage -geometry +0+0 miff:- result.png
Answered By: Mark Setchell
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.