Outline when converting PNG to PDF

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
dasprid
Posts: 12
Joined: 2014-07-12T11:30:52-07:00
Authentication code: 6789

Outline when converting PNG to PDF

Post 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:

Code: Select all

convert test.png test.pdf
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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Outline when converting PNG to PDF

Post 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
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Outline when converting PNG to PDF

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Outline when converting PNG to PDF

Post 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?
dasprid
Posts: 12
Joined: 2014-07-12T11:30:52-07:00
Authentication code: 6789

Re: Outline when converting PNG to PDF

Post 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
dasprid
Posts: 12
Joined: 2014-07-12T11:30:52-07:00
Authentication code: 6789

Re: Outline when converting PNG to PDF

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Outline when converting PNG to PDF

Post 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).
dasprid
Posts: 12
Joined: 2014-07-12T11:30:52-07:00
Authentication code: 6789

Re: Outline when converting PNG to PDF

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Outline when converting PNG to PDF

Post by fmw42 »

Can you post your IM version (convert -version) and platform and the result of identify -verbose test.pdf?

For my system:

Code: Select all

convert -version
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

Code: Select all

identify -verbose 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.
Post Reply