ImageMagick / Mogrify - compress image programmatically

I am trying to compress an image on the command line using Imagemagick in Perl (currently I can only flip it ...)

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

The image should be compressed at a size of 50%, but keep the same size ! I can resize the image, but how to resize the image pixel to reduce the resolution, but keep the same size?

I hope someone out there can help.

Thank you very much in advance

+7
source share
1 answer

ImageMagick provides a -compress switch that can do what you want.

-compress : use the pixel compression specified by type when recording the image

Options include: 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 save the binary image in uncompressed format. By default, the compression type of the specified image file is used.

If LZW compression is specified but LZW compression is not enabled, image data is written in the uncompressed LZW format, which can be read by LZW decoders. This can lead to larger than expected GIF files.

Lossless refers to lossless JPEG, which is only available if the JPEG library has been fixed to support it. Using lossless JPEGs is usually not recommended.

Use the -quality parameter to set the compression level that JPEG, PNG, MIFF, and MPEG encodings will use. Use the -sampling-factor parameter to set the sampling rate, which will be used by JPEG, MPEG, and YUV encodings to sample color 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 still viewable 
+12
source

All Articles