Page 1 of 1

Converting to monochrome images from RGB save little size

Posted: 2018-10-18T03:08:04-07:00
by DynV
So from some grey-scale images, which are basically monochrome with antialiasing, I used GIMP fuzzy select tool with a 0 threshold then made a mask out of that to make sure I'd end up with a monochrome image, for design purpose, not to save on the file size. I'm not the most active graphic designer so later I recalled about using the right mode to save on file size and since there would be no conversion I though why not?

I based it on conversion - Converting from color to true black and white in ImageMagick? - Super User [answered Mar 25 '15 at 6:03 DavidPostill] and came up with
$ for f in [SOURCE_DIRECTORY]/*; do convert "$f" -negate -threshold 0 -negate "[DESTINATION_DIRECTORY]${f#[SOURCE_DIRECTORY]}"; done
(What's in square brackets is replaced with the appropriate content which is only letters, underscores & dashes, and the square brackets removed in the process.)

It turns out little file size saving has been made from this. There's 12 images which were exported, from GIMP, into both PNG & GIF so the source directory contain 24 files. Here's a comparison from the top 2 biggest files, in TSV, sizes in bytes:

Code: Select all

monochrome	RGB
25,720	29,347
24,500	25,365
(the former isn't code, I just used that tag as there doesn't seem to be another preformatted text one)
Just to make sure I was comparing the same thing, for both of the files, I created a file with in 2 layers the monochrome & RGB version in GIMP and set the 1st layer to the mode Difference and the result is transparent & black, so no difference; to make doubly sure, I offsetted the bottom layer 1 px and white was added.

Since I work on Windows, I used Cygwin to have the work done. I updated pretty soon before and to make sure there haven't been an update since I ran the setup right before starting to write this and got
Nothing to Install/Update
, here's what it is:
$ convert --version
Version: ImageMagick 6.9.10-11 Q16 x86_64 2018-09-08
Copyright: © 1999-2018 ImageMagick Studio LLC
Features: Cipher DPC OpenMP
Delegates (built-in): autotrace bzlib cairo fftw fontconfig fpx freetype gslib jbig jng jp2 jpeg lcms lzma pangocairo png ps rsvg tiff webp x xml zlib
Am I doing something wrong? Is what I attempted actually is supposed to non-negligibly save file size?

Update 1: Changing /${f# into ${f#.

Re: Converting to monochrome images from RGB save little size

Posted: 2018-10-18T03:35:51-07:00
by snibgo
Your link is broken.

How large are your images, in pixels? Simple arithmetic gives the bits per pixel. If this is less than one and the image is noisy, further shrinkage is hard.

If your grayscale inputs are stored as three channels, and compressed efficiently, then storing them as single channel won't necessarily improve compression and save space.

But it might. It depends on many factors.

For further advice, I suggest you link to your input files. (And don't use a photo-sharing service that converts them to JPEG.)

Re: Converting to monochrome images from RGB save little size

Posted: 2018-10-18T04:16:24-07:00
by DynV
I think it's this forum that is breaking the link if you take what's following
in it, it should work.

For the images information
$ identify *.png
28c795e4db4c71da104642b271964c3a_(bard).png PNG 1031x1031 1031x1031+0+0 8-bit sRGB 25236B 0.016u 0:00.000
2c8a3940f4d506f7a7198a8035ae84c5_(fighter).png PNG 1006x1006 1006x1006+0+0 8-bit sRGB 25365B 0.000u 0:00.000
2d3bfdc3d027f6d38ffcef8d1d9044ad_(ranger).png PNG 903x903 903x903+0+0 8-bit sRGB 22963B 0.000u 0:00.000
522a9e630c6c0a25d8f921ed5b688f7f_(wizard).png PNG 898x898 898x898+0+0 8-bit sRGB 26271B 0.000u 0:00.000
81111081508e4e7bd138890ab2cdf9dd_(cleric).png PNG 899x899 899x899+0+0 8-bit sRGB 24821B 0.000u 0:00.000
8588b6f9179f2cfed6f26d3d4d89addb_(sorcerer).png PNG 701x701 701x701+0+0 8-bit sRGB 15645B 0.000u 0:00.000
979646f11eddd53c027dba86dfc428df_(druid).png PNG 829x829 829x829+0+0 8-bit sRGB 19533B 0.016u 0:00.000
9e18b24ae454cfec5a88bb15da8bee54_(warlock).png PNG 1086x1086 1086x1086+0+0 8-bit sRGB 29347B 0.000u 0:00.000
bb70661930e5533fcf2fb396855b0aa5_(rogue).png PNG 806x806 806x806+0+0 8-bit sRGB 17249B 0.000u 0:00.000
c7fe251cd11a2ecb590d7d9efa596a49_(barbarian).png PNG 929x929 929x929+0+0 8-bit sRGB 21310B 0.000u 0:00.000
f59aee806df75cdce2fd591d96423270_(monk).png PNG 694x694 694x694+0+0 8-bit sRGB 16244B 0.000u 0:00.000
fd2697d60765eee831e83f8e6a96f321_(paladin).png PNG 908x908 908x908+0+0 8-bit sRGB 23393B 0.000u 0:00.001
I have not included the GIF versions as although I like to have them, the PNG are my main concern.

If you search on the file name cropping from the underscore (up to the dot; ex: 28c795e4db4c71da104642b271964c3a.png from 28c795e4db4c71da104642b271964c3a_(bard).png), it's probable that you will get the files basis, they're D&D 5e classes symbols; which as I mentioned in the OP are RGB but their content is greyscale with the vast majority being monochrome, I did a bit of work on them making them monochrome but it's not very noticeable at all when comparing the 2 images zoomed-out and having for background a white 1:1 circle (thus part of the background transparent); here's the largest file size of them:

Re: Converting to monochrome images from RGB save little size

Posted: 2018-10-18T04:57:00-07:00
by snibgo
That image has colour channels thresholded, but has many values for alpha. To get better compression, you should also threshold alpha, eg:

Code: Select all

magick 2dmcv8m.png -channel RGBA -threshold 50% +channel out.png
This reduces the size from 29 KB to 15 KB.

Re: Converting to monochrome images from RGB save little size

Posted: 2018-10-18T14:04:50-07:00
by DynV
Is there a way to combine your command with the one that was mentioned in the OP (the command on the Super User URL answered by DavidPostill) ? If not I'll have to do it in 2 commands (and I'd rather have it done in 1).

Re: Converting to monochrome images from RGB save little size

Posted: 2018-10-18T15:05:00-07:00
by snibgo
Sure. You can threshold alpha at 50%, then RGB, like this::

Code: Select all

$ for f in [SOURCE_DIRECTORY]/*; do convert "$f" -channel A -threshold 50% -channel RGB -negate -threshold 0 -negate -channel "[DESTINATION_DIRECTORY]/${f#[SOURCE_DIRECTORY]}"; done

Re: Converting to monochrome images from RGB save little size

Posted: 2018-10-19T12:48:19-07:00
by DynV
Through an error I think was (inadvertently) caused in your last post, I found an error in my OP and fixed it, logged in the Update 1; the symptom is the "//" in
$ for f in [SOURCE_DIRECTORY]/*; do convert "$f" -channel A -threshold 50% -channel RGB -negate -threshold 0 -negate -channel "[DESTINATION_DIRECTORY]/${f#[SOURCE_DIRECTORY]}"; done
convert: unrecognized channel type `[DESTINATION_DIRECTORY]//[FILE_NAME]' @ error/convert.c/ConvertImageCommand/924.
and through troubleshooting figured if I removed the last "-channel" it worked ending up with
$ for f in [SOURCE_DIRECTORY]/*; do convert "$f" -channel A -threshold 50% -channel RGB -negate -threshold 0 -negate "[DESTINATION_DIRECTORY]${f#[SOURCE_DIRECTORY]}"; done
It did show the kind of file size reduction you mentioned and the visual quality was the same, or quite close, to the RGB originals; I archived the originals.

Again, thank you for your help.

Re: Converting to monochrome images from RGB save little size

Posted: 2018-10-19T13:31:17-07:00
by snibgo
Oops, sorry, my copy-paste went wrong. That last "-channel" should be "+channel", which restores the channel settings to the default.