Eamorr
Eamorr

Reputation: 10022

ImageMagick/Mogrify - compress image programmatically

I'm trying to compress an image on the command line using Imagemagick in Perl (currently, I'm only able to flip it...)

system("/usr/bin/mogrify", "-flip","/var/www/images/$pid-$count.jpg");

The image must be compressed in size by 50%, but retain the same dimensions! I can resize an image fine, but how to pixelate the image to lower the resolution, but keep the same dimensions?

Upvotes: 9

Views: 14909

Answers (1)

c00kiemon5ter
c00kiemon5ter

Reputation: 17674

ImageMagick provides the -compress switch, which might do what you want.

-compress: Use pixel compression specified by type when writing the image

Choices are: None, BZip, Fax, Group4, JPEG, JPEG2000, Lossless, LZW, RLE or Zip.

To print a complete list of compression types, use -list compress.

Specify +compress to store the binary image in an uncompressed format. The default is the compression type of the specified image file.

If LZW compression is specified but LZW compression has not been enabled, the image data is written in an uncompressed LZW format that can be read by LZW decoders. This may result in larger-than-expected GIF files.

Lossless refers to lossless JPEG, which is only available if the JPEG library has been patched to support it. Use of lossless JPEG is generally not recommended.

Use the -quality option to set the compression level to be used by JPEG, PNG, MIFF, and MPEG encoders. Use the -sampling-factor option to set the sampling factor to be used by JPEG, MPEG, and YUV encoders for down-sampling the chroma channels.


check this example/experiment:

>>> du data/lena.png 
464K    data/lena.png
>>> cp data/lena.png .
>>> convert lena.png lena.jpg
>>> du lena.jpg 
76K lena.jpg           # already a lot smaller by going png --> jpeg
>>> mogrify -compress JPEG -quality 5 lena.jpg
>>> du lena.jpg 
8.0K    lena.jpg       # well, it did compress a lot and it's still viewable

Upvotes: 16

Related Questions