Mohsen Shakiba
Mohsen Shakiba

Reputation: 1872

Optimize image with GraphicsMagick

I'm using GraphicsMagick in node.js and I have lots(thousands) that I need to optimize as much as possible. Each one is 250*250 PX and I need to make them 50*50 but this is the easy part.

The problem is how to optimize them too.

By optimizing them I mean make them into jpeg or png(which ever is smaller) and lower the quality(if needed) and other things...

my code so far...

gm(temp_location + file_name)
.gravity('Center')
.extent(50, 50)
.noProfile()
.write(new_location + "s"+name, function (err) {});

so any suggestion on how to make the image any smaller?
any suggestion is appreciated

Upvotes: 1

Views: 2812

Answers (2)

elbloco80
elbloco80

Reputation: 11

A complement to the above answer, concerning GIF format optimization.

GraphicsMagick documentation is very clear regarding JPEG or PNG compression however when outputting GIF files (this is my case...), these compression functions do not seem to apply.

I managed to reduce my GIF output file size by 52% by using this code:

var gm = require('gm');
gm()
.in(input_filename1)
.in(input_filename2)    
.bitdepth(8)
.colors(192)
.dither(false)
.filter('Point') // filter before resize to have a 'sharp big pixels' aspect
.antialias(false)
.resize(magnify_factor*size_x, magnify_factor*size_y, "!") 
.write(output_filename, function (err) {
    if (!err) console.log('gif file created!');
});

Here are other functions I tried but seemed to have no effect when dealing with GIFs

.limit("disk", "500KB") 
// this limits resource used but does not limit the output filesize

.quality(10)

.compress('LZW')

Upvotes: 0

Glenn Randers-Pehrson
Glenn Randers-Pehrson

Reputation: 12455

For JPEG (which is most suitable for photos and other natural images), lowering the quality (which means image quality) will help. Try -quality 40 or less.

For PNG (which is most suitable for line art), limiting the colors to 254 or fewer and increasing the quality (which actually means compression level) might help. For PNG with a limited number of colors, use a "quality" that is an even multiple of 10, because the second digit specifies the PNG filter method which should be "0" which means no filtering. Try -quality 90.

Upvotes: 2

Related Questions