- [Instructor] Hi again, and welcome to the next video
of the last section,
bulk image resizing and format conversion.
In the previous video
we've seen about user administration scripts.
This video will discuss
how to use scripts for image management.
All of use digital cameras and download photos
from the cameras as well as the internet.
When we need to deal with a large number of image files
we can use scripts to easily perform actions
on the files in bulk.
A regular task we come across with photos
is resizing the file.
Also format conversion from one image format to another
comes with use, for example, jpg to png conversion.
When we download pictures from our camera,
the large resolution pictures take a large size,
but we may need pictures of lower sizes
that are convenient to store and email over the internet.
Hence we resize it to lower resolutions.
We'll use the command convert from the imagemagick suite,
which contains excellent tools for manipulating images
that work across several image formats
and different constructs with rich options.
Most of the GNU Linux distributions
don't come with ImageMagick installed by default.
You need to manually install the package.
For more information point your web browser
to www.imagemagick.org.
In order to convert from one image format
to another image format, use...
We can resize an image to a specified image size,
either by specifying the scale percentage
or by specifying the width and height of the output image.
Resize the image by specifying width or height as follows.
It's required to provide either width or height,
so that the other will automatically be calculated
and resized so as to preserve the image size ratio.
Resize the image by specifying
the percentage scale factor as follows.
Let's see a script for image management.
The following is a sample output to scale the images
in the directory sample_dir to 20% size.
In order to scale the images to the width 1024, use...
Change the files to png format by adding -ext png
along with the preceding commands.
Scale or convert files
with a specified a destination directory as follows.
The preceding image_help.sh script
can accept several command line arguments,
such as -source, -percent, -scale, -ext, and -dst.
A brief explanation for each is as follows.
The source parameter is used to specify
the source directory for the images.
The percent parameter is used to specify the scale percent
and minus scale is used to specify
the scale width and height.
Either -percent or -scale is used;
both of them do not appear simultaneously.
The -ext parameter is used to specify the target fileformat.
- ext is optional;
if it's not specified, format conversion is not performed.
The -dest parameter is used to specify the destination
directory for scale or conversion image of files.
- dst is optional;
if dst is not specified, the destination directory
will be the same as the source directory.
As the first step in the script, it checks
whether the number of command arguments
given to the script are correct.
Either four, six or eight parameters can appear.
Now by using a while loop and case statement
we parse the command line arguments
corresponding to variables.
This is a special variable
that returns the number of arguments.
The shift command shifts the command arguments
one position to the left,
so that on execution of each shift
we can access command arguments one by one
by using the same dollar one variable
rather than using dollar one, dollar two,
dollar three and so on.
The case statement matches the value of dollar one.
It's like a switch statement in the C programming language.
When a case is matched
the corresponding statements are executed.
Each match case statement is terminated with this command.
Once all the parameters are parsed in these variables
a for loop is used to iterate through the path
of each file in the source directory
and the corresponding action
to convert the file is performed.
If the variable ext is defined,
if -ext is given in the command argument,
the extension of the destination file is changed
from this to this.
In the next statement it checks
whether the -dst parameter is provided.
If the destination directory is specified,
the destination path is created
by replacing the directory in the source path
with the destination directory
by using the filename slicing.
In the next statement it crafts the parameter
to convert command for performing resize.
After the parameters are crafted
the convert command is executed with proper arguments.
Awesome.
We've successfully learned about bulk image resizing
and format conversion.
In the next video we will learn
how to take screenshots from the terminal.