Page 1 of 1

Applying opacity when flattening to JPEG?

Posted: 2010-11-24T09:02:54-07:00
by pwnedd
Suppose you have an image with an alpha channel and the opacity is set to 50%. Does anyone know how you can "flatten" the image to JPEG so that the image appears to be 50% transparent on a black background?

Example:

Code: Select all

    $im = new Imagick("50percent_opacity.png");
    $im->setImageFormat("JPEG");
    header("Content-type: image/jpg");
    echo $im;
The result then is a JPEG image with a black background and a 100% opaque copy of the image on top.

What I want instead is a JPEG image with a black background and a half-transparent version of the original image on top, similar to how GIMP/Photoshop would flatten the image with a black background.

Any suggestions?

Thanks,
Keith

Re: Applying opacity when flattening to JPEG?

Posted: 2010-11-24T10:21:18-07:00
by fmw42
see flattenImage and setBackground at http://us2.php.net/imagick

Re: Applying opacity when flattening to JPEG?

Posted: 2010-11-24T12:08:55-07:00
by pwnedd
Hi Fred,

Thanks for the suggestion. I've tried using both of those methods, however, with no luck., e.g.:

Generate a 50% opaque image:

Code: Select all

convert logo: -type PaletteMatte -channel Alpha -evaluate Divide 2 input.png
Or, using IMagick:

Code: Select all

$logo = new Imagick("logo:");
$logo->setImageType(imagick::IMGTYPE_PALETTEMATTE);
$logo->setImageOpacity(0.5);
Example input (50% opaque image)
Example input (50% opaque image)
input.png (102.26 KiB) Viewed 16696 times
Next, attempt to apply black background and flatten:

Code: Select all

$logo = new Imagick("input.png");
$logo->setBackgroundColor("black");
$logo->flattenImages();
$logo->setImageFormat("jpg");
$logo->writeImage("actual_result.jpg");
Result (Actual):
Actual result
Actual result
actual_output.jpg (42.67 KiB) Viewed 16696 times
Whereas the expected/desired result should look like:
Desired result
Desired result
desired_output.jpg (23.86 KiB) Viewed 16696 times
Know what I'm missing here? How can I can from that input, to the desired output?

Thanks,
Keith

Re: Applying opacity when flattening to JPEG?

Posted: 2010-11-24T15:39:00-07:00
by fmw42

Code: Select all

$logo = new Imagick("input.png");
$logo->setBackgroundColor("black");
$logo->flattenImages();
$logo->setImageFormat("jpg");
$logo->writeImage("actual_result.jpg");
I have only dabbled with iMagick and so am not an expert. But your code appears reasonable to me. You might try setting the imageFormat prior to flattenImages, but it may not make a difference.

Are you using the latest iMagick and the latest IM? Imagick is at 3.x and Imagemagick is at 6.6.5-10

Re: Applying opacity when flattening to JPEG?

Posted: 2010-11-25T04:31:02-07:00
by pwnedd
Switching the order doesn't help either :\

I'm using pretty recent versions of both (ImageMagick 6.5.7-8 and IMagick 3.0.0).

Re: Applying opacity when flattening to JPEG?

Posted: 2010-11-25T10:55:13-07:00
by fmw42
pwnedd wrote:Switching the order doesn't help either :\

I'm using pretty recent versions of both (ImageMagick 6.5.7-8 and IMagick 3.0.0).
Imagick is at 3.1x and ImageMagick is at 6.6.5-10. But I don''t know if that will help. Perhaps some PHP expert can help further. You might contact Bonzo, if he does not respond to this topic.