Detect and Crop Landscape Images to Portraits with Bash PDF and ImageMagick

Detect and Crop Landscape Images to Portraits with Bash PDF and ImageMagick

Effective image processing and manipulation are crucial in digital content creation and management. One common task is recognizing images in landscape format and converting them into two portrait format images, particularly when working with PDFs. This article will guide you through a detailed process, using bash scripting and the powerful ImageMagick toolset to achieve this task efficiently.

Understanding Landscape and Portrait Formats

Before diving into the technical details, it is essential to understand the distinction between landscape and portrait formats. An image in landscape format is longer horizontally than vertically, while portrait format is taller vertically than horizontally. Our goal is to detect such landscape images and split them into two portrait images without losing important content.

Requirements and Dependencies

To follow this tutorial, you will need the following tools and dependencies:

ImageMagick: A software suite to create, edit, compose, or convert images Bash: A command-line shell for executing scripts PDFToImage: To convert PDFs into a sequence of images LibreOffice or PDF-XChange Viewer: To handle PDF files and extract images

Ensure these tools are installed on your system before proceeding. You can install ImageMagick using:

sudo apt-get install imagemagick

For PDFToImage, you may need to install ImageMagick with its dependencies:

sudo apt-get install imagemagick poppler-utils

Identifying Landscape Images

The first step is to identify landscape images within a PDF file. This can be achieved by analyzing the dimensions of the images extracted from the PDF. Here’s a bash script to detect landscape images and tag them:

#!/bin/bash
# Define the path to the PDF and output directory
PDF_FILEexample.pdf
OUTPUT_DIRlandscape_images
mkdir -p $OUTPUT_DIR
# Convert the PDF to images and move to the output directory
convert $PDF_FILE ${OUTPUT_DIR}/%
# Loop through images to identify landscapes
for IMG in ${OUTPUT_DIR}/*; do
    SIZE$(identify -format "%w %h" $IMG)
    IFS' ' read WIDTH HEIGHT 

This script converts the PDF into images using the convert command from ImageMagick, and then iterates through each image to check if the width is greater than the height, indicating a landscape image.

Cropping and Slicing the Images

Once you have identified the landscape images, the next step is to crop and slice them into two portrait images. This can be done using the composite and convert commands from ImageMagick.

#!/bin/bash
# Define the path to the landscape image
LSCC_IMAGElandscape_
# Define the output path for portrait images
PORT1
PORT2
# Crop and slice the landscape image into two portraits
convert $LSCC_IMAGE -crop 50%x100%  repage  adjoin $PORT1 $PORT2

This script crops the landscape image into two portraits by specifying a width of 50% and the height as 100%. The repage command resets the image bounding box, and adjoin ensures the two images are saved as separate files.

Automation with Bash Script

To automate the entire process, you can combine the detection and cropping steps into a single bash script. This script will iterate through all identified landscape images, crop them, and save the resulting portraits:

#!/bin/bash
# Define the path to the PDF and output directory
PDF_FILEexample.pdf
OUTPUT_DIRlandscape_images
mkdir -p $OUTPUT_DIR
# Convert the PDF to images and move to the output directory
convert $PDF_FILE ${OUTPUT_DIR}/%
# Loop through images to identify landscapes and crop them
for IMG in ${OUTPUT_DIR}/*; do
    SIZE$(identify -format "%w %h" $IMG)
    IFS' ' read WIDTH HEIGHT 

This script first converts the PDF into images, then loops through each image to check for landscapes. If a landscape image is detected, it is cropped into two portrait images and saved with the appropriate filenames.

Final Thoughts

Capturing, processing, and converting images effectively optimizes your digital content for various applications and platforms. By utilizing ImageMagick and bash scripting, you can automate the detection and conversion of landscape images into portrait format within PDFs, streamlining your workflow and maximizing the utility of your images.

Keywords

Bash PDF Image processing ImageMagick Portrait cropping Landscape detection