Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 413
Eamorr Avatar asked Jun 13 '11 14:06

Eamorr


People also ask

How do I resize an image in ImageMagick?

To resize an image to specific dimensions, use the convert command with an input file, the -resize parameter, your preferred dimensions, and an output filename: convert original. png -resize 100x100 new. png.

What does Mogrify mean?

Mogrify transforms an image or a sequence of images. These transforms include image scaling, image rotation, color reduction, and others. The transmogrified image overwrites the original image, unless an option such as -format causes the output filename to be different from the input filename.

Is ImageMagick good?

If you're not sure what ImageMagick is, it's one of the greatest tools you could have on your computer, to manipulate images and a few other types of files.

What is ImageMagick Ubuntu?

ImageMagick is a software suite which can be used to create, edit and display bitmap images, either from the command line or using a graphical interface. It can read, convert and write images in a large variety of formats.


1 Answers

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
like image 95
c00kiemon5ter Avatar answered Nov 05 '22 00:11

c00kiemon5ter