Converting to PNG with transparency with smooth edges

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
yervand
Posts: 5
Joined: 2013-06-24T14:45:51-07:00
Authentication code: 6789

Converting to PNG with transparency with smooth edges

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

Re: Converting to PNG with transparency with smooth edges

Post 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.
yervand
Posts: 5
Joined: 2013-06-24T14:45:51-07:00
Authentication code: 6789

Re: Converting to PNG with transparency with smooth edges

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

Re: Converting to PNG with transparency with smooth edges

Post 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/
Post Reply