gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:23:02 24 4
gpt4 key购买 nike

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

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

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

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

有人知道怎么做吗?

最佳答案

您可以像这样获取像素 ar 左上角(坐标: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 开始锻炼.

关于imagemagick - 使用 ImageMagick 调整图像大小并设置背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27954017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com