I'm using latest ImageMagick with PHP and this is my code:
$diplacementmap = new Imagick('displacement.png');
$android = new Imagick('android.png');
$android->compositeImage($diplacementmap, Imagick::COMPOSITE_DISPLACE, 0, 0);
echo $android;
For some reason, I'm not getting expected results. Why could it be?
Both files separately:
Android - https://i.stack.imgur.com/bYbYX.png
Displacement map - https://i.stack.imgur.com/6g4v9.png
Set your displacement values to 10,10 in ImageMagick or Imagick. When you set the arguments to 0,0, you may be getting some larger default.
For example in command line:
Input:
Displacement map:
convert android.png displacement.png -define compose:args=10,10 -compose displace -composite result10.png
or at 20
convert android.png displacement.png -define compose:args=20,20 -compose displace -composite result20.png
Related
I want use OCR. But the images can't read perfectly, so i'm converting image to delete noise background, Original Images.
then, i'm run this command :
convert -colorspace gray -modulate 120 -contrast-stretch 10%x80% -modulate 140 -gaussian-blur 1 -contrast-stretch 5%x50% +repage -negate -gaussian-blur 4 -negate -modulate 130 original.jpeg clean.jpeg
Images Result
The problem is, how to convert above command to php?
Well, i'm very confusing using imagick in php.
mycode (this what i know) :
$image = new Imagick('captcha.png');
$image->modulateImage(450, 0, 500);
$image->writeImage("output.jpg");
Result from PHP Imagick : HERE
I know, it's diffrent configuration number, but result not to far.
Any suggestions how?
==== answare (thank you fmw42)
$image = new Imagick('captcha.png');
$image->thresholdimage(0.1 * \Imagick::getQuantum(), 134217727);
$image->shaveImage(2, 1);
$image->writeImage("output.jpg");
To remove the black border and threshold your image in ImageMagick, do
Input:
convert img.png -shave 1x1 -threshold 0 result.png
Because the 8 and 7 touch, I would be surprised if OCR worked.
For Imagick, see
https://www.php.net/manual/en/imagick.thresholdimage.php
https://www.php.net/manual/en/imagick.shaveimage.php
I'm very new to the whole ImageMagick PHP library. I need to port this function to PHP using ImageMagick:
convert staticmap.png -gaussian-blur 10
\( -size 300x600 gradient:'rgba(255,255,255,0.9)'-'rgba(255,255,255,0.1)' -rotate 270 \)
-gravity north -compose over -composite output.png
or something that will give this output:
I can't use shell_exec like I always have because I'm running on Google App Engine and I don't think that function is enabled.
Is there an easier way to get the desired result? I want to blur it, too, but I think I can figure that part out.
EDIT: found a better way to do this on the command line. Hopefully that'll help conversion into PHP?
This is easy as all CLI options map directly to ImagickMagick.
<?php
/* convert */
// staticmap.png
$staticMap = new Imagick('staticmap.png');
// -gaussian-blur 10x0
$staticMap->gaussianBlurImage(10, 0);
// -size 300x600 gradient:'rgba(255,255,255,0.9)'-'rgba(255,255,255,0.1)'
$mask = new Imagick();
$mask->newPseudoImage(300, 600, 'gradient:rgba(255,255,255,0.9)-rgba(255,255,255,0.1)');
// -rotate 270
$mask->rotateImage('black', 270);
// -gravity north
$staticMap->setGravity(Imagick::GRAVITY_NORTH);
// -compose over -composite
$staticMap->compositeImage($mask, Imagick::COMPOSITE_OVER, 0, 0);
// output.png
$staticMap->writeImage('output.png');
I'm trying to recreate a script that uses the ImageMagick command "convert" to compose an image. But I want to do the same in PHP using Imagick:
convert ./a.png ./b.png ./c.png -virtual-pixel transparent -channel rgba -alpha on -background transparent -define compose:args=300x53.033 -compose displace -composite ./final.png
a.png
b.png
c.png
Result:
Where a.png is a base image, b.png is the overlay and c.png is the gray-scale 'mask'. For achieving this result, I've to execute all manipulations which are supported by the extension, write the image in disk and then execute this command through the exec function, which is a terrible workaround.
The following snippet does not work:
$a = new Imagick('../a.png');
$b = new Imagick('../b.png');
$c = new Imagick('../c.png');
$a->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$a->setImageBackgroundColor(new ImagickPixel('none'));
$a->setOption('compose:args', '300x53.033');
$a->compositeImage($c, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
$a->compositeImage($b, Imagick::COMPOSITE_DISPLACE, 0, 0);
Result:
Expected:
I would like to achieve this same result using only Imagick PHP.
What is the Imagick equivalent of following Imagemagick command?
convert i.jpg -set colorspace RGB ( -clone 0 -fill black -colorize 100% ) ( -clone 0 colorspace gray -negate ) -compose blend -define compose:args=70,30 -composite o.jpg
I did the following Imagick commands, but it doesnt seem to be the same
$img = new Imagick("i.jpg");
$img->setImageColorspace(Imagick::COLORSPACE_RGB);
$clone1 = $img->clone();
$clone1->colorizeImage('black', 1.0);
$clone2 = $img->clone();
$clone2->setImageColorspace(Imagick::COLORSPACE_GRAY);
$clone2->negateImage(0);
$img->setOption('compose:args', '70x30');
$img->compositeImage($clone1, Imagick::COMPOSITE_BLEND, 0, 0);
$img->compositeImage($clone2, Imagick::COMPOSITE_BLEND, 0, 0);
$img->writeImage("o.jpg");
Where have I made mistakes?
For the most part, what you have is correct. Two minor issues will need to be addressed to match the CLI result.
First
Move the setOption line before reading any images in the Imagick object.
$img = new Imagick();
$img->setOption('compose:args', '70x30');
$img->readImage("i.jpg");
// ...
Secound
Colorizing the black color to 100% usually results in a solid black image. For whatever reason, there's no effect with MagickColorizeImage method. There's some work-arounds using ImagickDraw & background-color assignment listed within the comments on PHP.net's documentation. I'd recommend revisiting your first $clone1 image.
I've got the following two commands for imagemagick in the command line:
convert in.png container-mask.png +matte -compose CopyOpacity -composite out.png
composite container.png out.png -compose multiply final.png
Those two commands include 3 files:
in.png: the file that should be masked
container-mask.png: the back/white mask of the areas of container.png where in.png should be visible
container.png image that includes the container for in.png, the one that has been masked in black/white with container-mask.png
Now the question is how to transpose this commands into PHP calls. I've played around quite a bit, but I can't make sense of the API at http://php.net/manual/en/book.imagick.php
Thanks and Bests,
Charly
I've found the answer. Well, that was not too complicated after all:
$original = new Imagick("in.png");
$mask = new Imagick("container-mask.png");
$container = new Imagick("container.png");
$mask->setImageMatte(0);
$original->compositeImage($mask, Imagick::COMPOSITE_COPYOPACITY, 0, 0);
$container->compositeImage($original, Imagick::COMPOSITE_MULTIPLY, 0,0);
$container->setImageFormat( "png" );
echo $container;