使用ImageMagick调整图像大小并设置背景颜色

时间:2015-01-14 22:56:59

标签: imagemagick extend

我想用ImageMagick将100x200图像调整为新的400x400图像。

到目前为止,我有以下命令:

convert in.png -resize^ 400x400 -compose Copy -gravity center -extent 400x400 out.png

现在我想从in.png的顶部左侧像素中读取颜色,并将其设置为out.png的背景颜色。

有人知道怎么做吗?

3 个答案:

答案 0 :(得分:7)

您可以像这样获取像素左上角(坐标:0,0)的颜色:

convert in.png -colorspace rgb -format "%[pixel:p{0,0}]" info:

<强>输出:

rgb(201,200,206)

如果您使用的是OSX / Linux / Unix,则可以在变量中捕获它并使用它来设置背景:

c=$(convert in.png -colorspace rgb -format "%[pixel:p{0,0}]" info:)
convert in.png -background "$c" ...

所以,如果我们从这张图片开始:

enter image description here

并执行此操作:

c=$(convert in.png -colorspace rgb -format "%[pixel:p{0,0}]" info:)

echo $c           # Check that puppy's colour
rgb(255,0,0)      # Yep, it's red

convert -background "$c" in.png -resize 400x400 -gravity center -extent 400x400 out.png

我们会得到这个:

enter image description here

如果您不幸遇到Windows,则必须使用FOR /F执行一些无法理解的内容,而这些内容需要您here自行解决。

答案 1 :(得分:0)

我用它来获取左上角像素的背景颜色,从动画gif的第一帧开始

convert&#39; original.gif [0]&#39; -colorspace rgb -crop 1x1 + 0 + 0 text:

答案 2 :(得分:-1)

使用-background选项设置背景颜色:

convert in.png -resize 400x400 -background black -compose Copy -gravity center -extent 400x400 out.png