Two PHP version questions - bounding_box and composite color result issue

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
magnets
Posts: 5
Joined: 2016-12-21T19:11:09-07:00
Authentication code: 1151

Two PHP version questions - bounding_box and composite color result issue

Post by magnets » 2016-12-22T16:59:12-07:00

I'm attempting to learn the PHP library. But I can not seem to figure out so many things.

Currently I am trying to get a "bounding box" for the image at http://www.aviongoo.com/tbf_images/need ... ng_box.jpg

That image is a small version of an image I created using imagemagick to mask out the background from two .jpg images.

I still have not figured out how to not lose the original colors in the process. I have discovered that by looking at the image on any of my 5 LCD screens that I see the image differently looking at the screen from different angles! Looking from the bottom shows me the original colors that I normally see looking straight at the screen, looking straight at the screen looks kind of gray. Looking down from the top of the screen changes colors - they seem to flip the shades of black and white. Really weird! I have to be doing something wrong in my code.

$bool = MagickCompositeImage($imagick, $clipMask, MW_DifferenceCompositeOp, 0, 0);
$bool = MagickNegateImage($imagick, false, MW_AllChannels);
$bool = MagickRotateImage($imagick, $pixel_wand, -90);
$bool = MagickTrimImage($imagick, 10);

The rotate did not affect the coloring while the TrimImage seems to have gotten rid of a good deal of non-white from the background of the image surrounding the CD/DVD drive image.

snibgo
Posts: 9378
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Two PHP version questions - bounding_box and composite color result issue

Post by snibgo » 2016-12-22T17:12:33-07:00

LCD screens do look different when viewed from different angles. That's a characteristic of the device, and IM can't change that.

JPG is a lossy format, which means it changes pixel values, so your white background probably isn't a constant colour. Try setting a fuzz before the trim.
snibgo's IM pages: im.snibgo.com

magnets
Posts: 5
Joined: 2016-12-21T19:11:09-07:00
Authentication code: 1151

Re: Two PHP version questions - bounding_box and composite color result issue

Post by magnets » 2016-12-22T18:34:30-07:00

snibgo, thanks for the response. I've just recently registered at imagemagick and already discovered you are considered one of the super-stars here.

Soon my projects will be dealing with RAW format. I hope imagemagick supports that format. For now I am stick with .jpg format.

Concerning setting the fuzz before trim. I'm lost I thought the "10" at the end of MagickTrimImage was setting the fuzz. What PHP function does set the fuzz?

No idea on the bounding box? Or is that impossible with the .jpg images?

And finally, I was not clear about the view of the images. Lets just talk about looking at the image straight on at the monitor before and after applying imagemagic functions. When I look at the original image read into the magickwand the colors look great. But after I process the image to strip away the background, the colors are off - they are significantly more gray. I'm doing something in imagemagick with a PHP function that is not preserving the original colors. I have no idea what I am doing wrong. I've been trying to use various composite operators but the MW_DifferenceCompositeOp was the only one so far that got me close to removing the background. Perhaps negate image is the problem? But without using that I get a dark background and the result is terrible.

User avatar
fmw42
Posts: 22081
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Two PHP version questions - bounding_box and composite color result issue

Post by fmw42 » 2016-12-22T20:46:22-07:00

Soon my projects will be dealing with RAW format. I hope imagemagick supports that format
See http://www.imagemagick.org/Usage/formats/#crw for raw image formats. IM also handles rgb or gray raw formats, but you need to provide the dimensions. See also http://www.imagemagick.org/script/formats.php

magnets
Posts: 5
Joined: 2016-12-21T19:11:09-07:00
Authentication code: 1151

Re: Two PHP version questions - bounding_box and composite color result issue

Post by magnets » 2016-12-23T03:01:03-07:00

Thanks Fred for the RAW documentation reference.

Since I am now assuming (always a bad thing to do) the .jpg format and its "lossy" nature may be the issue. I converted the two images from .jpg to .png. They both look the same in .png or .jpg format so my thought was to use the same script only changing the file names (replacing .jpg with .png). (Both the JPG and PNG script versions produced a PNG image for their final output since I used MagickSetFormat to set that final output format.)

The input file size differences are significant but my server should easily be able to handle a couple of 20MB images. The JPG version of the program worked. (As explained previously the final result is too gray.)

Unfortunately the PNG version of the program churned away for awhile but did not return an image. It did execute all the way to the end of the PHP program but there was no output. There was no error reported.

snibgo
Posts: 9378
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Two PHP version questions - bounding_box and composite color result issue

Post by snibgo » 2016-12-23T09:32:41-07:00

I don't use PHP. From http://phpimagick.com/Imagick/trimImage , it seems the number is a fuzz. But the range seems to be zero to Quantum. If you use Q16, Quantum is 65535. So 10 units is virtually nothing.

JPG is 8 bits/channel, so out of a range zero to Quantum, values are 0, 257, 514 etc. So a fuzz of 10 won't do anything. Try a fuzz of 1028 or greater.

IM can find bounding boxes. Either trim and find the dimension canvas offsets, or us "-format %@". I don't know the PHP equivalent.

For the colour problem, I suggest you post links to before and after versions of the image. Is the input CMYK? Is that why you are negating?

Once an image has been damaged by JPG compression, converting it to another format won't undo that damage.
snibgo's IM pages: im.snibgo.com

Post Reply