2012-05-24 39 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来做。