问题
I am converting the Image with a specific color and it is working fine with the magick
command, below is my command,
magick image.png -channel RGB -colorspace gray -auto-level +level-colors '#f48023', result.png
and it is working perfectly fine and now I am converting this command into PHP
code but not sure about -auto-level
and +level-colors
argument. Here is my code,
$image = new Imagick('image.png');
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_DEACTIVATE);
$image->setImageColorspace(Imagick::COLORSPACE_RGB);
$image->setImageType(Imagick::IMGTYPE_GRAYSCALE);
$image->opaquePaintImage('','#f48023', 0, true);
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE);
header("Content-Type: image/png");
echo $image->getImageBlob();
and the output looks not good at all,
here is my original Image
**EDIT(Updated code) **
$im = new Imagick();
$image = new Imagick('logo3.jpg');
list ($width, $height) = array_values ($image->getImageGeometry ());
$im->newImage(1, $height,new ImagickPixel('#f48023'));
$im->newImage(1, $height,new ImagickPixel('#FFF'));
$im->resetIterator();
$combine =$im->appendImages(true);
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_DEACTIVATE);
$image->setImageColorspace(Imagick::COLORSPACE_GRAY);
$image->contrastStretchImage(1,1,Imagick::CHANNEL_GRAY);
$image->clutImage($combine);
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE);
header("Content-Type: image/png");
echo $image;
and output of this code is below
logo3.jpg is here
回答1:
See autoLevelImage at https://www.php.net/manual/en/imagick.autolevelimage.php
I do not see any equivalent to +level-colors. But you can get a similar effect by creating a two pixel color map image, one pixel for each of two colors by appending the two 1-pixel images. See https://www.php.net/manual/en/imagick.appendimages.php
Then use clutImage to apply the colors from the color map image to the processed image. See https://www.php.net/manual/en/imagick.clutimage.php
ADDITION:
Here is what I mean using ImageMagick command line as an alternate to -auto-level and +level-colors. In the first command I use -auto-level and +level-colors. In the second command I use -contrast-stretch 0 (as equivalent to -auto-level) and a color map image with -clut as equivalent to +level-colors.
Input:
magick lorem_ipsum.png -colorspace gray -contrast-stretch 0 +level-colors '#f48023,' lorem_ipsum_recolor1.png
magick \( lorem_ipsum.png -colorspace gray -contrast-stretch 0 \) \
\( -size 1x1 xc:"#f48023" xc:white +append -filter cubic -resize 1024 \) \
-clut lorem_ipsum_recolor2.png
I do not see the need for -colorspace RGB, which would make the colors linear and darker. If you want to convert from some other colorspace, use -colorspace sRGB.
回答2:
Try this in PHP Imagick, where I have added resizeImage to your combine image. You can try FILTER_TRIANGLE in place of FILTER_CUBIC and see which one you like better. I have also replace your contrastStretchImage with autoLevelImage.
$im = new Imagick();
$image = new Imagick('logo3.jpg');
list ($width, $height) = array_values ($image->getImageGeometry ());
$im->newImage(1, $height,new ImagickPixel('#f48023'));
$im->newImage(1, $height,new ImagickPixel('#FFF'));
$im->resetIterator();
$combine =$im->appendImages(true);
$combine->resizeImage(1024,1,Imagick::FILTER_CUBIC,1)
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_DEACTIVATE);
$image->setImageColorspace(Imagick::COLORSPACE_GRAY);
$image->autoLevelImage();
$image->clutImage($combine);
$image->setImageAlphaChannel(Imagick::ALPHACHANNEL_ACTIVATE);
header("Content-Type: image/png");
echo $image;
来源:https://stackoverflow.com/questions/62777570/how-to-use-auto-level-level-colors-command-argument-into-php-imagemagick