Page 1 of 1

PNG8 semi-transparent pixel matte

Posted: 2014-02-13T05:01:56-07:00
by appel
I'm trying to make semi-transparent pixels output with a matte color (black in my case).

Currently when I output an image as 'png8' all pixels that are not 100% opaque will appear as fully transparent!

What I want is twofold:
1) 100% transparent pixels remain 100% transparent
2) pixels that are not opaque or 100% transparent will be converted to opaque pixel with a 'black' as background

Here's an example of what I want:
Upper text shows normal 'png' with semi-transparent pixels
Lower text shows what I want to accomplish with 'png8'
Image

In photoshop this is simply called "matte color". But I've attempted specifying imageMatteColor=black and imageMatte=true in conjunction with 'png8' but it has no effect.


(I am using php to do this, but that shouldn't matter I hope.)

Re: PNG8 semi-transparent pixel matte

Posted: 2014-02-13T06:48:53-07:00
by snibgo
Hmm. There is probably a simple way of doing this, but I can't see it. A complex way is:

Code: Select all

convert ^
  in.png ^
  ( +clone -alpha Extract -fill White +opaque black -write mpr:MASK +delete ) ^
  -background black -flatten ^
  mpr:MASK ^
  -compose CopyOpacity -composite ^
  out.png

Re: PNG8 semi-transparent pixel matte

Posted: 2014-02-13T08:53:10-07:00
by appel
Thanks a lot for that.

I'm not really so clever using the command line, seems to obscure. How would that translate to php functions?

Here's my experiment code:

Code: Select all

<?php
$width = 100;
$height = 20;
$text = "Hello world";

$image = new Imagick();
$image->newImage($width, $height, new ImagickPixel('#00000000'));
$image->setImageFormat('png8');

$draw = new ImagickDraw();
$draw->setFillColor('#ffffff');
$draw->setFontSize(12);
$draw->setGravity(4);
$draw->annotation(10, 0, $text);
$image->drawImage($draw);

header('Content-type: image/png');
echo $image;
?>
I'm sorry for pasting this code here, I'm not asking you to code for me, but I don't realize what corresponding methods to use.

Re: PNG8 semi-transparent pixel matte

Posted: 2014-02-13T09:02:10-07:00
by snibgo
Sorry, I don't know php. If you put the entire command (with my ^ end-of-lines) in an exec, it should work.

EDIT: Sorry, I meant withOUT my ^ end-of-lines.

Re: PNG8 semi-transparent pixel matte

Posted: 2014-02-13T10:22:27-07:00
by appel
I managed to find the secret to this, a method called Imagick::paintTransparentImage()

Code: Select all

<?php
$width = 200;
$height = 40;
$text = "Hello world";

$image = new Imagick();
$image->newImage($width, $height, new ImagickPixel('#000000'));
$image->setImageFormat('png8');

$draw = new ImagickDraw();
$draw->setFillColor('#ffffff');
$draw->setFontSize(35);
$draw->setGravity(5);
$draw->annotation(0, 0, $text);
$image->drawImage($draw);

$image->paintTransparentImage(new ImagickPixel('#000000'), 0, 0);

header('Content-type: image/png');
echo $image;
?>

Re: PNG8 semi-transparent pixel matte

Posted: 2014-02-13T10:39:21-07:00
by glennrp
Don't use the "png8" subformat (just use "png") if you want semitransparent pixels. We define "png8" to support only binary transparency.
[EDIT] Sorry I misunderstood your objective. PNG8 will work as your output format because it only has fully transparent and opaque pixels.