2012-05-24 41 views
0

我的應用程序用戶可以上傳照片。有時,我希望他們隱藏圖片的一些信息,例如車輛的登記牌或發票的個人地址。將圖像的一部分像素化以隱藏信息

爲了滿足這種需求,我計劃對圖像的一部分進行像素化處理。如何以給定要隱藏區域的座標和區域大小的方式像素化圖像。

我發現如何像素化(通過縮放圖像),但我怎麼只能針對圖像的一個區域?

該區域由兩對座標(x1,y1,x2,y2)或一對座標和尺寸(x,y,寬度,高度)指定。

回答

1

我現在在工作,所以無法測試任何代碼。我會看看你是否可以使用-region或使用掩碼。 複製圖像並像素化整個圖像創建所需區域的遮罩,用蒙版在原始圖像中切出一個洞並將其覆蓋在像素化圖像上。

Example of masking an image

您可以修改這個代碼(很老很可能被改進):

// Get the image size to creat the mask 
// This can be done within Imagemagick but as we are using php this is simple. 
$size = getimagesize("$input14"); 

// Create a mask with a round hole 
$cmd = " -size {$size[0]}x{$size[1]} xc:none -fill black ". 
" -draw \"circle 120,220 120,140\" "; 
exec("convert $cmd mask.png"); 

// Cut out the hole in the top image 
$cmd = " -compose Dst_Out mask.png -gravity south $input14 -matte "; 
exec("composite $cmd result_dst_out1.png"); 

// Composite the top image over the bottom image 
$cmd = " $input20 result_dst_out1.png -geometry +60-20 -composite "; 
exec("convert $cmd output_temp.jpg"); 

// Crop excess from the image where the bottom image is larger than the top 
$cmd = " output_temp.jpg -crop 400x280+60+0 "; 
exec("convert $cmd composite_sunflower_hard.jpg "); 

// Delete tempory images 
unlink("mask.png"); 
unlink("result_dst_out1.png"); 
unlink("output_temp.jpg"); 
1

謝謝您的回答,BONZO。

我找到了一種方法來實現我想要的ImageMagick convert命令。這是一個3個步驟的過程:

  1. 我創建了整個源圖像的像素化版本。
  2. 然後,我使用原始圖像(保持相同大小)填充黑色(使用gamma 0)構建一個遮罩,然後在我想要不可讀區域繪製空白矩形。
  3. 然後我合併三個圖像(原始,像素和麪具)在一個複合操作。

下面是一個帶有2個區域(a和b)像素化的例子。

 
convert original.png -scale 10% -scale 1000% pixelated.png 
convert original.png -gamma 0 -fill white -draw "rectangle X1a, Y1a, X2a, Y2a" -draw "rectangle X1b, Y1b, X2b, Y2b" mask.png 
convert original.png pixelated.png mask.png -composite result.png 

它像一個魅力。現在我會用RMagick來做。