0

I have a file with that consists of color names and values

foo,(255, 212, 201),#FFD4C9
bar,(248, 201, 189),#F8C9BD
baz,(167, 145, 138),#A7918A

That I'd like to turn into 200px × 200px color swatches (i.e. just rectangles of that color) named foo.gif, bar.gif, etc. I've tried to do this with Wand in Python 3, but I'm having no luck.

SIZE = 200
with open("foo.txt") as f:
    for line in f:
        if line[0] != "#":
            (name, _, hex_color) = tuple(line.strip().split(","))
            hex_color = hex_color.lower()
            print("{}, {}".format(name, hex_color))

            image_name = "{}.gif".format(name)
            with Drawing() as draw:
                # set draw.fill_color here?
                draw.rectangle(left=0, top=0, width=SIZE, height=SIZE)
                with Image() as image:
                    draw(image)
                    image.format = 'gif'
                    image.save(filename=image_name)

gives me

Traceback (most recent call last):
  File "color_swatches.py", line 36, in <module>
    image.format = 'PNG'
  File "/usr/local/lib/python3.4/site-packages/wand/image.py", line 2101, in format
    raise ValueError(repr(fmt) + ' is unsupported format')
ValueError: 'gif' is unsupported format

I've also tried saving as jpeg, jpg, png, and PNG to no avail. Perhaps my doing this at a quarter to 4 in the morning is to blame.


edit: I was able to accomplish the task with the following bash script,

#!/bin/bash
while IFS=, read name _ hex
do
    convert -size 200x200 xc:white -fill $hex -draw "rectangle 0,0 200,200" \
        swatches/$name.gif
done < $1

but I'm still curious what I'm doing wrong with Wand. Based on an issue I had where omitting xc:<color> caused the bash script to fail, I thought that adding a line

image.background_color = Color("#fff")

after the with Image() as image: line might work, but alas, I get a new error:

Traceback (most recent call last):
  File "color_swatches.py", line 38, in <module>
    image.background_color = Color("#fff")
  File "/usr/local/lib/python3.4/site-packages/wand/image.py", line 419, in wrapped
    result = function(self, *args, **kwargs)
  File "/usr/local/lib/python3.4/site-packages/wand/image.py", line 1021, in background_color
    self.raise_exception()
  File "/usr/local/lib/python3.4/site-packages/wand/resource.py", line 218, in raise_exception
    raise e
wand.exceptions.WandError: b"wand contains no images `MagickWand-2' @ error/magick-image.c/MagickSetImageBackgroundColor/9541"
Ryan M
  • 647
  • 2
  • 6
  • 16

1 Answers1

1

The first error is a little misleading, but the second message is correct. Your Image() constructor allocates the wand object, but not a new image. The same way your bash script calls -size 200x200 you need to define the width= & height= in Image().

with Drawing() as draw:
  # set draw.fill_color here? YES
  draw.fill_color = Color(hex_color)
  draw.rectangle(left=0, top=0, width=SIZE, height=SIZE)
  with Image(width=SIZE,height=SIZE) as image:
    draw(image)
    image.format = 'gif'
    image.save(filename=image_name)
emcconville
  • 23,800
  • 4
  • 50
  • 66