PNG file manipulation

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
cejones
Posts: 8
Joined: 2019-05-20T12:05:48-07:00
Authentication code: 1152

PNG file manipulation

Post by cejones » 2019-05-20T12:15:15-07:00

I need to take a single PNG file, tile it 3x3 and then save it out, preserving the alpha channel.

I have worked my way to the following command:

montage original_file*.png -tile 3x3 -alpha set -geometry +0+0 tiled_file.png

montage sucks because it requires I have 9 identical files in the working directory, so my script generates them and then deletes them after the montage command. I tried to use the convert command in place of montage, which does not require multiple files to tile, but it was not handling the alpha channel properly. (convert -size {$newXresolution}x{$newYresolution} tile:original_file.png tiled_file.png) where the new resolutions are 3X the originals.

This montage command creates what I need, but when I compare the original file to the output file, the alpha channel starts off as 8-bit, but the output file shows it as 1-bit.

Is there a way to tell montage to keep all image parameters the same as the original file... especially the alpha channel settings? I tried saving using the PNG8:, PNG16, etc but this did not help.

Version of IM I am using: Version: ImageMagick 6.9.10-11 Q16 x86_64 2018-09-08

Any help is appreciated,

Chris
Austin, TX

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

Re: PNG file manipulation

Post by fmw42 » 2019-05-20T13:24:49-07:00

Why use montage. Just use append. Here is an example

# make transparent image from internal Imagemagick logo:

Code: Select all

convert logo: -transparent white logot.png
# make 3x3 tiling

Code: Select all

convert logot.png -duplicate 2 +append \
\( -clone 0 -clone 0 \) -append \
result.png

cejones
Posts: 8
Joined: 2019-05-20T12:05:48-07:00
Authentication code: 1152

Re: PNG file manipulation

Post by cejones » 2019-05-20T14:10:15-07:00

Thank you fmw42. This works great. I now need to learn how duplicate, append and clone work so I can understand what this is actually doing

What if I wanted to add X number of pixels between each image... to space them out?

Chris

snibgo
Posts: 12437
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PNG file manipulation

Post by snibgo » 2019-05-20T14:32:44-07:00

cejones wrote:montage sucks because it requires I have 9 identical files in the working directory, ...
Or just one file and "-duplicate 8".
cejones wrote:This montage command creates what I need, but when I compare the original file to the output file, the alpha channel starts off as 8-bit, but the output file shows it as 1-bit.
I suggest you remove "-alpha set" (which is harmless but pointless) and insert "-background None".
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 729
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: PNG file manipulation

Post by GeeMack » 2019-05-20T14:52:54-07:00

cejones wrote:
2019-05-20T12:15:15-07:00
montage sucks because it requires I have 9 identical files in the working directory, so my script generates them and then deletes them after the montage command.
The other solutions offered above are good. Here's another pretty simple idea to make a 3x3 tiled result from a single input image...

Code: Select all

convert input.png -background none -duplicate 2 +append -duplicate 2 -append result.png

cejones
Posts: 8
Joined: 2019-05-20T12:05:48-07:00
Authentication code: 1152

Re: PNG file manipulation

Post by cejones » 2019-05-20T15:13:51-07:00

Thank you GeeMack. Yours is a cleaner looking line of code. I tested it and it works well.

And thank you snibgo. I modified my montage code with -duplicate 8 and it indeed removes the need for multiple files. Awesome day... learned how to use duplicate, append and clone.

Thank you all for the help!

snibgo
Posts: 12437
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PNG file manipulation

Post by snibgo » 2019-05-20T15:32:19-07:00

cejones wrote:What if I wanted to add X number of pixels between each image... to space them out?
With GeeMack's "convert" command, you can "-border" immediately after the input.png, or "-extent" or "-splice", perhaps with a final "-chop", depending on the exact effect you want.
snibgo's IM pages: im.snibgo.com

cejones
Posts: 8
Joined: 2019-05-20T12:05:48-07:00
Authentication code: 1152

Re: PNG file manipulation

Post by cejones » 2019-05-21T06:48:32-07:00

-border works great and solves that question. Thank you snibgo. I really like your IM pages... lots of great scripts for me to learn from.

Post Reply