ImageMagick - 在不知道图像尺寸的情况下计算透视图 [英] ImageMagick - calculate perspective without knowing image dimensions
问题描述
如何获取图像的最大宽度和高度,对其执行一些数学运算,然后在我的透视变形中使用它?
How can I get the maximum width and height of an image, perform some maths on it, then use it in my perspective distortion?
我有一堆我想要应用透视失真的图像。
I have a bunch of images to which I want to apply a perspective distortion.
唯一的问题是,每张图片的大小不同。
The only problem is, each image is a different size.
此代码适用于我知道尺寸(1440 * 900)的图像。
This code works on an image where I know the size (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
获得最大值 - 但我找不到倍增的方法那些数字。
I know I can get the maximum values by using %h
and %w
- but I can't find a way to multiply those numbers.
基本上,我想做的是定义这样的点:
Essentially, what I want to do is define the points like this:
-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'来调用透视图
For bonus points, I'd like to be able to call the perspective using -distort Perspective '@points.txt'
推荐答案
您可以使用ImageMagick的内置 fx
运算符为您做数学运算,而不涉及 bash
数学, bc
或 eval
。
You can use ImageMagick's built-in fx
operator to do maths for you, without involving bash
mathematics, bc
or 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
哦,对于那些奖励积分......; - )
Oh, for those bonus points... ;-)
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
这篇关于ImageMagick - 在不知道图像尺寸的情况下计算透视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!