Page 1 of 1

Converting to PNG with transparency with smooth edges

Posted: 2013-09-16T16:14:15-07:00
by yervand
I have the following command to convert a pdf file to a png with transparency...

The PDF file (CMYK format) is a white background with a black image so I want to take everything that is White and make it transparency while leaving anything that is black alone.

Im using the following command:
exec('convert ' . $pdf_file . ' -channel rgba -fuzz 900 -transparent "#ffffff" ' . $png_file, $output, $return);
Can anyone tell me if this is the correct way of doing this or if there is a better way to do it?

Also when it does get converted the edges of the black area are jagged meaning there are still some white areas that remain on the file and it just looks really bad. Anyone have a solution to this or a better way of doing this.

Re: Converting to PNG with transparency with smooth edges

Posted: 2013-09-16T16:19:33-07:00
by fmw42
Transparency will be either on or off (no partial) with the command you have. So you will lose any antialiasiing.

IM does not process CMYK very well. So you would be better to convert to sRGB by adding -colorspace sRGB right after the convert and before the pdf read.

You use of -fuzz 900 assumes that you are using 16-bit (Q16) IM compile. A value of 900 is rather small in comparison to 65535. I generally prefer to use percent with -fuzz.

Generally, -channel rgba is not needed with -transparent. But it should not hurt to include it.

You have not said what kind of pdf you have. Is it in color or grayscale or binary black/white?

What version of IM are you using and what platform?

It might be helpful if you provide a link to your pdf so other can see what you have and test with it.

Re: Converting to PNG with transparency with smooth edges

Posted: 2013-09-16T17:16:35-07:00
by yervand
Thanks for the info

Ill try to get files to you soon and all that you asked for

But as far as fuzz value what would you recommend for percentage wise? I tried 65% and it looks pretty good on a larger png file but still bad on a smaller one. Does it depend on the dimensions?

Re: Converting to PNG with transparency with smooth edges

Posted: 2013-09-16T17:34:53-07:00
by fmw42
It depends on the image itself and not the dimensions.

We probably can help you with creating a mask from the thresholded image and then blurring it to antialias the edges and then use the mask as a transparency channel instead of the binary one. But it would help to look at your images.

The process would be something like

convert image \( -clone 0 -fuzz XX% -transparent white -blur 0x1 \) -alpha off -compose copy_opacity -composite resultimage


The -blur 0x1 may have to be adjusted depending upon your image.

The above is unix notation. If on windows remove the \ before the two parens and use %% in place of %.

See http://www.imagemagick.org/Usage/windows/