The convert command from the ImageMagick suite of instruments offers methods to make all kinds of modifications to picture recordsdata. Amongst these is an possibility to alter the decision of photos. The syntax is easy, and the command runs extraordinarily shortly. It will probably additionally convert a picture from one format to a different (e.g., jpg to png) in addition to blur, crop, despeckle, dither, flip and be part of photos and extra.
Though the instructions and scripts on this put up largely concentrate on jpg recordsdata, the convert command additionally works with a big number of different picture recordsdata, together with png, bmp, svg, tiff, gif and such.
Fundamental resizing
To resize a picture utilizing the convert, you’d use a command like this:
$ convert -resize 1200x1000 smile.jpg smile-2.jpg
The syntax is “convert -resize decision currentfile newfile”.
The decision ought to be expressed as the specified width (in pixels), adopted by an “x” after which the specified top. Notice that if the numbers aren’t numerically associated to the present dimensions of the picture, the resultant decision may not be what you anticipate. Producing a 1200×1000 picture from a 2400×2000 is one factor. Asking for it to be saved as a 2000×1200 will end in one that’s solely 1440×1200.
Utilizing a script
In the event you intend to transform numerous photos or shall be resizing photos usually, it is a good suggestion to make use of a script. The primary script proven under would create a “smile_2.jpg” file from a “smile.jpg” file utilizing the 1200×800 decision. Notice the way it extracts the file extension from the filename in order that it will possibly construct the brand new filename.
#!/bin/bash if [ $# -eq 0 ]; then echo -n "picture file: " learn img else img=$1 fi add=2 # get filetype and base title from argument filetype=`echo $img | awk -F . '{print $2}'` basename=`echo $img | sed 's/.jpg//'` # goal decision decision=1200x800 # new file title will embrace "_2" newfile="${basename}_${add}.$filetype" # run convert command convert -resize $decision $img $newfile # show the brand new file ls -l $newfile
Resizing a gaggle of picture recordsdata
The subsequent script will create a 1200×800 decision file from every of the jpg recordsdata within the present listing and can show every of the brand new recordsdata after it has been arrange.
#!/bin/bash num=2 decision=1200x800 for picture in `ls *.jpg`; do basename=`echo $picture | sed "s/.jpg//"` convert -resize $decision $picture ${basename}_${num}.jpg ls -ltr | tail -1 achieved
In the event you run the script, you’d see one thing like this:
$ resize_jpg_files -rw-r--r--. 1 shs shs 49321 Could 25 09:52 camper_2.jpg -rw-r--r--. 1 shs shs 3872 Could 25 09:52 map_2.jpg -rw-r--r--. 1 shs shs 3872 Could 25 09:52 pig_2.jpg -rw-r--r--. 1 shs shs 130432 Could 25 09:52 tree-cutting_2.jpg -rw-r--r--. 1 shs shs 45082 Could 25 09:52 volcano_rings_2.jpg
Resizing by file sort
The subsequent script will ask you what sort of picture recordsdata to transform and can then run by way of the recordsdata of the kind within the present listing. Just like the earlier script, it provides a “_2” to the file names to distinguish them from the originals. This could simply be modified, after all, if another character or string works higher for you.
#!/bin/bash echo -n "file sort: " learn filetype add=2 decision=800x600 for picture in `ls *.$filetype`; do echo $picture basename=`echo $picture | sed "s/.$filetype//"` convert -resize $decision $picture ${basename}_${add}.$filetype ls -ltr | tail -1 achieved
Utilizing a number of picture resolutions
This final script asks for the title of a single picture file after which creates new variations of the picture utilizing three totally different resolutions. It additionally provides the meant decision to the file title.
#!/bin/bash # ask for picture file title echo -n "picture: " learn picture if [ ! -f $image ]; then echo "No such file: $picture" exit 1 fi # NOTE: decision is top x width for reso in 400x500 600x800 800x1200; do basename=`echo $picture | sed 's/.jpg//'` convert -resize $reso $picture ${basename}_${reso}.jpg ls -ltr | tail -1 achieved
The resultant recordsdata may seem like this:
$ ls -l canine* -rw-r--r--. 1 shs shs 14501 Could 25 12:29 dog_400x500.jpg -rw-r--r--. 1 shs shs 28658 Could 25 12:29 dog_600x800.jpg -rw-r--r--. 1 shs shs 45082 Could 25 12:29 dog_800x1200.jpg -rw-r--r--. 1 shs shs 58628 Could 25 12:25 canine.jpg
Wrap-up
The convert command makes resizing picture recordsdata extraordinarily simple. To study extra about a number of the command’s different choices, try this put up on changing and manipulating picture recordsdata on the Linux command line.
Copyright © 2023 IDG Communications, Inc.