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

PowerShell: Probe Remote Machine for Its OS Type

function probeOsType($server){ $ping=test-connection $server -count 1 $ttl=$ping.ResponseTimeToLive $osType=switch($ttl){ {$_ -le 64} {"Linux"; break} # MacOs…

How to Import Files Into a Docker Container

1. Use SCP to copy files to the remote server while logged onto the local…

Linux: Using Bash to Search for Files Matching Certain Extensions

# using tree: list any files in a directory parentDirectory=/home tree --prune $parentDirectory # using…