I love the command line options of imagemagick. Mogrify is great to resize images and change quality, which is what I use most often. However, I have noted that the filesize if often larger than what it should be. Especially with small images. For instance, I have a regular 640px (width) photo, which I change to quality 80 and a width of 80px:
mogrify -quality 80 -resize 80 file.jpg
Works well and my image gets resized and the quality is changed to 80. However, the filesize is around 40Kb. For such a tiny image, that is huge! When I use mtPaint, and open the file and save it (not changing anything, just CTRL+O, CTRL+S), the filesize decreases with more than 95% to less than 2Kb! I have seen this is often the case.
What goes wrong?
I found the answer... it was in the "metadata"! Apparently this easily weighs about 18 Kb per image, so in the original you might not note this, but in the tiny resize it means 18 Kb + 2 Kb = 20 Kb total filesize. They significantly increased by doing:
mogrify -strip file.jpg
TL;DR: specify
-compress
+ compression format, in addition to-strip
, to keep the same compression format of the input file(s) and reduce file size.Problem
As user6019 has mentioned,
-strip
will remove the metadata of whatever file you pass throughmogrify
ormagick mogrify
(>= ver. 7).However, when I tried this command, which is a version of the above:
magick mogrify -strip -density 535 -debug cache *.tif
,the sizes of my output files still didn't decrease.
Version information
OS: Windows 11 Home, Version 21H2
IM: 7.1.0-19 Q16-HDRI x64 2021-12-22
Solution
After inspecting the details of the files before and after
mogrify
, I noticed thatmogrify
removed the compression format of my input files, leaving totally uncompressed files. As a result, all file sizes were blown up by about 400x (input = 59 KB; output = 26,172 KB).After reading this post, it seems that ImageMagick treats LZW-compressed files differently depending on if the compression format is "enabled". I explicitly forced the compression format using this line of code (after compiling the above command):
magick mogrify -compress LZW *.tif
and file sizes returned to the same magnitude as they were before processing with IM (i.e. file sizes decreased).
So, in the future, if people are concerned about output file sizes following
mogrify
or evenconvert
/magick
calls, double-check compression too!