Page 1 of 1

Trying to generate files from a list

Posted: 2013-07-02T12:49:59-07:00
by farbewerk
Hello all,

I'm trying to create around 900 small files from a list of hex codes. I have a YAML file (hexcode.yml) and am using the following code:

Code: Select all

#!/usr/bin/ruby
require "yaml"
 
	YAML.load_file("hexcode.yml").each do |name, color|
		p "Creating: #{name} (#{color}) => images/colors/#{name}.png"
		%x[ convert -size 2x2 xc:white -draw "fill white rectangle 0,0 2,2" -draw "fill #{color} rectangle 0,0 2,2" "images/colors/#{name}.png" ]

	end
On run, the terminal happily outputs the list:

"Creating: 000000 '#000000' 000080 '#000080' 00008B '#00008B' 00009C '#00009C' 0000CD '#0000CD' 0000EE '#0000EE' 0000FF '#0000FF', etc..."

and errors with:

Code: Select all

convert: unrecognized color `rectangle' @ warning/color.c/GetColorCompliance/947.
convert: non-conforming drawing primitive definition `fill' @ error/draw.c/DrawImage/3164.
I'm running this on OS X 10.8.x, IM version 6.8.0-10 2013-03-03 Q16

Any help would be appreciated.

Re: Trying to generate files from a list

Posted: 2013-07-02T13:25:36-07:00
by snibgo
Your command ...
rectangle 0,0 2,2
is wrong, because the coordinate (2,2) is outside your image. However, IM ignores this (it clips the rectangle).

The documentation (http://www.imagemagick.org/script/comma ... s.php#draw) doesn't say you can put "fill" inside the draw. However, this does seem to work. So the command:

Code: Select all

convert -size 2x2 xc:Red -draw "fill #0000FF rectangle 0,0 3,3" x.png
... works for me.

Perhaps you have a ruby problem, and the quotes need to be escaped, or something.

Re: Trying to generate files from a list

Posted: 2013-07-02T13:30:47-07:00
by GreenKoopa
Do you have an empty entry in your list? Your error
... unrecognized color `rectangle' ...
makes me think that
... -draw "fill #{color} rectangle ...
is collapsing into
... -draw "fill rectangle ...
and rectangle is not a valid color for fill.

Re: Trying to generate files from a list

Posted: 2013-07-02T14:21:30-07:00
by farbewerk
I think there is supposed to be an array created that points the name and color to the values in the lookup list. This is actually code lifted from this page:

http://www.starrhorne.com/2012/01/18/ma ... -rake.html

I emailed the author but decided to try and give it a go myself first as there isn't a lot of budget for this project.

Does the current code seem to be set up correctly to read the list into an array?

Re: Trying to generate files from a list

Posted: 2013-07-02T14:29:19-07:00
by farbewerk
Snibgo, the command you showed also worked for me. I think that the list isn't being passed correctly to the script. Anybody see anything?

Re: Trying to generate files from a list

Posted: 2013-07-02T14:40:39-07:00
by fmw42
"Creating: 000000 '#000000' 000080 '#000080' 00008B '#00008B' 00009C '#00009C' 0000CD '#0000CD' 0000EE '#0000EE' 0000FF '#0000FF', etc..."
Be consistent with your hex colors. You have several hex values without a #. When you make a list, I would recommend leaving off the single quotes and putting the resulting hex color in the command. You might also leave off all the #s and put that into your command. Note variables in unix would require a double quote to parse it or leave the quotes off.

In your case you already use

#{color}

so leave off all the #s in your list.

I do not use Ruby so do not know about their variables, but in unix, it would be #${color}

Re: Trying to generate files from a list

Posted: 2013-07-02T15:22:33-07:00
by GreenKoopa
farbewerk wrote: "Creating: 000000 '#000000' 000080 '#000080' 00008B '#00008B' 00009C '#00009C' 0000CD '#0000CD' 0000EE '#0000EE' 0000FF '#0000FF', etc..."
Is this really your output? That doesn't match the code or the author's output.

Try printing the ImageMagick command instead of executing it. Then we can isolate IM from Ruby and YAML. If your YAML list is long, try testing with a short list (maybe 2-4 colors).
fmw42 wrote: Be consistent with your hex colors...
I think it alternates between color/file names and color values.
fmw42 wrote:You might also leave off all the #s and put that into your command. ... I do not use Ruby so do not know about their variables, but in unix, it would be #${color}
I believe #{var} is how you read a variable within a string in Ruby.

Re: Trying to generate files from a list

Posted: 2013-07-02T16:55:17-07:00
by fmw42
GreenKoopa wrote:I think it alternates between color/file names and color values.
Thanks for clarifying. I missed that!

Since the names come from the hex values, it ought to be rather easy to have a list of just hex values or just names and extract the other via code.

Re: Trying to generate files from a list

Posted: 2013-07-02T17:21:32-07:00
by farbewerk
The list does alternate between name and code. So I can just put a column with the hex values in there. Question is, how do I get imagemagick to take the values in the list? Is there bash script I can put in there to do this? I don't know Ruby either so I'm using my basic programming knowledge to cobble everything together.

Re: Trying to generate files from a list

Posted: 2013-07-02T17:42:04-07:00
by fmw42
This is an example of taking values from a list and making 2x2 color rectangles. You can modify it further if you want a border of white around it or some other color. It is a simple bash unix set of commands to loop over each value in the list and create a 2x2 patch from the hex equivalent and then name it.


list="000000 FF0000 FF00FF FFFF00 00FFFF"
for color in $list; do
convert -size 2x2 xc:"#$color" $color.png
done

Re: Trying to generate files from a list

Posted: 2013-07-03T07:02:32-07:00
by farbewerk
Do I need a shebang line on that bash script and then chmod to run it?

Re: Trying to generate files from a list

Posted: 2013-07-03T08:08:31-07:00
by farbewerk
Also, the color list is pretty large. Can I read the list from a file into the list variable?

Re: Trying to generate files from a list

Posted: 2013-07-03T09:22:58-07:00
by fmw42
if you want to run from a script file, yes, add #/bin/bash

#/bin/bash
list=`cat fileofcolors.txt`
for color in $list; do
convert -size 2x2 xc:"#$color" $color.png
done

Depending upon whether the file is one string or each color on a separate line, you may need to put double quotes about $list

You could also have the fileofcolors.txt as an argument. Say you call this script, colors.sh, then


#/bin/bash
list=`cat $1`
for color in $list; do
convert -size 2x2 xc:"#$color" $color.png
done

colors.sh fileofcolors.txt

Re: Trying to generate files from a list

Posted: 2013-07-03T09:23:22-07:00
by farbewerk
Figured it out:

Code: Select all

#! /usr/bin/sh
list=$(cat hexcodes.txt)
	for color in $list; do

	convert -size 2x2 xc:"#$color" $color.png

done
IM is happily cranking away...

Thanks to all for the assistance.

Re: Trying to generate files from a list

Posted: 2013-07-25T16:38:08-07:00
by anthony
While this is not strictly imagemagick, but shell scripting, this is better...

Code: Select all

#! /usr/bin/sh
while read color; do
  convert -size 2x2 xc:"#$color" $color.png
done < hexcodes.txt
If your want to include the names of your colors, use multiple columns like this

Code: Select all

000000   Black
FF0000  Red
FF00FF Magenta
FFFF00 Cyan
Then script like this

Code: Select all

#! /usr/bin/sh
while read hexcode color; do
  convert -size 2x2 xc:"#$hexcode" -label "$color" -comment $hexcode  "$color.png"
done < hexcodes.txt
I not only named the filename with the color name, but included the colorname and hexcode in the image file itself. That way no matter what the image file is called (numbers or something else) you still have access to the original name and hexcode used to generate that image.

Color can be multiple words as it is the last 'read' variable from the line bash reads