简体   繁体   English

ImageMagick/Mogrify - 以编程方式压缩图像

[英]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...)我正在尝试使用 Perl 中的 Imagemagick 在命令行上压缩图像(目前,我只能翻转它......)

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?图像必须压缩 50%,但保持相同的尺寸,我可以很好地调整图像的大小,但是如何像素化图像以降低分辨率,但保持相同的尺寸?

ImageMagick provides the -compress switch, which might do what you want. ImageMagick 提供了-compress开关,它可能会做你想做的事。

-compress : Use pixel compression specified by type when writing the image -compress : 写入图像时使用类型指定的像素压缩

Choices are: None, BZip, Fax, Group4, JPEG, JPEG2000, Lossless, LZW, RLE or Zip.选项包括:无、BZip、传真、Group4、JPEG、JPEG2000、无损、LZW、RLE 或 Zip。

To print a complete list of compression types, use -list compress .要打印完整的压缩类型列表,请使用-list compress

Specify +compress to store the binary image in an uncompressed format.指定+compress以未压缩格式存储二进制图像。 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.如果指定了 LZW 压缩但未启用 LZW 压缩,则图像数据以 LZW 解码器可以读取的未压缩 LZW 格式写入。 This may result in larger-than-expected GIF files.这可能会导致 GIF 文件超出预期。

Lossless refers to lossless JPEG, which is only available if the JPEG library has been patched to support it. Lossless 是指无损 JPEG,只有在 JPEG 库已修补支持它时才可用。 Use of lossless JPEG is generally not recommended.通常不建议使用无损 JPEG。

Use the -quality option to set the compression level to be used by JPEG, PNG, MIFF, and MPEG encoders.使用-quality选项设置 JPEG、PNG、MIFF 和 MPEG 编码器使用的压缩级别。 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.使用-sampling-factor选项设置 JPEG、MPEG 和 YUV 编码器用于对色度通道进行下采样的采样因子。


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM