0

I my application users are able to upload photos. Sometime, I want them to hide some information of the picture, for instance the registration plate of a vehicle, or the personal address of an invoice.

To meet that need I plan to pixelate a portion of the image. How can I pixelate an image in such a way given the coordinates of the area to hide and the size of the area.

I found out how to pixelate (by scaling the image down and up) but how can I only target an area of the image?

The area is specified by two pairs of coordinates (x1, y1, x2, y2), or a pair of coordinates and dimensions (x, y, width, height).

2 Answers 2

1

I am at work at the moment so can not test any code. I would see if you could work with -region or else use a mask. Copy the image and pixelate the whole image create a mask of the area required, cut a hole in the original image with the mask and overlay it over the pixelated image.

Example of masking an image

You could modify this code ( quite old and could probably be improved on ):

// 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

Thanks for your answer, Bonzo.

I found a way to achieve what I want with ImageMagick convert command. It's a 3-steps process:

  1. I create a pixelated version of the whole source image.
  2. I then build a mask using the original image (to keep the same size) filled with black (with gamma 0) then I draw blank rectangle where I want unreadable areas.
  3. Then I merge the three images (original, pixelated and mask) in a composite operation.

Here is an example with 2 areas (a et b) pixelated.

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

It works like a charm. Now I will do it with RMagick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.