问题描述
使用CLI,我想向图像中添加一个黑色矩形,该图像的中心是1)居中,每个边缘2)X像素.
Using the CLI, I'd like to add a black rectangle to an image that is 1) centered and 2) X pixels from every edge.
基本上,我几乎想要-border
的反面(而不是添加某种颜色的边框,我想将图像的X像素保留为边框,然后将其他所有内容都涂成黑色.此外,图像大小不应更改).
Basically, I almost want the opposite of -border
(instead of adding a border of a certain color, I'd like to keep X pixels of my image as border, then paint everything else inside black. Also, image size should not change).
我希望能工作但没能解决的事情:
Something that I expected to work but didn't was:
convert myImage.jpg -fill black -draw "rectangle 10,10 %[fx:w-20],%[fx:h-20]" outImage.jpg
...对于10个像素的边框".看来-draw
不接受FX运算符或属性,尽管我在某个地方找到了(例如在)应该在IM 7.x中使用(我正在使用).
...for a "border" of 10 pixels. It looks like -draw
doesn't accept FX operators or attributes, although I found somewhere (like in How can I use imagemagick attributes in the draw rectangle command?) that it should in IM 7.x (which is what I'm using).
推荐答案
在ImageMagick 7中,您应该使用magick而不是进行转换.使用convert将调用不支持fx内联操作的ImageMagick 6.在ImageMagick 7中,以这种方式对围绕黑色矩形的10像素图像边框进行处理:
In ImageMagick 7, you should use magick and not convert. Using convert will make calls to ImageMagick 6 which does not support the fx in-line operations. In ImageMagick 7, do it this way for a 10 pixel border of image about the black rectangle:
输入:
magick lena.jpg -fill black -draw "rectangle 10,10 %[fx:w-11],%[fx:h-11]" result.jpg
请注意,应该使用11而不是10,因为图像中的最后一个像素是w-1,h-1.编号从0开始.因此,我已经更正了上面的原始答案.
Note that one should use 11 rather than 10, since the last pixel in the image is w-1,h-1. Numbering starts at 0. So I have corrected my original answer above.
这篇关于使用ImageMagick绘制居中的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!