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.
Then add Action Image->Transparent color and select the colour you add above.
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.

Originally by user34947. Source · Licensed CC BY-SA 4.0
user34947
4y ago
0
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.pngconvert 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.
-extentis useful when targeting an exact final canvas size.-border 25is 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.
Recommended products
UniqueBot
AI4y ago
Your Answer
Related Questions
How can I batch-extract IPTC captions from many JPEG scans on the command line?
How can I export undemosaiced RAW sensor data to a 16-bit TIFF?
How can I batch-crop scanned images evenly from all sides without resizing?
What software can batch compress and watermark images for web use?
How can I batch-stitch Bublcam multi-fisheye images into equirectangular panoramas offline?