Resizing photographs on the Linux command line

[ad_1]

The transform command from the ImageMagick suite of equipment offers methods to make all types of modifications to image data files. Amongst these is an option to transform the resolution of photographs. The syntax is easy, and the command runs really rapidly. It can also transform a graphic from a person structure to yet another (e.g., jpg to png) as very well as blur, crop, despeckle, dither, flip and be a part of pictures and more.

Though the commands and scripts in this write-up mainly focus on jpg files, the change command also works with a huge selection of other graphic data files, which include png, bmp, svg, tiff, gif and these types of.

Basic resizing

To resize an picture employing the transform, you would use a command like this:

$ convert -resize 1200x1000 smile.jpg smile-2.jpg

The syntax is “change -resize resolution currentfile newfile”.

The resolution should be expressed as the desired width (in pixels), adopted by an “x” and then the ideal height. Take note that if the numbers are not numerically similar to the current proportions of the impression, the resultant resolution could not be what you be expecting. Making a 1200×1000 picture from a 2400×2000 is a person detail. Inquiring for it to be saved as a 2000×1200 will consequence in 1 that is only 1440×1200.

Employing a script

If you intend to change a amount of illustrations or photos or will be resizing visuals generally, it really is a excellent idea to use a script. The initially script revealed below would produce a “smile_2.jpg” file from a “smile.jpg” file making use of the 1200×800 resolution. Notice how it extracts the file extension from the filename so that it can create the new filename.

#!/bin/bash
 
if [ $# -eq 0 ] then
    echo -n "image file: "
    browse img
else
    img=$1
fi
incorporate=2

# get filetype and base name from argument
filetype=`echo $img | awk -F . 'print $2'`
basename=`echo $img | sed 's/.jpg//'`

# concentrate on resolution
resolution=1200x800

# new file title will contain "_2"
newfile="$basename_$include.$filetype"

# run change command
change -resize $resolution $img $newfile

# display screen the new file
ls -l $newfile

Resizing a team of impression documents

The up coming script will make a 1200×800 resolution file from every of the jpg data files in the current directory and will screen each of the new documents following it has been set up.

#!/bin/bash

num=2
resolution=1200x800

for impression in `ls *.jpg` do
  basename=`echo $picture | sed "s/.jpg//"`
  convert -resize $resolution $graphic $basename_$num.jpg
  ls -ltr | tail -1
carried out

If you run the script, you would see anything like this:

$ resize_jpg_information
-rw-r--r--. 1 shs shs  49321 May 25 09:52 camper_2.jpg
-rw-r--r--. 1 shs shs   3872 May 25 09:52 map_2.jpg
-rw-r--r--. 1 shs shs   3872 Might 25 09:52 pig_2.jpg
-rw-r--r--. 1 shs shs 130432 Might 25 09:52 tree-chopping_2.jpg
-rw-r--r--. 1 shs shs  45082 May perhaps 25 09:52 volcano_rings_2.jpg

Resizing by file style

The next script will inquire you what kind of picture files to change and will then run through the data files of the variety in the latest directory. Like the former script, it provides a “_2” to the file names to differentiate them from the originals. This can very easily be altered, of system, if some other character or string functions much better for you.

#!/bin/bash

echo -n "file variety: "
browse filetype
increase=2
resolution=800x600

for graphic in `ls *.$filetype` do
  echo $picture
  basename=`echo $impression | sed "s/.$filetype//"`
  change -resize $resolution $picture $basename_$incorporate.$filetype
  ls -ltr | tail -1
carried out

Using a number of image resolutions

This past script asks for the title of a one graphic file and then creates new variations of the impression utilizing a few distinctive resolutions. It also provides the intended resolution to the file name.

#!/bin/bash

# talk to for image file identify
echo -n "image: "
examine impression

if [ ! -f $image ] then
  echo "No this sort of file: $image"
  exit 1
fi

# Note: resolution is height x width
for reso in 400x500 600x800 800x1200 do
  basename=`echo $graphic | sed 's/.jpg//'`
  change -resize $reso $picture $basename_$reso.jpg
  ls -ltr | tail -1
accomplished

The resultant information may well seem like this:

$ ls -l pet dog*
-rw-r--r--. 1 shs shs 14501 Might 25 12:29 doggy_400x500.jpg
-rw-r--r--. 1 shs shs 28658 May possibly 25 12:29 canine_600x800.jpg
-rw-r--r--. 1 shs shs 45082 May perhaps 25 12:29 pet_800x1200.jpg
-rw-r--r--. 1 shs shs 58628 May 25 12:25 doggy.jpg

Wrap-up

The change command helps make resizing impression documents extremely simple. To learn extra about some of the command’s other choices, check out this post on converting and manipulating picture documents on the Linux command line.

Copyright © 2023 IDG Communications, Inc.

[ad_2]

Supply link