bash-tutorial/tutorial/image_size_reduction.org

3.5 KiB

Use imagemagick to manipulate images

ImageMagick is a suite of tools used for digital image edition and manipulation. The tools can be used in the command line (cli).

There are multiple functionalities avaliable in ImageMagick. Commands like convert, mogrify and identify are just some of the useful tools available.

Using the convert command

Converting multiple jpg files into lower size images:

First, cd (change working directory) into the directory containing all the images

  cd directory

Then use convert to resize all the 'jpg' files within the directory:

  convert '*.jpg' -set filename:fn '%[basename]-small' -resize 960x640 '%[filename:fn].jpg'

Here the '%[basename]-small' will copy original files, resize and rename including the suffix "small" to the original name.

Reduce quality using convert (the command is similar to the previous one, but it uses -quality instead of -resize)

  convert '*.jpg' -set filename:fn '%[basename]-small' -quality 50%  '%[filename:fn].jpg'

Using mogrify

mogrofy can be used for similar purposes.

Let's check som images in a directory to verify how much those images can be modified.

Change directory use the commands in the same place were the images are included, e.g:

  cd directory

Counting number of .jpg images in a directory:

  ls -1U DIR_NAME | wc -l

Verify the output:

75

Verifying the images weight of all the images:

  du -h

Output:

306 M

Resize all the .jpg images within the directory:

 mogrify -resize 960x640 *.jpg

Output:

42M

Reduce the quality of all the images within the directory:

  mogrify -quality 80% *.jpg

Weight:

  du -h

Output:

12M

Remove some metada from images

`mogrify -strip`can be used to remove some metada from the images.

First you can check the image metada using the command identify from Imagemagick.

  identify -verbose image.jpg

This command will print an output like this:

You can use mogrify to remove metada from one or multiple images using something like:

  mogrify -strip ./image.jpg # one image
  mogrify -strip ./*.jpg # modifiyng all 'jpg' files in directory