convert -flatten produces harsh result

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?".
silenceseeker

convert -flatten produces harsh result

Post by silenceseeker »

I am trying to convert a two-layer .xcf file (created by GIMP) to a .png file, effectively merging the two layers into one.

I have been doing it so far using GIMP with excellent results, but now I want to automate this in a script. Using the following command line:

Code: Select all

convert -flatten circle.xcf[0] circle.xcf[1] circle-simple-flatten.png
I am receiving very hard edges, not what I am interested in:
Image

When saving (while flattening) the same exact file using GIMP, I receive this:
Image

Can you see the difference in the edges?

Any idea how to make convert produce the same result as in GIMP?

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

Re: convert -flatten produces harsh result

Post by fmw42 »

try
convert circle.xcf[0] circle.xcf[1] -background white -flatten circle-simple-flatten.png

see command line syntax at http://www.imagemagick.org/Usage/basics/#why
silenceseeker

Re: convert -flatten produces harsh result

Post by silenceseeker »

fmw42 wrote:convert circle.xcf[0] circle.xcf[1] -background white -flatten circle-simple-flatten.png
Thanks. Unfortunately this didn't solve the problem. It produces the same exact result.

In GIMP, when saving to a PNG file, it prompts with several options. Among them are:
  1. 1. Merge Visible Layers (vs. Flatten Image).
    2. Save color values from transparent pixels.
I believe that the problem lies in #2. Is there an option in 'convert' to "save color values from transparent pixels"?

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

Re: convert -flatten produces harsh result

Post by fmw42 »

it looks to me like the transparency channel should be 8-bit grayscale but appears to be used as (binary)

please post a link to your input image so we can see what might be the issue.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: convert -flatten produces harsh result

Post by el_supremo »

Looks to me like the red circle is not anti-aliased but that there's some anti-aliased pixel info in the transparency channel so that choosing "save color values from transparent pixels" in Gimp adds back the antialiased pixels giving a smooth result.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
silenceseeker

Re: convert -flatten produces harsh result

Post by silenceseeker »

fmw42 wrote:it looks to me like the transparency channel should be 8-bit grayscale but appears to be used as (binary)

please post a link to your input image so we can see what might be the issue.
The input image can be downloaded from here:

http://rapidshare.com/files/337569797/circle.xcf.html

Please note that both layers have alpha channel but, for some reason, 'convert -flatten' doesn't smooth the edges nicely.

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

Re: convert -flatten produces harsh result

Post by fmw42 »

cannot download your image right now as the server say no slots available for free users at this time
silenceseeker

Re: convert -flatten produces harsh result

Post by silenceseeker »

fmw42 wrote:cannot download your image right now as the server say no slots available for free users at this time
Sorry. Does the following work better?
http://www.yousendit.com/download/VGljN ... Z2wzZUE9PQ

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

Re: convert -flatten produces harsh result

Post by fmw42 »

I downloaded your image. you have two frames. the first is a completely opaque white image (so you really don't need this frame). the second is the red circle in a black background with an 8-bit alpha channel that is binary and not grayscale (it only has two colors, black and white so fully opaque and fully transparent and no ramping along the red boundary). Thus you cannot get the antialiased results with what you have provided. Perhaps you are not saving the same image you used in GIMP.

frame [0] stats
gray:
min: 255 (1)
max: 255 (1)
mean: 255 (1)
standard deviation: -0 (-0)
kurtosis: 0
skewness: 0
alpha:
min: 255 (1)
max: 255 (1)
mean: 255 (1)
Histogram:
65536: (255,255,255,255) #FFFFFF white


frame [1] stats
red:
min: 0 (0)
max: 255 (1)
mean: 202.872 (0.795578)
standard deviation: 102.836 (0.403279)
kurtosis: 0.148789
skewness: -1.46587
green:
min: 0 (0)
max: 254 (0.996078)
mean: 0.362183 (0.00142032)
standard deviation: 9.29438 (0.0364485)
kurtosis: 665.428
skewness: 25.7694
blue:
min: 0 (0)
max: 254 (0.996078)
mean: 0.362183 (0.00142032)
standard deviation: 9.29438 (0.0364485)
kurtosis: 665.428
skewness: 25.7694
alpha:
min: 0 (0)
max: 255 (1)
mean: 202.483 (0.794052)
standard deviation: 103.12 (0.404393)
kurtosis: 0.114961
skewness: 1.45429
Histogram:
13397: ( 0, 0, 0, 0) #00000000 none <--- this is the transparent black
52039: (255, 0, 0,255) #FF0000 red <---- this is fully opaque red (all the rest are also fully transparent colors around the boundary)
4: (255,186,186, 0) #FFBABA00 rgba(255,186,186,0)
4: (255,200,200, 0) #FFC8C800 rgba(255,200,200,0)
2: (255,207,207, 0) #FFCFCF00 rgba(255,207,207,0)
4: (255,211,211, 0) #FFD3D300 rgba(255,211,211,0)
4: (255,217,217, 0) #FFD9D900 rgba(255,217,217,0)
6: (255,220,220, 0) #FFDCDC00 rgba(255,220,220,0)
2: (255,223,223, 0) #FFDFDF00 rgba(255,223,223,0)
4: (255,227,227, 0) #FFE3E300 rgba(255,227,227,0)
2: (255,229,229, 0) #FFE5E500 rgba(255,229,229,0)
2: (255,238,238, 0) #FFEEEE00 rgba(255,238,238,0)
2: (255,240,240, 0) #FFF0F000 rgba(255,240,240,0)
14: (255,242,242, 0) #FFF2F200 rgba(255,242,242,0)
4: (255,245,245, 0) #FFF5F500 rgba(255,245,245,0)
4: (255,246,246, 0) #FFF6F600 rgba(255,246,246,0)
4: (255,250,250, 0) #FFFAFA00 rgba(255,250,250,0)
26: (255,252,252, 0) #FFFCFC00 rgba(255,252,252,0)
12: (255,254,254, 0) #FFFEFE00 rgba(255,254,254,0)

So no partial transparency to antialias and all these other extraneous colors presumably around the perimeter to cause problems (perhaps that is how GIMP antialiases?)

Here is my command:

convert circle.xcf[1] -background white -flatten circle.png
Image

Here is the frame [1] image without the alpha channel
convert circle.xcf[1] -alpha off circle_aoff.png
Image

Here is the frame [1] image alpha channel
convert circle.xcf[1] -alpha extract circle_aoff.png
Image

I would just use IM to draw an antialiased red circle on a white background.
silenceseeker

Re: convert -flatten produces harsh result

Post by silenceseeker »

fmw42, thank you very much for your help. The XCF file I posted served only as an illustration to the more general problem. I am trying to automate a task of "properly" flattening layers for any XCF file that is based on template XCF file. Therefore, using IM to generate the circle is not really a solution for me.

I just found out about mogrify. I will try to see whether that could help convert the 1-bit alpha channel in a smart way, the way GIMP automatically does when saving as a PNG.

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

Re: convert -flatten produces harsh result

Post by fmw42 »

mogrify will not do any better than convert. its advantage is to process all files in a directory the same way. So you need to find out how best to use IM to recreate your GIMP process or perhaps generate your GIMP files differently to be more consistent with how IM deals with antialiasing via grayscale alpha channels.
silenceseeker

Re: convert -flatten produces harsh result

Post by silenceseeker »

fmw42, thanks again for your help. I am inclined to accept your advice and use IM instead of GIMP for the entire process. As a starting point I would like to generate a red sphere (on white background) from a plain red square input image:

Using GIMP, I can go (by manually clicking around several operations and options) from this:
Image

To this:

Image

Do you happen to know whether it's possible to accomplish the same thing using IM?

I tried SPHERIZE (one of Fred's ImageMagick Scripts) but all it could produce was a red circle (not sphere) with the same 1-bit alpha edge...

This is the command I used for the above:
spherize -d 160,160 -b "white" image.png sphere.png
Any tips or pointers would be much appreciated.

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

Re: convert -flatten produces harsh result

Post by fmw42 »

my spherize does not do shading

closest I have seen is at http://www.imagemagick.org/Usage/mapping/#spherical

Using that technique I have come up with this:

convert \( -size 100x100 xc:black -fill red -draw 'circle 49.5,49.5 49.5,0' \) \
\( -clone 0 -fill white -opaque red \) \
\( -clone 1 -blur 0x20 -shade 110x21.7 -contrast-stretch 0% \
+sigmoidal-contrast 6x50% -fill grey50 -colorize 10% \) \
\( -clone 0 -clone 2 -compose HardLight -composite \) \
-delete 0,2 +swap -alpha off -compose CopyOpacity -composite red_shaded_sphere_b20.png

Image

convert \( -size 100x100 xc:black -fill red -draw 'circle 49.5,49.5 49.5,0' \) \
\( -clone 0 -fill white -opaque red \) \
\( -clone 1 -blur 0x30 -shade 110x21.7 -contrast-stretch 0% \
+sigmoidal-contrast 6x50% -fill grey50 -colorize 10% \) \
\( -clone 0 -clone 2 -compose HardLight -composite \) \
-delete 0,2 +swap -alpha off -compose CopyOpacity -composite red_shaded_sphere_b30.png

Image

convert \( -size 100x100 xc:black -fill red -draw 'circle 49.5,49.5 49.5,0' \) \
\( -clone 0 -fill white -opaque red \) \
\( -clone 1 -blur 0x50 -shade 110x21.7 -contrast-stretch 0% \
+sigmoidal-contrast 6x50% -fill grey50 -colorize 10% \) \
\( -clone 0 -clone 2 -compose HardLight -composite \) \
-delete 0,2 +swap -alpha off -compose CopyOpacity -composite red_shaded_sphere_b50.png

Image


First line draws a red circle.
Second line makes a white circle from the red one.
Third & Fourth lines make the shading image from the white circle.
Fifth line composites the shading with the red circle
Sixth line uses the white circle to mask the outside of the red shaded circle to transparent.
silenceseeker

Re: convert -flatten produces harsh result

Post by silenceseeker »

fmw42 wrote:my spherize does not do shading
closest I have seen is at http://www.imagemagick.org/Usage/mapping/#spherical
Using that technique I have come up with this:
fmw42, you are amazing. I didn't know that Fred's ImageMagick Scripts are yours. Thank you!

The automated process you offered is impressive. I wonder whether you can add two improvements:
  1. 1. The sphere-border seems to still be 1-bit alpha.
    2. The shading gradient is a straight line, not a circular as in GIMP.
Is it easy enough for you to improve?

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

Re: convert -flatten produces harsh result

Post by fmw42 »

I just took the technique that Anthony used and simplified it to use on a flat red circle image. I have not truly analyzed anthony's method.

I might be able to improve the antialiasing, but I have no idea how GIMP does it shading. I can try some things, but I don't really know. GIMP may be doing true 3D processing, whereas, the shading from IM is a cludge as it is 2D shading applied to a 2D circle. If I can find the code in GIMP, I will see if I can make any sense of it. Cannot promise any timeframe.
Post Reply