Night Potato
Night Potato

Reputation: 13

Preserving PNG transparency during a simple transformation in imagemagick

Task: i have an input png file (many actually, but i'll just loop the solution). It is 16x16 PNG, 32bit with partial transparency along edges.

It so happens that toolbar of a certain stupid platform requires 17x17 files. My problem is that imagemagick kills transparency when doing simple transformations.


So: Sanity check:

convert add.png PNG32:add_COPIED.png

creates another 16x16@32bpp file. So far so good.

Transformation (gravity is fine):

convert add.png -extent 17x17 PNG32:add_17.png

creates a file with solid white background. That's not good.


What doesn't work:
I tried a serious number of combinations of transparent, transparent-color, background, alpha and flatten. Got nowhere.

What does work:

convert address_book.png -alpha Extract address_book_MASK.png  
convert address_book.png -extent 17x17 PNG32:address_book_17.png  
convert address_book_MASK.png -background black -extent 17x17 address_book_MASK17.png  
composite -compose CopyOpacity address_book_MASK17.png address_book_17.png PNG32:address_book_FIN.png

While i have a working set of commands and I can get through the day, I honestly believe that this is the wrong way to do things - four commands that create 3 intermediate files that i need to delete later. Surely it can be done in a better way?

Upvotes: 1

Views: 740

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 208052

Set the background colour before changing the extent:

convert input.png -background none -extent WxH result.png

Upvotes: 1

Related Questions