Script to crop images en masse

Paint programs, vector editors, 3d modelers, animation editors, etc.
Post Reply
Message
Author
labbe5
Posts: 2159
Joined: Wed 13 Nov 2013, 14:26
Location: Canada

Script to crop images en masse

#1 Post by labbe5 »

#!/usr/bin/env bash
# GNU shell script to image mass cropping
# --------------------------------------------------------------------------
# Copyleft (Ɔ) Yoander Valdés(sedlav) Rodríguez <https://www.librebyte.net/>
# This script is released under GNU GPL 2 + licence
# --------------------------------------------------------------------------
# Use:
#
# This script find all images under current DIR, calculate new size and crop
# every image. This script asks if you want to keep original file or
# override it. Download the script give it exec perms and put it on
# /usr/local/bin.
# --------------------------------------------------------------------------
#
# Verify if ImageMagick suite is installed
#
( ! which identify convert &>/dev/null ) && { echo You must install ImageMagick suite; exit 1; }
#
# Ask for original file keeping
#
read -p "Do you want to keep original file [y|n]:" answer
#
# Pattern for jpg, jpeg, png, gif, tiff files
#
PATTERN='.*.(jpe?g|png|gif|tiff)$'
#
# Find and read every file that match above pattern in current DIR
#
find $(pwd) -regextype posix-egrep -iregex $PATTERN -type f -print0 | while read -d $'' file; do
# Get the image size new
size=$(identify "$file" | awk '{ print $3 }' | awk -Fx -v to_crop=20 '{ print $1"x"$2-to_crop}')
# New file name
cropped_name=$(dirname $file)/cropped_$(basename "$file")
# Crop the image
convert "$file" -crop $size+0+0 "$cropped_name"
# Rename to original name
[[ $answer =~ ^[0Nn]$ ]] && [[ -e "$cropped_name" ]] && mv -f "$cropped_name" "$file"
done

From : https://www.librebyte.net/en/multimedia ... s-in-mass/

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#2 Post by Flash »

Can you give us an example picture of how this works? Is it actually cropping, or is it shrinking the image by reducing the number of pixels, without cutting off large bits?

User avatar
Fossil
Posts: 1157
Joined: Tue 13 Dec 2005, 21:36
Location: Gloucestershire, UK.

#3 Post by Fossil »

" Cropping images in mass
ImageMagick

Recently a friend told me:
I have to remove the watermark of hundreds of images, you could do it with a program like GIMP but are hundreds of images, I know that you can automate this process using some commands of the ImageMagick suite; I need your help to make the script in bash."
If this is a matter of using someone elses images without their prior permission it is morally unethical!

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#4 Post by musher0 »

Hello all.

You don't need to call in the entire army (Image Magick)
when a commando (netpbm) can do the job.

Depending on your Pup, you may have part of the netpbm kit already installed,
To check, open a console and type

Code: Select all

cd /usr/bin
ls p?m*
The full netpbm package has a couple of crop utilities.

Overview of netpbm

List of netpbm graphic utilities
Search for "crop" (without the quotation marks)

Getting netpbm
You may want to check if the PPM for your Pup offers netpbm.
If not, netpbm at SourceForge has a 64bit deb package and the source available.
No 32-bit pkg though; if there is a need, I'll compile a 32-bit package from source.

IHTH
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#5 Post by MochiMoppel »

Flash wrote:Is it actually cropping, or is it shrinking the image by reducing the number of pixels, without cutting off large bits?
It crops. It cuts 20 px from the bottoms of all images, regardless of their size. One crop fits all :lol:

IMO this script is too limited and too complicated.
Using convert one can achieve the same result with a one-liner in ROX:
1) in ROX-Filer select the images you want to crop
2) Open a shell command entry box (Shortcut Schift+!)
3) Copy following command into the box and hit Enter

Code: Select all

for i in "$@" ;do convert "$i" -crop -0-20  "cropped_$i"; done
The first value after -crop (-0) would crop the right side of the image, the second crops the bottom.

Post Reply