how to flatten to white background

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
rs13
Posts: 65
Joined: 2009-01-15T12:57:26-07:00

how to flatten to white background

Post by rs13 »

Hello,

I am manipulating a .png image by changing its resolution to 300dpi and turning both opaque and transparent white color to #FFFFFE. Anthony was kind enough to help me with this with the following command:

exec('convert -strip -density 300x300 -units PixelsPerInch ' .
"../..".$_GET['dir'] . ' -alpha off -fill "#FFFFFE" -opaque "#FFFFFF" -alpha on -fill "#FFFFFF" -opaque none ' .
"../..".$_GET['dir']);

The issue that I now have is that as a last step I need to flatten the image and make the background #FFFFFF. Can you please advise how I need to alter the above command to accomplish this?

The original post that helped me derive the convert command above is here: viewtopic.php?f=1&t=12914&p=43439#p43381

With Regards,

Robert
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to flatten to white background

Post by fmw42 »

first if your output is png, then png does not support pixelsperinch, only pixelspercentimeter. see http://www.imagemagick.org/Usage/formats/#png_density

to flatten (and going back to Anthony's original command), try

convert -density 300x300 -units pixelspercentimeter image.png -alpha off -fill '#FFFFFE' -opaque '#FFFFFF' -alpha on -background white -flatten result.png
rs13
Posts: 65
Joined: 2009-01-15T12:57:26-07:00

Re: how to flatten to white background

Post by rs13 »

Thank you for your help. Let me test that out.

However, in regards to yoru pixelsperinch, it does work for us and png images do end up 300 dpi.

I quickly tried switching this to pixelpercentimeter and the converted images ends up 300 pixels per centimeter which ends ups being 762px per inch. This does not work for us as we need to be exactly 300px per inch. Any thoughts?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to flatten to white background

Post by fmw42 »

convert your pixels per inch to the equivalent pixels per centimeter

300 dpi * 2.2cm/in = 660 dpc

then set -density 660x660 -units pixelspercentimeter

see
http://www.imagemagick.org/script/comma ... .php#units
http://www.imagemagick.org/Usage/formats/#png_density

"After some testing it seems the PNG image file format does not support a "-units" setting of 'PixelsPerInch', only 'undefined' and 'PixelsPerCentimeter'.
Because of this IM converts a given density/unit setting into the appropriate values for 'PixelsPerCentimeter'.


The last line above seems to imply if you specify -units of pixels per inch, IM will convert to the equivalent pixels per centimeter (probably in a similar manner to what I have specified). So your result will have the expected 300 dpi equivalent but will likely show as 660 dpc
rs13
Posts: 65
Joined: 2009-01-15T12:57:26-07:00

Re: how to flatten to white background

Post by rs13 »

Thank you for clarification. Correct me if I am wrong but 1 inch = 2.54 cm so I would then use 762x762. In any case, when I run the following conversion command:

exec('convert -density 762x762 -units pixelspercentimeter ' . $strFilePath . ' -alpha off -fill "#FFFFFE" -opaque "#FFFFFF" -alpha on -background white -flatten ' .
$strFilePath . ".png");


I get a broken png image that I can only see 2/3 see http://stage.partybeans.com/flatten-sample.png.

Any ideas why this may be occurring?
rs13
Posts: 65
Joined: 2009-01-15T12:57:26-07:00

Re: how to flatten to white background

Post by rs13 »

Here is also the original image that I am running the convert command through.

http://stage.partybeans.com/orig-sample.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to flatten to white background

Post by fmw42 »

correct me if I am wrong but 1 inch = 2.54 cm
Right. Sorry, my error.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to flatten to white background

Post by fmw42 »

Lets look at this more carefully as I believe I made an error above.

1 in = 2.54 cm

so 300 dots/in divided by 2.54 cm/in = 118.2 dots/cm

or 1 cm = 0.394 in

so 300 dots/in multiplied by 0.394 in/cm = 118.2 dots/cm

So using our manual calculation, we have

convert orig-sample.png -density 118x118 -units pixelspercentimeter orig-sample_118dpc.png

identify -verbose orig-sample_118dpc.png

Resolution: 118x118
Units: PixelsPerCentimeter


Now letting IM do it.

convert orig-sample.png -density 300x300 -units pixelsperinch orig-sample_300dpi.png

identify -verbose orig-sample_300dpi.png

Resolution: 118.11x118.11
Units: PixelsPerCentimeter


So IM does it right.
rs13
Posts: 65
Joined: 2009-01-15T12:57:26-07:00

Re: how to flatten to white background

Post by rs13 »

Yep, that makes sense, but what about the broken image that I am receiving when I run the entire command (as per the url links I attached)?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to flatten to white background

Post by fmw42 »

This works fine for me (assuming that is what you want).

convert -density 118x118 -units pixelspercentimeter orig-sample.png \
-alpha off -fill '#FFFFFE' -opaque '#FFFFFF' -alpha on \
-background white -flatten orig-sample_result.png
rs13
Posts: 65
Joined: 2009-01-15T12:57:26-07:00

Re: how to flatten to white background

Post by rs13 »

Yep, that worked for me now.

You guys rock!! Thank you for all your help.
Post Reply