10

Alternatively: How do I take a non-square PNG and "fill out" the "rest" of the image with transparency so that the resulting square image has the original image centered in the square?

ULTIMATELY, what I want is to take any image of any GM-supported format of any size, and create a scaled-down PNG (say, 40 pixels maximum for either dimension), with aspect ratio maintained, transparency-padded for non-square original images, AND with an already-prepared 40x40 PNG transparency mask applied.

I already know how to scale down and keep aspect ratio; I already have the command for applying my composite. My only missing piece is square-alizing non-square images (padding with transparency).

Single command preferred; multi-command chain acceptable.

(edit)

Extra info: Here's the composite command I'm using:

gm composite -compose copyopacity mask.png source-and-target.png source-and-target.png

where mask.png has white pixels for what I want to keep of source-and-target.png and transparent pixels for what I want to remove (and become transparent) of source-and-target.png.

Journeyman Geek
  • 127,463
  • 52
  • 260
  • 430
Pistos
  • 387
  • 1
  • 4
  • 15

3 Answers3

9

This command will take any sized input file and fit it best to a 40x40 square and pad with transparency:

convert \
   original.png \
  -thumbnail '40x40>' \
  -background transparent \
  -gravity center \
  -extent 40x40 \
  -compose Copy_Opacity \
  -composite mask.png \
   original-resized.png

The gravity option ensures the image is centered in both directions, and transparent is used wherever there are no pixels. Then the compositing is done with the mask.png

Laogeodritt
  • 113
  • 4
Paul
  • 59,223
  • 18
  • 147
  • 168
  • So we can't do it in one shot with ```composite```? – Pistos Apr 24 '12 at 01:23
  • @Pistos I am not clear on what you are doing with the composite - it is just a transparency mask to hide/show portions of the thumbnail after resizing? Can you [edit] and add the command sequence you have so far, so we can see if it can be combined? – Paul Apr 24 '12 at 02:47
  • @Pistos I think the best approach is to do the compositing with -convert rather than the other way around. I have updated my answer above. – Paul Apr 24 '12 at 03:33
  • Close, but not quite. Your command didn't work as-is, and even with adjustments, it didn't work. It's okay, I'll do it in two steps. I'll update things here after I get the final, working CLI steps. Thanks again. – Pistos Apr 24 '12 at 03:53
  • I would add `-filter Catrom` before the `thumbnail` option because from my experience it generally produces higher quality images. – thdoan Dec 04 '13 at 02:40
9

One command to convert all PNGs from one folder:

mogrify \
 -resize 50x50 \
 -background transparent \
 -gravity center \
 -extent 50x50 \
 -format png \
 -path resized \
 *.png

mogrify is a command from ImageMagick package. You have to create output directory first.

  • 1
    Nice one. BTW, If you feel adventurous, you can omit `-path resized` to edit the files in place. – aaronk6 May 19 '16 at 10:55
  • 1
    This is great. On my install (OSX, 1.3.35) it's `gm mogrify` and instead of `-path resized`, use `-create-directories -output-directory resized` – Blake Mar 19 '20 at 13:08
1

Here's what I eventually went with. A two step process:

gm convert \
  -thumbnail '40x40>' \
  -background transparent \
  -gravity center \
  -extent 40x40 \
   original.png \
   intermediate.png

gm composite \
  -compose in \
   intermediate.png \
   mask.png \
   out.png

Where mask.png is white pixels for what I wanted to keep, and transparent pixels for what I wanted to mask out (discard).

Kurt Pfeifle
  • 12,411
  • 2
  • 54
  • 71
Pistos
  • 387
  • 1
  • 4
  • 15