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?
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.
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.
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.
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.
ImageMagick provides the -compress
switch, which might do what you want.
-compress
: Use pixel compression specified by type when writing the imageChoices 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With