bash-tutorial/tutorial/image_size_reduction.org

149 lines
3.5 KiB
Org Mode

#+date: 2023-08-20
#+options: toc:nil num:nil author:nil
* Use imagemagick to manipulate images
[[https://imagemagick.org/][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, [[https://www.man7.org/linux/man-pages/man1/cd.1p.html][cd]] (change working directory) into the directory containing all the images
#+begin_example sh
cd directory
#+end_example
Then use ~convert~ to resize all the 'jpg' files within the directory:
#+begin_example sh
convert '*.jpg' -set filename:fn '%[basename]-small' -resize 960x640 '%[filename:fn].jpg'
#+end_example
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~)
#+begin_example sh
convert '*.jpg' -set filename:fn '%[basename]-small' -quality 50% '%[filename:fn].jpg'
#+end_example
** 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:
#+begin_example sh
cd directory
#+end_example
Counting number of ~.jpg~ images in a directory:
#+begin_example sh
ls -1U DIR_NAME | wc -l
#+end_example
Verify the output:
75
Verifying the images weight of all the images:
#+begin_example sh
du -h
#+end_example
Output:
306 M
Resize all the ~.jpg~ images within the directory:
#+begin_example sh
mogrify -resize 960x640 *.jpg
#+end_example
Output:
42M
Reduce the quality of all the images within the directory:
#+begin_example sh
mogrify -quality 80% *.jpg
#+end_example
Weight:
#+begin_example sh
du -h
#+end_example
Output:
12M
** Remove some metada from images
[[https://imagemagick.org/script/command-line-options.php#strip][`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.
#+begin_example sh
identify -verbose image.jpg
#+end_example
This command will print an output like this:
# Filename: image.jpg
# Permissions: rw-r--r--
# Format: JPEG (Joint Photographic Experts Group JFIF format)
# Mime type: image/jpeg
# Class: DirectClass
# Geometry: 960x640+0+0
# Resolution: 72x72
# Print size: 13.3333x8.88889
# Units: PixelsPerInch
# Colorspace: sRGB
# Type: TrueColor
# Base type: Undefined
# Endianness: Undefined
# Depth: 8-bit
# Channels: 3.0
# Channel depth:
# Red: 8-bit
# Green: 8-bit
# Blue: 8-bit
# Channel statistics:
# Pixels: 614400
# ...
# ...
# Version: ImageMagick 7.1.1-12 Q16-HDRI x86_64 21239 https://imagemagick.org
You can use ~mogrify~ to remove metada from one or multiple images using
something like:
#+begin_example sh
mogrify -strip ./image.jpg # one image
mogrify -strip ./*.jpg # modifiyng all 'jpg' files in directory
#+end_example
** References
- [[https://askubuntu.com/questions/1354205/imagemagick-convert-on-a-multiple-files][Imagemagick to convert multiple files]]
- [[https://imagemagick.org/][Imagemagick web site]]
- [[https://www.digitalocean.com/community/tutorials/reduce-file-size-of-images-linux][Digital ocean tutorial]]