我有一个包含颜色名称和值的文件
foo,(255, 212, 201),#FFD4C9
bar,(248, 201, 189),#F8C9BD
baz,(167, 145, 138),#A7918A
我想变成 200px × 200px 的色板(即只是那种颜色的矩形),命名为foo.gif
,bar.gif
等。我尝试Wand
在 Python 3 中这样做,但我没有运气。
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)
给我
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
我也尝试另存为jpeg
, jpg
, png
, 并PNG
无济于事。也许我在凌晨四点半做这件事是罪魁祸首。
编辑:我能够使用以下bash
脚本完成任务,
#!/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
但我仍然很好奇我做错了什么Wand
。基于我遇到的省略xc:<color>
导致bash
脚本失败的问题,我认为添加一行
image.background_color = Color("#fff")
行之后with Image() as image:
可能会起作用,但是,我收到一个新错误:
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"