I have 2 images,
first is of news paper and second is of a person's face.
I am writing a php program, purpose of that is to fix a person's face in news paper. below is the news paper image.
I am getting perosn's profile pic through an api, sometime it is a little bit bigger than white place holder and sometime smaller (Have no control over that)
I tested Image perspective feature in Gimp 2.8 and fixed the profile pic in white place holder perfectly
I have tried Imagick
class' distortImage() method with compositeImage() method to rotate and profile pic and fix it in news paper, but problem is distortImage()
method
increases size of a profile pic randomly.
I have also tried SimpleImage class, used rotate()
and overlay()
methods to achieve the goal, but faced same problem that profile pic's size gets increased and eventually have to deal with some hard coding.
In above both approach I need to hard code start positions (however it should always be top left corner of place holder).
There are more than 100 images of news paper with image place holder, the position of place holder gets changes from one image to another (they are basically frames of one small video)
Now the question is, is it possible to do this through gimp batch mode ?? then want to write script to achieve this task.
I have tried to find documentation online to know how to write script for image perspective function in gimp batch, but didn't reach to perfect place :(
Kindly guide accordingly.
If any other image processing tool is available on Ubuntu, which facilitates to write script and let required operation done, then I can use that image processing tool/software as well.
Code
<?php
/* read co ordinates as csv from file and stored in $co_ordinates */
$XYaxies = explode(",", $co_ordinates[$i]);
$x1=(int)$XYaxies[0];
$y1=(int)$XYaxies[1];
$x2=(int)$XYaxies[2];
$y2=(int)$XYaxies[3];
$x3=(int)$XYaxies[4];
$y3=(int)$XYaxies[5];
$x4=(int)$XYaxies[6];
$y4=(int)$XYaxies[7];
/* these are coordinates of white image place holder */
/* x1,y1 top left corner */
/* x2,y2 top right corner */
/* x3,y3 bottom right corner */
/* x4,y4 bottom left corner */
$profile_pic_path = "face.png";
$frame_path="frame1.png";
$target_frame_path = "result_framr1.png";
$im = new Imagick($profile_pic_path);
$im->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$im->setImageMatte(true);
$profile_x1=$x1; $profile_x4=$x1;
$profile_x2=$x1+$profile_pic_width;
$profile_x3=$profile_x2;
$profile_y1=$y1; $profile_y2=$y1;
$profile_y3=$profile_y2+$profile_pic_height;
$profile_y4=$profile_y3;
/* Control points for the distortion */
$controlPoints=array(
0,0, ($x1-$profile_x1),($y1-$profile_y1) , # top left
$im->getImageWidth(),0, $im->getImageWidth()-($x2-$profile_x2),($y2-$profile_y2), # top right
0,$im->getImageHeight(),($x4-$profile_x4),$im->getImageHeight()-($y4-$profile_y4), # bottom LEFT
$im->getImageHeight(),$im->getImageWidth(),$im->getImageHeight()-($y3-$profile_y3),$im->getImageWidth()-($x3-$profile_x3)# bottum RIGHT
);
$im->distortImage(Imagick::DISTORTION_PERSPECTIVE, $controlPoints, true);
$background = new Imagick($frame_path);
$background->writeImage($target_frame_path);
?>
Thanks in advance.