使用ImageMagick压缩PNG图像 [英] Compress a PNG image with ImageMagick

查看:1758
本文介绍了使用ImageMagick压缩PNG图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要压缩JPEG图片,我可以:

  $ thumb = new Imagick 
$ thumb-> readImage(url);
$ thumb-> setImageCompression(Imagick :: COMPRESSION_JPEG);
$ thumb-> setImageCompressionQuality(80);但是,我还需要压缩PNG图像(保留Alpha透明度)以保持大小不变​​。



<有没有办法用ImageMagick做?

解决方案

pngquant 量化或减少图像中的颜色数量,直到恰好在可辨别的质量下降之前。你可以在ImageMagick中尝试类似这样的东西...



首先,使用内置的 rose: ,检查图片中的颜色数 - 是3,019:

  convert rose:-format%k info:
3019

并制作 PNG 并检查大小 - 是6,975字节

  convert rose:rose.png 
ls -l rose.png
-rw-r - r - @ 1 mark staff 6975 5 Sep 20:57 rose.png



现在将玫瑰转换为255种颜色,并检查大小 - 最小为3,691字节:

  convert rose:-colors 255 rose255.png 
ls -l rose255.png
-rw- r - r-- 1 mark staff 3691 5 Sep 21:02 rose255.png



现在将玫瑰色转换为64种颜色,并检查尺寸 - 最小为2,361字节

  convert rose:-colors 64 rose64.png 
ls -l rose64.png
-rw-r - r-- 1 mark staff 2361 5 Sep 21:04 rose64.png



另一种优化或减少PNG文件大小的方法是使用 -strip 删除图片中的任何元数据,例如拍摄照片的日期和时间,相机和镜头型号,创建图像的程序名称以及版权和颜色配置文件。



此外,注意...通常,透明像素的颜色是不相关的,因为你看不到它们,但统一的东西通常压缩得更好。因此,通过使用 -alpha background ,保存PNG文件时,使所有透明像素的颜色相同是个好主意。



#创建随机噪声的图像
-rw-r - r - @ 1 mark staff 1576107 6 Sep 11:37 a.png#157kB

convert -size 512x512 xc:gray + noise random -alpha transparent a.png#recreate but make transparent
-rw-r - r - @ 1 mark staff 1793567 6 Sep 11:38 a.png#179kB,extra transparency channel

convert -size 512x512 xc:gray + noise random -alpha transparent -alpha background a.png#make all tr​​ansparent pixels black
-rw-r - r - @ 1 mark staff 1812 6 Sep 11 :38 a.png#Presto!


To compress a JPEG image, I can do:

$thumb = new Imagick();
$thumb->readImage("url");
$thumb->setImageCompression(Imagick::COMPRESSION_JPEG);
$thumb->setImageCompressionQuality(80);

However, I need to also compress PNG images (preserving alpha transparency) to keep sizes down. Is there a way to do it with ImageMagick?

解决方案

pngquant effectively quantizes, or reduces the number of colours in an image till just before there is a discernible drop in quality. You can try something similar in ImageMagick like this...

First, using the built-in rose: image, check the number of colours in the image - it is 3,019:

convert rose: -format %k info:
3019

and make a PNG of it and check the size - it is 6,975 bytes

convert rose: rose.png
ls -l rose.png
-rw-r--r--@ 1 mark  staff  6975  5 Sep 20:57 rose.png

Now convert the rose to 255 colours and check the size - it is down to 3,691 bytes:

convert rose: -colors 255 rose255.png
ls -l rose255.png
-rw-r--r--  1 mark  staff   3691  5 Sep 21:02 rose255.png

Now convert the rose to 64 colours and check the size - down to 2,361 bytes

convert rose: -colors 64 rose64.png
ls -l rose64.png
-rw-r--r--  1 mark  staff  2361  5 Sep 21:04 rose64.png

Another way of optimising or reducing PNG filesizes is to use -strip to strip out any metadata from images - such as the date and time the picture was taken, the camera and lens model, the name of the program that created the image and the copyright and colour profiles.

Also, worth bearing in mind... normally, the colour of transparent pixels is irrelevant because you can't see them, but uniform things generally compress better. So, it may be a good idea to make all transparent pixels the same colour when saving PNG files, by using -alpha background.

Example

convert -size 512x512 xc:gray +noise random a.png                                      # create an image of random noise
-rw-r--r--@ 1 mark  staff  1576107  6 Sep 11:37 a.png                                  # 157kB

convert -size 512x512 xc:gray +noise random -alpha transparent a.png                   # recreate but make transparent
-rw-r--r--@ 1 mark  staff  1793567  6 Sep 11:38 a.png                                  # 179kB, extra transparency channel

convert -size 512x512 xc:gray +noise random -alpha transparent -alpha background a.png # make all transparent pixels black
-rw-r--r--@ 1 mark  staff  1812  6 Sep 11:38 a.png                                     # Presto!

这篇关于使用ImageMagick压缩PNG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆