General Purpose Conversion: Photos -> 1-Bit?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
OblivionSignal
Posts: 2
Joined: 2014-04-30T11:57:42-07:00
Authentication code: 6789

General Purpose Conversion: Photos -> 1-Bit?

Post by OblivionSignal »

Hello folks,

New IM user. First, thanks for this incredibly powerful CLI application. I simply love it.

What I'd like to do is develop an efficient, general purpose script for converting color photographs into 1-Bit GIF format. The end goal is to get the smallest file size, and still retain enough visual information with the pixels so the photograph (for instance a face) can be positively recognized when compared with the original.

Original Images range in size typically from a maximum dimension of 800 to 1400, and typically in PNG or JPG format. We're not dealing with huge, high-dimension tiffs here.

I'm a Linux user, and using the Bash shell for handling IM from the console for batch processing. I've tried quite a few options with convert. The order in which these are processed, from left to right, seems to be significant. My knowledge of image processing is at the hobbyist level.

A list of things I've tried:

Code: Select all

-ordered-dither (with all built-in options such as o2x2 / o4x4 / h4x4a / h4x4o etc]
-colorspace gray
-sigmoidal-contrast [with different settings]
-monochrome
-colors 2
Putting these in different orders changes the quality of the dither, and the different ordered-dither options can have a dramatic effect on the features of a processsed photograph. I'm well aware of the fact that it's typically more efficient just to use the JPG format with a lower quality to optimize file size, but I'd like to see if anyone has come up with a more-or-less "standard" method using convert to yield the best possible 2-color result.

Thanks for your time!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: General Purpose Conversion: Photos -> 1-Bit?

Post by fmw42 »

There is not likely any one universal method, because it is image dependent. Some image have histograms that are bimodal (two major peaks) or multimodal (many major peaks) and another have only a single mode (one major peak). There are a number of approaches and as you point out may depend upon the order used. But some simple cases are:

1) +dither -depth X -colors Y (limit the depth before reducing colors)
2) -monochrome
3) -threshold

Then there are some special algorithms for doing this, for which I have scripts at the links below.

Otsu thresholding
Kmeans thresholding
Fuzzy Cmeans thresholding
Entropy Thresholding
Triangle Thresholding

You can see a comparison study that I made some while ago at http://www.fmwconcepts.com/imagemagick/ ... /index.php

Edit: Sorry I think I misunderstood your objectives.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: General Purpose Conversion: Photos -> 1-Bit?

Post by glennrp »

I get pretty good results for portraits with -ordered-dither 4x4, after reducing the JPEG to grayscale.
OblivionSignal
Posts: 2
Joined: 2014-04-30T11:57:42-07:00
Authentication code: 6789

Re: General Purpose Conversion: Photos -> 1-Bit?

Post by OblivionSignal »

@fmw42
Thanks, your site is extremely informative. No problem on "missing my objectives."

Playing with threshold is interesting, but it doesn't retain details very well. And of course I'm not under the naive illusion that there will ever be a "one size fits all" solution here, but I'm curious if a bit of tinkering and combining methods can get me somewhere close.

I need to learn more about histograms, and bimodal/multimodal peaks. But I'm instantly having the thought: if you could match histograms, at least whether it's bimodal, or multimodal, across a set of photos.

Let's say I have 3 photographs, call them A, B, and C. Let's say A is bimodal, and B and C are multimodal. If I could manipulate A's histogram to be multimodal, it might be a way to generate a "baseline" for them. Or if it would be more optimal to manipulate B and C into a bimodal histogram.

A layman's thoughts, and I'm sure I've said something rather ignorant. But you never know.

If that were possible, though, then maybe getting them converted to identical colorspaces, before trying to dither etc. Maybe that would be a step in the right direction.

@glennrp
Thanks, I'm going to try that way of ordering the process.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: General Purpose Conversion: Photos -> 1-Bit?

Post by fmw42 »

I will have to research adding the dithering process to see how robust that is with respect to using some of my thresholding. It would appear to be a good idea.
Post Reply