像素化图像的一部分以隐藏信息

时间:2012-05-24 06:49:37

标签: ruby imagemagick

我的应用程序用户可以上传照片。有时,我希望他们隐藏一些图片的信息,例如车辆的登记牌,或发票的个人地址。

为了满足这种需要,我计划像素化图像的一部分。考虑到要隐藏的区域的坐标和区域的大小,我如何以这种方式像素化图像。

我发现了如何像素化(通过向下和向上缩放图像),但我怎样才能仅定位图像的某个区域?

该区域由两对坐标(x1,y1,x2,y2)或一对坐标和尺寸(x,y,宽度,高度)指定。

2 个答案:

答案 0 :(得分: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 :(得分:1)

感谢您的回答,Bonzo。

我找到了一种通过ImageMagick convert命令实现我想要的方法。这是一个分为3个步骤的过程:

  1. 我创建了整个源图像的像素化版本。
  2. 然后我使用原始图像(保持相同的大小)使用黑色(使用gamma 0)构建一个蒙版,然后我绘制空白矩形,我想要不可读的区域。
  3. 然后我在合成操作中合并三个图像(原始,像素化和蒙版)。
  4. 以下是2个区域(a et 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来做。