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.
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
There are images I want to remove the background of (or set it to transparent rather). For that reason, I have tested a bash imagick command that looks like this:
convert test.jpg -alpha set -channel RGBA -bordercolor white -border 1x1 -fuzz 2% -fill none -floodfill +0+0 white -shave 1x1 test.png
Because I need to use this in my php script, I need to translate this over now. What I've come up with is this:
$imagick->readImage($path);
$imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_SET);
$imagick->borderImage('white', 1, 1);
$imagick->floodFillPaintImage('transparent', 20, 'white', 0, 0, false);
$imagick->shaveImage(1, 1);
$imagick->writeImage(str_replace('.jpg', '.png', $path));
From what I can tell, it generates the image and it removes big parts of the background. But the fuzziness setting seems to be ignored.
The results of this script are always the same as when I use -fuzz 0% in the command prompt, no matter what fuzziness value I pass. Am I doing something wrong or is there a bug (which would leave me searching for another script capable of doing this)?
Am I doing something wrong or is there a bug
Let's call it a documentation error.
Imagick::floodFillPaintImage (and most of the other functions that take a fuzz parameter) need to have the fuzz scaled to the quantum range that ImageMagick was compiled with. e.g. for ImageMagick compiled with 16 bits of depth, the quantum range would be (2^16 - 1) = 65535
There is an example at http://phpimagick.com/Imagick/floodFillPaintImage
$imagick->floodFillPaintImage(
$fillColor,
$fuzz * \Imagick::getQuantum(),
$targetColor,
$x, $y,
$inverse,
$channel
);
So the reason that you're seeing the output image be the same as if you had passed in 0 fuzz, is that ImageMagick is interpreting the value of 2 that you are passing in as 2 / 65535 .... which is approximately zero.
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.
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;