如何获取图像的最大宽度和高度,对其执行一些数学运算,然后在我的透视变形中使用它?
我有一堆想要应用透视失真的图像。
唯一的问题是,每张图片的大小都不同。
此代码适用于我知道尺寸(1440 * 900)的图像。
convert test.jpg -matte \
-virtual-pixel transparent \
-distort Perspective '0,0 75,0 \
0,900 0,450 \
1440,0 1440,200 \
1440,900 1200,900' \
distorted.jpg
我知道我可以使用%h
和%w
来获取最大值 - 但我无法找到将这些数字相乘的方法。
基本上,我想要做的是定义这样的点:
-distort Perspective '0,0 75,0 \
0,%h 0,(%h/2) \
%w,0 %w,200 \
%w,%h (%w*0.75),%h'
对于奖励积分,我希望能够使用-distort Perspective '@points.txt'
答案 0 :(得分:2)
您可以使用ImageMagick的内置fx
运算符为您做数学运算,而不涉及bash
数学,bc
或eval
。
像这样:
persp=$(convert image.jpg -format "0,0 75,0 0,%h 0,%[fx:int(h/2)] %w,0,%w,200 %w,%h %[fx:int(w*0.75)],%h" info:)
echo $persp
0,0 75,0 0,900 0,450 1440,0,1440,200 1440,900 1080,900
然后做:
convert image.jpg ... -distort Perspective "$persp" ... distorted.jpg
哦,对于那些奖励积分......; - )
convert image.jpg -format "0,0 75,0 0,%h 0,%[fx:int(h/2)] %w,0,%w,200 %w,%h %[fx:int(w*0.75)],%h" info: > points.txt
convert image.jpg ... -distort Perspective @points.txt distorted.jpg