Search code examples
pythonimagemagickprocessing

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 (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


Solution

  • 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 1x1, just change the convert command to specify the size of the square, to say 10x10:

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