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
Answers:
Code is in python
You can use the following steps:
- convert your list to numpy array
import numpy as np
myarray = np.asarray(mylist)
- 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')
You’ve tagged this with the processing 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.
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:
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
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
Code is in python
You can use the following steps:
- convert your list to numpy array
import numpy as np
myarray = np.asarray(mylist)
- 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')
You’ve tagged this with the processing 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.
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:
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