Page 1 of 1
Outline when converting PNG to PDF
Posted: 2014-07-12T11:36:48-07:00
by dasprid
Hi,
I've stumbled on a problem which took me more than an hour already to investigate. When you take a look at the following image:
http://stuff.dasprids.de/images/test.png
You'll see that it's a white object on a transparent background. Now when I try to convert it to a PDF with ImageMagick:
The resulting PDF will in fact be transparent, but there'll be a tiny black outline around the object. My guess is that this is due to the way ImageMagick converts the alpha to a PDF mask and flattens the image itself onto black background. I've tried a few things to change the background color to white, but nothing helped. It'd be great if someone knows a solution to this problem.
Cheers,
DASPRiD
Re: Outline when converting PNG to PDF
Posted: 2014-07-12T11:56:04-07:00
by snibgo
You can change the background, from transparent black, to transparent white:
Code: Select all
convert test.png -background White -alpha background out.pdf
Re: Outline when converting PNG to PDF
Posted: 2014-07-12T12:12:51-07:00
by fmw42
What version of IM and platform are you using?
When I convert that with your command on IM 6.8.9.5 Q16 Mac OSX, I get a pure white pdf with perfectly opaque alpha when viewing in IM or my Safari browser. However, in Adobe Reader or Chrome, I see your black faint outline.
IM says
Code: Select all
Fred-Weinhauss-Mac-mini:desktop fred$ idv test.pdf
Image: test.pdf
Format: PDF (Portable Document Format)
Mime type: application/pdf
Class: DirectClass
Geometry: 91x91+0+0
Resolution: 72x72
Print size: 1.26389x1.26389
Units: Undefined
Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: Gray
Depth: 16/1-bit
Channel depth:
gray: 1-bit
alpha: 1-bit
Channel statistics:
Gray:
min: 65535 (1)
max: 65535 (1)
mean: 65535 (1)
standard deviation: 0 (0)
kurtosis: 0
skewness: 0
Alpha:
min: 65535 (1)
max: 65535 (1)
mean: 65535 (1)
standard deviation: 0 (0)
kurtosis: 0
skewness: 0
Colors: 1
Histogram:
8281: (65535,65535,65535,65535) #FFFFFFFFFFFF graya(255,1)
Seems like IM is forcing an opaque alpha channel when you have bilevel image even with 8-bit transparency.
This seems like a bug to me.
I think you issue with the faint black outline is due to antialiasing of your boundary. You can get rid of it by making the background fully white.
Code: Select all
convert test.png -alpha off -fill white -colorize 100 -alpha on test.pdf
However, I still get a resulting bilevel pdf with fully opaque alpha channel.
Re: Outline when converting PNG to PDF
Posted: 2014-07-12T12:13:59-07:00
by fmw42
snibgo was posting at the same time. His command does the same thing but is a bit more elegant and simpler.
I am still not sure why I get fully opaque transparency from identify. Perhaps PDF does not support bilevel with either 8-bit alpha or even binary alpha as even the following does not work.
Code: Select all
convert test.png -background white -alpha background \
-channel a -threshold 50% +channel PNG8:- | convert - -depth 8 test.pdf
snibgo, do you get that also -- fully opaque alpha?
Re: Outline when converting PNG to PDF
Posted: 2014-07-12T13:20:29-07:00
by dasprid
snibgo wrote:You can change the background, from transparent black, to transparent white:
Code: Select all
convert test.png -background White -alpha background out.pdf
That indeed did the trick, thanks a lot. Now I'm wondering, do you happen to know how the same would look with the Imagick PHP library?
http://php.net/imagick
Re: Outline when converting PNG to PDF
Posted: 2014-07-12T15:09:26-07:00
by dasprid
Nevermind, I solved it myself. It is kinda tricky because every alpha channel mode has a constant but the "background" one, so I had to figure out the correct integer for that. Just for reference, if anyone else is looking this up:
Code: Select all
$image = new Imagick('image.png');
$image->setImageFormat('pdf');
$image->setImageBackgroundColor('white');
$image->setImageAlphaChannel(2);
file_put_contents('image.pdf', $image->getImageBlob());
Just made a pull request to get the constant into PHP Imagick:
https://github.com/mkoppanen/imagick/pull/34
Re: Outline when converting PNG to PDF
Posted: 2014-07-12T15:19:11-07:00
by fmw42
Does your PDF show any transparency against a non-white background? All my attempts are giving a solid white image with perfectly opaque alpha channel (fully opaque and no transparency).
Re: Outline when converting PNG to PDF
Posted: 2014-07-12T15:21:35-07:00
by dasprid
fmw42 wrote:Does your PDF show any transparency against a non-white background? All my attempts are giving a solid white image with perfectly opaque alpha channel (fully opaque and no transparency).
Yep, works perfectly fine.
Re: Outline when converting PNG to PDF
Posted: 2014-07-12T16:16:20-07:00
by fmw42
Can you post your IM version (convert -version) and platform and the result of identify -verbose test.pdf?
For my system:
Version: ImageMagick 6.8.9-5 Q16 x86_64 2014-06-30
http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib cairo fftw fontconfig freetype gslib jbig jng jp2 jpeg lcms lqr ltdl lzma openexr png ps rsvg tiff webp x xml zlib
I am getting the following from test.pdf.
Code: Select all
convert test.png -background white -alpha background test.pdf
Image: test.pdf
Format: PDF (Portable Document Format)
Mime type: application/pdf
Class: DirectClass
Geometry: 91x91+0+0
Resolution: 72x72
Print size: 1.26389x1.26389
Units: Undefined
Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: Gray
Depth: 16/1-bit
Channel depth:
gray: 1-bit
alpha: 1-bit
Channel statistics:
Gray:
min: 65535 (1)
max: 65535 (1)
mean: 65535 (1)
standard deviation: 0 (0)
kurtosis: 0
skewness: 0
Alpha:
min: 65535 (1)
max: 65535 (1)
mean: 65535 (1)
standard deviation: 0 (0)
kurtosis: 0
skewness: 0
Colors: 1
Histogram:
8281: (65535,65535,65535,65535) #FFFF
So perfectly opaque image.