Combining RGB channels back into a single image using ImageMagick (php)

So, I have a situtation with ImageMagick and php, where I need to process each of the RGB channels separately, and then combine them back into the final image. So, in the code below, $ red, $ green and $ blue are channels (like grayscale images). The following code is what I tried (and several options), but every time I finish an image that shows only cyan, magenta, or yellow. In this case, the resulting image is Cyan.

$im->removeImage(); $im->addImage($red); $im->addImage($green); $im->addImage($blue); $img = $im->combineImages(self::CHANNEL_ALL); $im->removeImage(); $im->removeImage(); $im->removeImage(); $im->addImage($img); 

I think part of my problem is that the PHP documentation does not say how to use combImages and there are no samples as far as I can find. Therefore, it is very likely that I am using this method incorrectly, and I suspect that it is related to the way I combine images in one Imagick object to start with.

EDIT

This question ultimately boils down to the following: how do I recreate the following script using only php?

 convert tmp_r.png tmp_g.png tmp_b.png -combine tmp_rgb.png 
+4
source share
3 answers

So, I think I figured out how to make this work. The missing element was a call to flattenImages (). I'm not quite sure why this worked, but it seems to be what I was looking for. Here's the code (keep in mind that $ this is in the context of a member method of a class that extends Imagick):

 $this->removeImage(); // gets rid of the old, unprocessed image $rgb = clone $this; $rgb->addImage($red); $rgb->addImage($green); $rgb->addImage($blue); $rgb->flattenImages(); // this is what was missing above $rgb = $rgb->combineImages(self::CHANNEL_ALL); $this->addImage($rgb); 

Can anyone comment on why this might be? I expected flattenImages () to combine the three images into one and destroy some of the information, but it seems that it actually tells ImageMagick to process all the contained images together, whereas it processed them independently previously.

+3
source

[EDIT] I have to admit, looking further into the documentation, I'm not sure what the constant CHANNEL_ALL does. They claim that you can combine channels by logically combining them. You may try:

$ im-> combImages (imagick :: CHANNEL_RED | imagick :: CHANNEL_GREEN | imagick :: CHANNEL_BLUE);

[ORIGINAL] I studied this API, and frankly, I think that you are looking for a conversion function, not a union function.

Take a look at the link below and click "Combine RGB Channel Images", http://www.imagemagick.org/Usage/color_basics/

Try it, leave a comment if you need additional help :-)

+4
source

Try the following:

 $im->addImage($red); $im->addImage($green); $im->addImage($blue); $im->combineImages(imagick::CHANNEL_RED | imagick::CHANNEL_GREEN | imagick::CHANNEL_BLUE); 

btw combImages does not return an imagick object, but true / false indicates success or failure, so $im will contain your combined image.

Edit: Apparently combImages is sucking off a lot of time, so here's an alternative: imagick::compositeImage

 $im->compositeImage($red, imagick::COMPOSITE_COPY, 0, 0, imagick::CHANNEL_RED); $im->compositeImage($green, imagick::COMPOSITE_COPY, 0, 0, imagick::CHANNEL_GREEN); $im->compositeImage($blue, imagick::COMPOSITE_COPY, 0, 0, imagick::CHANNEL_BLUE); 
-1
source

Source: https://habr.com/ru/post/1415412/


All Articles