How can I batch-add transparent padding around images programmatically?

Asked 10/23/2021

9 views

2 answers

0

I need to process thousands of images automatically and add transparent padding around each one instead of editing them by hand. For example, if an image is 800×800, I want the output to be 850×850 with the extra 25 pixels on each side transparent. Is there a command-line or code-friendly tool that can do this in batch?

Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0

Photography Stack Exchange contributor

4y ago

2 Answers

4

To have transparency you should store the images as PNG format (for example). From command line you can use ImageMagic with command:

convert source.jpg -resize 800x800 -gravity center -background transparent -extent 850x850  target.png

If you are fine with graphical tool you can use XnView MP, open in browser mode, select images, Ctrl+U, on Actions tab add Image->Canvas Resize, select Relative and enter 25 for Wight and Height. For background select colour which do not exist on your images. enter image description here Then add Action Image->Transparent color and select the colour you add above. enter image description here In Output tab select PNG format and press Convert. P.S. Both tools are available for Windows, Linux and MacOS

If you want to use multiply cores in XnView in Output tab select "Use multiply CPU Cores" and drag the slice on desired number of cores. enter image description here

Originally by user34947. Source · Licensed CC BY-SA 4.0

user34947

4y ago

0

AI Answer

Generated from our catalog & community — verify before relying on it.

Yes. A common way to do this programmatically is with ImageMagick from the command line. To keep the added area transparent, save to a format that supports transparency, such as PNG.

Examples:

  • convert input.jpg -resize 800x800 -gravity center -background transparent -extent 850x850 output.png
  • convert input.png -matte -bordercolor none -border 25 output.png

Both approaches add transparent space around the image. Since you want to go from 800×800 to 850×850, the border is 25 pixels on each side.

Key points:

  • Use PNG (or another format with alpha support); JPEG does not support transparency.
  • -extent is useful when targeting an exact final canvas size.
  • -border 25 is a simpler option when you just want equal padding on all sides.

If you prefer a GUI batch tool, XnView MP can also batch resize canvas and make the added color transparent, but for code-driven workflows ImageMagick is the better fit.

UniqueBot

AI

4y ago

Your Answer