Page 1 of 1

Optimal settings for jpeg->gif conversion...

Posted: 2014-08-18T17:15:47-07:00
by johnoyler
I have many 1500x2200 monochrome jpegs. These are scans of text documents, and they range in size from about 600k on up to around 1meg. I'd like to reduce the filesize without losing (much) clarity, and I suspect gifs might be ideal for this.

A friend at work helped me run a test, and using Adobe Photoshop, he was able to convert it to "gif restricted" or maybe "gif restrictive", and the filesize on our sample image dropped from 600k down to about 154k. I believe it auto-indexed to either 5 or 6 graytones.

I am unable to do similar with IM, and even when I reduce filesize it's at the expense of image quality. Is there a recipe for this sort of thing that would provide decent results? Is Adobe using some proprietary algorithm for this sort of thing?

Re: Optimal settings for jpeg->gif conversion...

Posted: 2014-08-18T17:33:54-07:00
by fmw42
Post an example image to some free hosting service such as dropbox.com and put the URL here, so we can test with one of your images.

When you say your image is monochrome, do you mean binary or just grayscale?

If the latter, try

Code: Select all

convert image.jpg -colors 6 result.gif
or

Code: Select all

convert image.jpg +dither -colors 6 result.gif

Re: Optimal settings for jpeg->gif conversion...

Posted: 2014-08-18T18:34:14-07:00
by johnoyler
http://i.imgur.com/0ulsipl.jpg

As you can see, there's alot of speckling. The despeckle filter in Gimp just makes a mess of it. Adobe's own filter did little better... we got the best results just converting it (in Photoshop) to gif.

I don't intend to OCR these, so don't worry about that as a consideration. Only whether it degrades readability for the human eye.

Re: Optimal settings for jpeg->gif conversion...

Posted: 2014-08-18T18:48:36-07:00
by snibgo
You don't have anti-aliasing, so you might as well make it black and white.

Code: Select all

F:\web\im>%IM%convert 0ulsipl.jpg -threshold 50%% -despeckle -despeckle -despeckle -type bilevel s.png
The .gif version is slightly larger.

Re: Optimal settings for jpeg->gif conversion...

Posted: 2014-08-18T18:52:46-07:00
by fmw42
try this

Code: Select all

convert 0ulsipl.jpg -morphology close diamond:1 -colors 2 test.gif
file size = 129 KB

Re: Optimal settings for jpeg->gif conversion...

Posted: 2014-08-18T20:11:13-07:00
by johnoyler
Wow. I thought you must have had it wrong until I tried it... 2 colors is enough for this to work.

I bet even more could be shaved off if I could clean up the shadow on the righthand side.

Thank you, this is a big help.

Re: Optimal settings for jpeg->gif conversion...

Posted: 2014-08-18T21:11:17-07:00
by fmw42
I do not think it would be any smaller in file size.

Also to be safe, the command should probably be

Code: Select all

convert image.jpg -colorspace gray -morphology close diamond:1 -colors 2 result.gif

Re: Optimal settings for jpeg->gif conversion...

Posted: 2014-08-19T19:26:39-07:00
by johnoyler
I've ran into a slight problem. I am confident that IM is creating a valid and correct png, but my preferred viewing software is acting dorky. The files that happen to have black/000 at index 0 in the color map/index appear correct, but those that have white/fff at index 0 appear inverted in the viewing software.

Is there a way to swap index 0 and index 1 with a convert command?

Is there a way to force black to index 0 when I do future conversions? I'm not much of an expert on terminology so it's making it difficult to google for an answer. I've figured out how to do it in Gimp (and confirmed that it does fix the problem)... Colors (menu), Map, Rearrange Colormap ... but I honestly don't want to use that for several hundred files.

Anyway, thanks in advance. I appreciate all the help.

Re: Optimal settings for jpeg->gif conversion...

Posted: 2014-08-19T19:30:45-07:00
by fmw42
Using your image from above, if I do

Code: Select all

convert image.jpg -colorspace gray -morphology close diamond:1 -colors 2 -auto-level result.gif
then the result has white as the 0 entry in the colormaps and black as the 1 entry.

However, by using -remap with the following ordered colormap image, I can reverse the colormap.

Code: Select all

convert -size 1x1 xc:black xc:white +append bw.gif
convert image.jpg -colorspace gray -morphology close diamond:1 -colors 2 -auto-level -remap bw.gif result2.gif