Image Magick¶
Introduction¶
Image Magick is a command line software that you can use to manipulate any king of bitmap images. Use ImageMagick to resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves.
Installation¶
(On MacOs)
You will use the following command to install it, but before, you have to install Xcode.
sudo port install ImageMagick
Warning
Be patient … It could take a long, very long time to build …
Use of imageMagick¶
As I want to keep my repo clean and very light, I would like to use ImageMagick to automatically scan the folder “images”, and resize if needed the images.
To do that, I begin to read the documentation of ImageMagick, and specifically the command ling rubric.
I find quickly a command that could really help me : its mogrify. And the command line that interested me was :
magick mogrify -resize 256x256 *.jpg
This CL resize all your JPEG images in a folder to a maximum dimension of 256x256 and the original image file is overwritten. Perfect ! I will just change the the size limit and try it :
magick mogrify -resize 1024x1024 *.jpg
To try this, I will copy my repo and will select it with the command cd
.
Fail
It resize only the pictures of the folder where I’m and not the subfolders… Secondly, I need to add an option for the quality of the pictures.
I try this :
magick mogrify -resize 1024x1024 -quality 50% *.jpg
It reduce correctly the quality but I need to make this for all the pictures of the images folder.
I discover the command find
, and I try to use it to find all the *.jpg
of the folder and apply a transformation with ImageMagick.
find . -name '*.jpg' -exec mogrify -resize 1024x1024 -quality 50% {} +
Fail
It work perfectly, but the quality were to bad on some large screenshot and the smaller image has been resized at a larger size, so I decided to modify a bit the command.
I sought for a command that could resize the picture only if the size is too important. I find a command that could me by using distort srt
. I tried something like that :
find . -name '*.jpg' -exec mogrify +distort srt "%[fx:(w>1280 || h>1280)?0.9:1] 0" {} +
To sum up, this command will :
- Search all the
*.jpg
files into all the subfolders - Modify the ratio of the pictures if the w or h are greater than 1280px
- The ratio of the pictures will be of 90%
Fail
It works fine on large pictures and I can execute this CL more than one time until all the pictures are less than 1280px. But for pictures less than 1280px, the size increase .
And finally I found that it could be done with only the resize
command !
find . -name '*.jpg' -exec mogrify -quality 80% -resize 1600x1600\> {} +
And it works fine for me. The size of the older “images” folder was 10.4mo , and now I’m at 6.5mo. Maybe I could be more aggressive on the quality of the pictures, but we will see that in the future.
Note
Maybe I could add this command into the CLI ? … I can also make a local folder with all the original images and copy the modified images to the right folder.