2014-02-19 54 views
1

如何獲得圖像的最大寬度和高度,對其執行一些數學運算,然後在透視圖中使用它進行扭曲?ImageMagick - 在不知道圖像尺寸的情況下計算透視圖

我有一堆想要應用透視變形的圖像。

唯一的問題是,每個圖像是不同的大小。

此代碼適用於我知道尺寸(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'

回答

2

您可以使用ImageMagick的角度內置fx運營商爲您做數學,不涉及bash數學,bceval

像這樣:

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 
+0

哇!沒想到會得到答案。很好,謝謝 :-) –

相關問題