在ImageMagick中使用Stacks选择性地模糊部分图像

时间:2022-09-27 08:59:21

I'm trying to annotate some text onto a base image with a dropshadow. I don't like the results I get from using the -shadow option, so I'm putting down the text, blurring it, then putting down the text again in white, a few pixels offset from the shadow. Here's the command I'm using:

我试图用一些阴影将一些文本注释到基本图像上。我不喜欢使用-shadow选项获得的结果,所以我放下文本,模糊它,然后再次将文本放在白色,偏离阴影的几个像素。这是我正在使用的命令:

convert base_image.jpg \
        -font TT0590M_.ttf \
        -fill gray30 \
        -annotate +0+0 '' -gravity North \
        -annotate +72+32 'ABCDEFGHIJKLM' \
        -blur 0x4 \
        -fill white \
        -annotate +72+27 'ABCDEFGHIJKLM' \
        combined.png

My problem is that the -blur option is blurring not only the first layer of text, but the underlying base image as well. I only want the first layer of text to blur, not the base image.

我的问题是-blur选项不仅模糊了第一层文本,还模糊了底层基本图像。我只希望第一层文本模糊,而不是基本图像。

I read up a little on using stacks and tried to isolate the first layer of text and the blur command using \( \) around that part, such as in the following:

我读了一些关于使用堆栈的内容,并尝试使用\(\)围绕该部分隔离第一层文本和模糊命令,如下所示:

convert base_image.jpg \
        -font TT0590M_.ttf \
        -fill gray30 \
        -annotate +0+0 '' -gravity North \
        \( -annotate +72+32 'ABCDEFGHIJKLM' \
        -blur 0x4 \) \
        -fill white \
        -annotate +72+27 'ABCDEFGHIJKLM' \
        combined.png

The results are the same - both the text shadow and the underlying base image are getting blured. I'm afraid I'm not having much luck understanding stacks or what other commands I should be using to get the effect I'm after.

结果是相同的 - 文本阴影和底层基础图像都变得模糊。我恐怕我没有太多的运气理解堆栈或我应该用什么其他命令来获得我想要的效果。

1 个解决方案

#1


As so often happens, I've continued to bang away at this since posting the question and I've managed to solve it on my own.

正如经常发生的那样,自从发布问题以来我一直在努力解决这个问题,并且我已经设法自己解决了这个问题。

The important change is that I started with a blank, transparent canvas instead of starting with the base image. After I get the text right, I insert the base image into the stack, swap the order of the two images in the stack, and then composite them with a compose type of "screen", which lays the one overtop of the other.

重要的变化是我开始使用空白透明画布而不是从基本图像开始。在我得到正确的文本之后,我将基本图像插入到堆栈中,交换堆栈中两个图像的顺序,然后将它们与组合类型的“屏幕”合成,这使得一个在另一个上方。

One other note of import: the -channel RGBA is needed so that the blurring works in conjunction with the transparency of the text layer, due to a peculiarity in the way that IM works. Why this is necessary is explained on this page.

另一个导入注意事项:需要-channel RGBA,以便模糊与文本图层的透明度结合使用,因为IM工作方式的特殊性。为什么这是必要的在本页解释。

Also, on Windows systems (.bat file instead of shell script), the single quotes need to be double quotes and the backslashes "\" need to be carets "^", or it all comes crashing down.

此外,在Windows系统上(.bat文件而不是shell脚本),单引号需要是双引号,反斜杠“\”需要是“^”,否则它们都会崩溃。

The script below is the final, working result (*nix version):

下面的脚本是最终的工作结果(* nix版本):

convert  \
    -size 500x500 xc:transparent \
    -font TT0590M_.ttf \
    -annotate +0+0 '' -gravity North \
    -fill gray30 \
    -annotate +75+35 'ABCDEFGHIJKLMNOPQR\nABCDEFGHIJKLMNOPQR' \
    -channel RGBA \
    -blur 0x4 \
    -fill white \
    -annotate +72+30 'ABCDEFGHIJKLMNOPQR\nABCDEFGHIJKLMNOPQR' \
    -insert 0 base_image.jpg \
    -swap 0,1 \
    -composite -compose screen \
    combined.png

#1


As so often happens, I've continued to bang away at this since posting the question and I've managed to solve it on my own.

正如经常发生的那样,自从发布问题以来我一直在努力解决这个问题,并且我已经设法自己解决了这个问题。

The important change is that I started with a blank, transparent canvas instead of starting with the base image. After I get the text right, I insert the base image into the stack, swap the order of the two images in the stack, and then composite them with a compose type of "screen", which lays the one overtop of the other.

重要的变化是我开始使用空白透明画布而不是从基本图像开始。在我得到正确的文本之后,我将基本图像插入到堆栈中,交换堆栈中两个图像的顺序,然后将它们与组合类型的“屏幕”合成,这使得一个在另一个上方。

One other note of import: the -channel RGBA is needed so that the blurring works in conjunction with the transparency of the text layer, due to a peculiarity in the way that IM works. Why this is necessary is explained on this page.

另一个导入注意事项:需要-channel RGBA,以便模糊与文本图层的透明度结合使用,因为IM工作方式的特殊性。为什么这是必要的在本页解释。

Also, on Windows systems (.bat file instead of shell script), the single quotes need to be double quotes and the backslashes "\" need to be carets "^", or it all comes crashing down.

此外,在Windows系统上(.bat文件而不是shell脚本),单引号需要是双引号,反斜杠“\”需要是“^”,否则它们都会崩溃。

The script below is the final, working result (*nix version):

下面的脚本是最终的工作结果(* nix版本):

convert  \
    -size 500x500 xc:transparent \
    -font TT0590M_.ttf \
    -annotate +0+0 '' -gravity North \
    -fill gray30 \
    -annotate +75+35 'ABCDEFGHIJKLMNOPQR\nABCDEFGHIJKLMNOPQR' \
    -channel RGBA \
    -blur 0x4 \
    -fill white \
    -annotate +72+30 'ABCDEFGHIJKLMNOPQR\nABCDEFGHIJKLMNOPQR' \
    -insert 0 base_image.jpg \
    -swap 0,1 \
    -composite -compose screen \
    combined.png