# Manipulating images & PDFs using CLI commands

DevFeed: [Manipulating images & PDFs using CLI commands](<https://devfeed.tech/articles/manipulating-images-pdfs-using-cli-commands-25342.md>)

Original publisher: [Read original article](<https://kau.sh/blog/terminal-cli-image-pdf-conversion/>)

Author: Kaushik Gopal

Published: 2022-07-22T03:40:45Z

Content type: tutorial

Language: en

Sources: [Kaushik Gopal's Site](<https://devfeed.tech/sources/kaushik-gopal-s-site.md>)

Topics: [Command-line interface](<https://devfeed.tech/topics/cli.md>), [Image](<https://devfeed.tech/topics/image.md>), [pdf](<https://devfeed.tech/topics/pdf.md>), [macOS](<https://devfeed.tech/topics/macos.md>), [Homebrew](<https://devfeed.tech/topics/homebrew.md>)

Tags: [cli](<https://devfeed.tech/tags/cli.md>), [command-line](<https://devfeed.tech/tags/command-line.md>), [commands](<https://devfeed.tech/tags/commands.md>), [images](<https://devfeed.tech/tags/images.md>), [install](<https://devfeed.tech/tags/install.md>), [macos](<https://devfeed.tech/tags/macos.md>), [pdf](<https://devfeed.tech/tags/pdf.md>), [svg](<https://devfeed.tech/tags/svg.md>)

## AI overview

A practical collection of CLI commands for manipulating image and PDF files. It covers installing ImageMagick and Poppler with Homebrew on macOS, merging and splitting files, replacing PDF pages, resizing and compressing images, converting between common formats, and inspecting image properties.

## Source excerpt

Whenever I need to convert, merge, or combine images or PDF files, I pull out my Terminal and attempt doing it first with CLI (command line interface) commands. Over time I've built an arsenal of CLI commands that 9/10 times does the trick faster than any other program. Prerequisites #### Most of the image manipulation commands require imagemagick and the PDF ones require poppler. Both of which are just a homebrew install away for macOS: # image utilities brew install "imagemagick" # pdf utilities brew install "poppler" Merging ## Merge images horizontally/vertically ### # convert ships with imagemagick ## horizontal convert +append image_1.png image_2.png merged ## vertical convert -append image_1.png image_2.png merged // vertical # using montage # slightly better as it maintains transparency within images ## horizontal montage 0_index.png 1_post.png -background none -tile 2x1 -geometry +0+0 PNG32:out.png ## vertical montage 0_index.png 1_post.png -background none -tile 1x2 -geometry +0+0 PNG32:out.png Merge images and PDFs ### # convert ships with imagemagick convert 1.png 2.pdf merged.pdf Merge PDFs ### # pdfunite ships with poppler pdfunite output-1.pdf output-2.pdf output-3.pdf merged-1-3.pdf pdfunite output-4.pdf output-5.pdf merged-4-5.pdf # using native tools that comes with the macOS "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o merged.pdf source1.pdf source2.pdf # using ghostscript [also works with encrypted PDFs] gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf source1.pdf source2.pdf source3.pdf Splitting ## Split multi-page PDF into single page PDFs ### # pdfseparate & pdfunite ship with poppler # split input.pdf into output-1.pdf output-2.pdf ... etc. pdfseparate input.pdf output-%d.pdf # split input.pdf from page 2 till the end pdfseparate -f 2 input.pdf output-%d.pdf Replacing single pages in PDF ## A common usecase is to swap out a single page in a pdf. poppler doesn't have a readymade command f