Trying to generate files from a list

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
farbewerk
Posts: 11
Joined: 2013-07-02T12:42:24-07:00
Authentication code: 6789

Trying to generate files from a list

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trying to generate files from a list

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Trying to generate files from a list

Post 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.
farbewerk
Posts: 11
Joined: 2013-07-02T12:42:24-07:00
Authentication code: 6789

Re: Trying to generate files from a list

Post 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?
farbewerk
Posts: 11
Joined: 2013-07-02T12:42:24-07:00
Authentication code: 6789

Re: Trying to generate files from a list

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to generate files from a list

Post 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}
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Trying to generate files from a list

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to generate files from a list

Post 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.
farbewerk
Posts: 11
Joined: 2013-07-02T12:42:24-07:00
Authentication code: 6789

Re: Trying to generate files from a list

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to generate files from a list

Post 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
farbewerk
Posts: 11
Joined: 2013-07-02T12:42:24-07:00
Authentication code: 6789

Re: Trying to generate files from a list

Post by farbewerk »

Do I need a shebang line on that bash script and then chmod to run it?
farbewerk
Posts: 11
Joined: 2013-07-02T12:42:24-07:00
Authentication code: 6789

Re: Trying to generate files from a list

Post by farbewerk »

Also, the color list is pretty large. Can I read the list from a file into the list variable?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trying to generate files from a list

Post 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
Last edited by fmw42 on 2013-07-03T09:25:05-07:00, edited 1 time in total.
farbewerk
Posts: 11
Joined: 2013-07-02T12:42:24-07:00
Authentication code: 6789

Re: Trying to generate files from a list

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trying to generate files from a list

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply