transparent png => matte png with background color --- inconsistent results

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
ohnonot
Posts: 13
Joined: 2012-09-12T03:41:25-07:00
Authentication code: 67789

transparent png => matte png with background color --- inconsistent results

Post by ohnonot »

this will take a little explaining....

i have lots of transparent (mostly png) tiles.
i wrote a script to
  • preview the tiles by filling a fixed size canvas with the tile, tiled, and a particular background color

    Code: Select all

    display -size 800x800 -background "#ff5555" tile:tile.png
  • if so desired, create a new tile with the background color, make it matte, to be used as a background

    Code: Select all

    convert tile.png -background "#ff5555" -transparent "#ff5555" -alpha remove newtile.png
the problem is, the results are sometimes inconsistent:
Image - (here's the original tile:Image)
the top half is the display preview, the bottom is the newly created tile, (applied to the root window with feh).

i have tried many different options (the -transparent option doesn't seem to be doing anything here, the -alpha remove option is only effective in the convert command...), they all either have the same result, or worse, or error out....

how can i get consistent results and fully matte png files?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: transparent png => matte png with background color --- inconsistent results

Post by fmw42 »

This works for me on IM 6.9.3.7 Q16 Mac OSX.

Code: Select all

convert -size 800x800 tile:nami.png result.png
You should always provide your IM version and platform and in this case version of libpng.

Code: Select all

convert -version

Code: Select all

convert -list format
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: transparent png => matte png with background color --- inconsistent results

Post by snibgo »

I don't understand what you are trying to do.

You have png files with transparency (example, please!), and you want to make them fully opaque. What do you want do do with the transparency? Flatten against a solid colour, or what?

You have a small file, nami.png, which is mostly transparent. By itself, this won't make your transparent png images opaque.
(applied to the root window with feh)
I don't know what this means.
snibgo's IM pages: im.snibgo.com
ohnonot
Posts: 13
Joined: 2012-09-12T03:41:25-07:00
Authentication code: 67789

Re: transparent png => matte png with background color --- inconsistent results

Post by ohnonot »

sorry i forgot to provide the version.

Code: Select all

convert -version
Version: ImageMagick 6.9.3-7 Q16 x86_64 2016-03-08 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenCL OpenMP 
Delegates (built-in): bzlib cairo fontconfig freetype gslib jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png ps rsvg tiff webp wmf x xml zlib
and

Code: Select all

convert -list format
     ---snip---
     PNG* PNG       rw-   Portable Network Graphics (libpng 1.6.21)
           See http://www.libpng.org/ for details about the PNG format.
    PNG00* PNG       rw-   PNG inheriting bit-depth, color-type from original if possible
    PNG24* PNG       rw-   opaque or binary transparent 24-bit RGB (zlib 1.2.8)
    PNG32* PNG       rw-   opaque or transparent 32-bit RGBA
    PNG48* PNG       rw-   opaque or binary transparent 48-bit RGB
    PNG64* PNG       rw-   opaque or transparent 64-bit RGBA
     PNG8* PNG       rw-   8-bit indexed with optional binary transparency
     ---snip---
Flatten against a solid colour, or what?
yes, that's what i want. and i want the results from the two commands from my original post to be consistent.

except that the first command provides a preview of what that tiled pattern, with that particular background color, looks like on a larger canvas, and the second command simply transforms the original transparent tile (nami.png) to a same-size non-transparent tile with the previously given background color.

the larger image in my original post shows the results from both commands in one image.
many transparent tiles show this inconsistency, but some don't.
here's a better example, with seperate images:
spawned from this transparent tile and #ff0000 as background color:
- the display command produced this preview canvas
- the convert command produced this non-transparent tile (here's an 800x800 canvas produced from that tile, for better comparison).

i guess i could work around the problem by creating the flattened tile first, then create a preview canvas from it -
but even so, i'd like to understand the inconsistency i'm seeing.

and just to give you some context, i am writing a script for myself to preview a large collection of tiles & choose a background color & choose a combination of tile + background to use as the desktop background.

edit:
saying "transparent tiles" i mean of course partly transparent tiles, usually kept grayscale, specifically for the purpose of applying different bg-colors, see e.g. here. the site also illustrates how my script works (preview canvas).
ohnonot
Posts: 13
Joined: 2012-09-12T03:41:25-07:00
Authentication code: 67789

Re: transparent png => matte png with background color --- inconsistent results

Post by ohnonot »

i found out how to get consistent results, but i still do not understand what is happening.

tile.png is a placeholder for the original transparent tile, e.g. nami.png is one of the tiles that look differently with theose two different "flattening methods".

these two commands provide consistent results:

Code: Select all

display -size 800x800 -background "#ff0000" tile:tile.png
convert -size "$(identify -format "%wx%h" tile.png)" -background "#ff0000" tile:tile.png newtile.png
and these two commands provide consistent results:

Code: Select all

convert -size "$(identify -format "%wx%h" tile.png)" xc:"#ff0000" tile.png -layers flatten png:- | display -size 800x800 tile:-
convert -size "$(identify -format "%wx%h" tile.png)" xc:"#ff0000" tile.png -layers flatten newtile.png
btw, i am scripting this on a linux machine, and the script's goal is to give the user choices to set a tiled desktop background.

as far as my script is concerned, at this point it would make more sense to use a temporary file (flattened tile) to produce the preview canvas, but i like how i can get "instant results" with display without having to resort to temporary files.

can someone explain the differences between those "flattening methods" (and hopefully find a way to simplify the last display command)?
Post Reply