Posted On September 15, 2021

How to Use ImageMagic to Resize Images

kimconnect 0 comments
blog.KimConnect.com >> Linux >> How to Use ImageMagic to Resize Images

Converting PNG to JPG (optimized):

# This is an optimized resizing command to convert PNG to JPG with minimal loss of image quality
outputPath=./resized
size=1200 # width
mkdir $outputPath
originalFormat=png
newFormat=jpg
mogrify -path $outputPath -format $newFormat -resize $size -filter Triangle -define filter:support=2 -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude-chunk=all -interlace none -colorspace sRGB -strip *.$originalFormat

Resizing JPG & PNG Files On First-level Child Folders

# Set image sizing variables
size=2475x1650
newFormat=jpg

# Look into current directory for any folders, then create a sub-folder named 'resized'. Afterward, trigger the command to resize any jpg or png images to be dumped into the 'resized' sub-folders of each directory

for d in */ ; do
    [ -L "${d%/}" ] && continue; # skip sym-links
    outputPath=$(echo "$d""resized");
    mkdir $outputPath;
    mogrify -path $outputPath -format $newFormat -resize "$size"">" -filter Triangle -define filter:support=2 -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define -interlace none -colorspace sRGB -strip $d*.{jpg,JPG,png,PNG};
done

Resizing JPG Files (Optimized):

# This is an optimized resizing command to convert PNG to JPG with minimal loss of image quality
outputPath=./resized
size=2400x1600 # height x width
mkdir $outputPath
originalFormat=jpg
newFormat=jpg
mogrify -path $outputPath -format $newFormat -resize $size -filter Triangle -define filter:support=2 -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -define -interlace none -colorspace sRGB -strip *.$originalFormat

Resizing JPG Files:

# This command would pickup all PNG images of current directory and create resized versions of them in the ./resized folder
mkdir ./resized
mogrify -path ./resized -resize 1200 *.jpg

Resize All and Only Images In a Directory, Including Sub-Directories

path=./
size=2400x1600

find $path -type f -exec file \{\} \; | awk -F: '{if ($2 ~/image/) print $1}' \
| while IFS= read -r imageFile; do \
    mogrify -resize $size\> "$imageFile"; \
  done

This would happen if the resized directory doesn’t exist

kim@kim-linux:~$ cd ~
kim@kim-linux:/Scans$ mogrify -path ./resized -resize 1200 *.png 
mogrify-im6.q16: unable to open image `./resized//Military-Payment-Certificate-One-Dollar-Series-661-Backside.png': No such file or directory @ error/blob.c/OpenBlob/2874.
mogrify-im6.q16: WriteBlob Failed `./resized//Military-Payment-Certificate-One-Dollar-Series-661-Backside.png' @ error/png.c/MagickPNGErrorHandler/1641.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

How to Add Printers on Linux Mint 20

Overview: The Common Unix Printing System (CUPS) is an open source printing system developed by…

How to Move Virtualbox Guest VMs to Different Hosts

Connect to Source Host and copy files to NAS: ssh webadmin@server01 vboxmanage list running vms…

How to Transfer Files Via SSH or WinRM

Secured Shell (SSH) is the prevalent standard for remote accessing of Linux systems. Even Microsoft…
Other project: DragonCoin.com