首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用转换将一个图像插入另一个图像

使用转换将一个图像插入另一个图像
EN

Stack Overflow用户
提问于 2016-07-20 09:01:12
回答 1查看 3K关注 0票数 7

我有一个大小为logo.png720x720图像,我希望它被拉伸或压缩,以适应整个高度或宽度,保持高宽比,在一个有界的矩形top-left: 32,432bottom-right: 607,919中,在另一个640x960大小的图像background.png图像中。

因此,对于上面的示例,logo.png将被调整为488x488,并定位于top-left: 76,432

但我不想计算488x48876,432,只想使用上面的top-leftbottom-right说明符,即让ImageMagick来计算出来。

ImageMagick能做这样的事情吗?如果它不能单独使用,是否有使用convert和其他任何东西的脚本解决方案?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-22 13:53:27

希望更简单的版本

我认为这仍然会产生同样的结果,但更简单:

代码语言:javascript
运行
复制
#!/bin/bash

# Make initial images
convert -size 720x720! gradient:red-yellow -fill white -gravity center -pointsize 72 -annotate 0 "logo" logo.png
convert -size 640x960! gradient:blue-cyan  -fill white -gravity north  -pointsize 72 -annotate 0 "background" background.png

# Specify top-left and bottom-right
tl="32,432"
br="607,919"

# Get x1,y1,x2,y2 - bounding box of inserted image
IFS=, read -r x1 y1 <<< "$tl"
IFS=, read -r x2 y2 <<< "$br"

# Work out width and height 
w=$((x2-x1+1))
h=$((y2-y1+1))

# Resize logo proportionally, then extend canvas with invisible pixels to full size of insertion area and composite onto background
convert background.png \( logo.png -resize ${w}x${h} -background none -gravity center -extent ${w}x${h} \) -gravity northwest -geometry +${x1}+${y1} -composite result.png

改进的应答

这是更简单、更快的结果,希望结果是一样的:

代码语言:javascript
运行
复制
#!/bin/bash

# Make initial images
convert -size 720x720! gradient:red-yellow -fill white -gravity center -pointsize 72 -annotate 0 "logo" logo.png
convert -size 640x960! gradient:blue-cyan  -fill white -gravity north  -pointsize 72 -annotate 0 "background" background.png

# Specify top-left and bottom-right
tl="32,432"
br="607,919"

# Get x1,y1,x2,y2 - bounding box of inserted image
IFS=, read -r x1 y1 <<< "$tl"
IFS=, read -r x2 y2 <<< "$br"

# Work out w and h, and smaller side "s"
w=$((x2-x1+1))
h=$((y2-y1+1))
s=$w
[ $h -lt $w ] && s=$h
echo Smaller side: $s

# Resize logo proportionally, then extend canvas with invisible pixels to full size of insertion area and place on background
convert background.png \( logo.png -resize ${s}x${s} -background none -gravity center -extent ${w}x${h} \) -gravity northwest -geometry +${x1}+${y1} -composite result.png

原始答案

我认为,从你的评论,你想要调整大小的形象为中心,所以我做到了。

另外,还有很多调试代码,直到我知道自己在正确的轨道上时,我才对其进行优化,所以它肯定可以改进。

代码语言:javascript
运行
复制
#!/bin/bash

# Make initial images
convert -size 720x720! gradient:red-yellow -fill white -gravity center -pointsize 72 -annotate 0 "logo" logo.png
convert -size 640x960! gradient:blue-cyan  -fill white -gravity north  -pointsize 72 -annotate 0 "background" background.png

# Specify top-left and bottom-right
tl="32,432"
br="607,919"

# Get x1,y1,x2,y2 - bounding box of inserted image
IFS=, read -r x1 y1 <<< "$tl"
IFS=, read -r x2 y2 <<< "$br"

# Work out w and h, and smaller side "s"
w=$((x2-x1+1))
h=$((y2-y1+1))
s=$w
[ $h -lt $w ] && s=$h
echo Smaller side: $s

# Work out size of resized image
read -r a b < <(convert logo.png -resize ${s}x${s} -format "%w %h" info:)
echo Resized logo: $a x $b

# Work out top-left "x" and "y"
x=$((x1+((w-a)/2)))
y=$((y1+((h-b)/2)))
echo x:$x, y:$y
convert background.png \( logo.png -resize ${s}x${s} +repage \) -geometry +${x}+${y} -composite result.png

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38476804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档