Skip to content

Image Manipulation on Termux: Tools and Techniques

Introduction:

Termux, the Android terminal emulator, not only lets you manage Linux utilities on your phone but also opens up a world of possibilities for image manipulation. Whether you need to resize, convert, apply effects, or process images in bulk, Termux has several powerful tools that can help you do just that. This post will walk you through using the most popular image manipulation tools in Termux, including ImageMagick, GraphicsMagick, and more.

1. ImageMagick: A Powerhouse for Image Manipulation

As mentioned before, ImageMagick is one of the most versatile tools for image manipulation in Termux. It supports over 200 image formats and comes with features like resizing, rotating, converting, and adding effects.


pkg update
pkg install imagemagick

Key Commands:


# Resizing an image
magick input.jpg -resize 800x600 output.jpg


# Converting formats
magick input.png output.jpg


# Adding blur effect
magick input.jpg -blur 0x8 output.jpg

2. GraphicsMagick: The Fast Alternative

GraphicsMagick is another tool similar to ImageMagick, but it is known for being faster and more efficient in certain cases. It is also a popular choice for batch image processing.


pkg install graphicsmagick

Key Commands:


# Resizing an image
gm convert input.jpg -resize 800x600 output.jpg


# Converting image formats
gm convert input.png output.jpg

3. Pillow: Python Imaging Library

For those who prefer a more programmatic approach, the Python Imaging Library (Pillow) offers an excellent interface for image manipulation. You can use it in Termux by installing Python and Pillow.


pkg install python
pip install pillow

Example Python Code:


from PIL import Image
# Open an image file
img = Image.open("input.jpg")
# Resize the image
img = img.resize((800, 600))
# Save the resized image
img.save("output.jpg")

Pillow allows for more advanced operations like image filtering, text overlay, and drawing shapes on images.

4. FFmpeg: Video Frame Extraction and GIF Creation

Although FFmpeg is mostly known for video and audio processing, it also offers a wide array of image manipulation options, including frame extraction and GIF creation.


pkg install ffmpeg

Extracting frames from a video:


ffmpeg -i input_video.mp4 -vf "fps=1" output_frame%d.png

This will extract one frame per second from input_video.mp4 and save it as output_frame1.png, output_frame2.png, etc.

Converting video to GIF:


ffmpeg -i input_video.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" output.gif

FFmpeg is especially useful when working with videos or creating GIFs from video files.

5. ExifTool: Metadata Manipulation

ExifTool is a powerful tool for reading and editing image metadata (EXIF data). You can use it to remove or modify metadata such as camera details, GPS coordinates, and more.


pkg install exiftool

Viewing image metadata:


exiftool input.jpg

Removing metadata:


exiftool -all= input.jpg

This can be particularly useful if you want to strip out sensitive metadata from images before sharing them.

6. ImageOptim: Reducing Image File Sizes

ImageOptim is a tool for compressing and optimizing images, making them smaller without sacrificing much quality. While it’s not traditionally available in Termux, you can use alternative command-line tools like jpegoptim or optipng to achieve similar results.


pkg install jpegoptim

Compressing JPEG images:


jpegoptim input.jpg


pkg install optipng

Compressing PNG images:


optipng input.png

These tools are perfect for reducing file size, especially for web usage, without compromising too much on quality.

7. Batch Processing and Automation

Once you’re familiar with the tools, you can combine them with basic shell scripting in Termux to automate image processing. For example, you can write a script to resize all images in a directory:


#!/bin/bash
for img in *.jpg; do
    magick "$img" -resize 800x600 "resized_$img"
done

Save this script as resize_images.sh and run it in Termux to resize all .jpg images in the current directory.

Conclusion: A Suite of Tools for Every Need

With Termux, you have access to a wide array of image manipulation tools, from simple resizing and conversion with ImageMagick and GraphicsMagick to more advanced manipulation using Python’s Pillow or FFmpeg for video frames. Whether you’re a beginner or an advanced user, Termux gives you the flexibility to handle almost any image processing task right on your Android device.

From batch processing large numbers of images to creating GIFs and stripping out metadata, these tools will help you perform all your image manipulation tasks efficiently.