Optimal settings for jpeg->gif conversion...

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
johnoyler
Posts: 4
Joined: 2014-08-18T17:00:54-07:00
Authentication code: 6789

Optimal settings for jpeg->gif conversion...

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
johnoyler
Posts: 4
Joined: 2014-08-18T17:00:54-07:00
Authentication code: 6789

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

try this

Code: Select all

convert 0ulsipl.jpg -morphology close diamond:1 -colors 2 test.gif
file size = 129 KB
johnoyler
Posts: 4
Joined: 2014-08-18T17:00:54-07:00
Authentication code: 6789

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
johnoyler
Posts: 4
Joined: 2014-08-18T17:00:54-07:00
Authentication code: 6789

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
Post Reply