dongzhi5386 2014-11-25 16:37
浏览 88

如何使用Imagick将JPG图像保存在4:2:0颜色空间中?

Google Pagespeed can convert RGB images to the YUV 4:2:0 colourspace. I would like to do the same in PHP using imagick.

Briefly, on why 4:2:0 is desired:

"This filter reduces the color sampling of jpeg images to 4:2:0. Human vision is much more sensitive to changes in brightness than changes in hue or saturation, and 4:2:0 color sampling allows us to keep brightness data while reducing the amount of hue and saturation data by 3/4. This can significantly reduce the image size while having only a small effect on perception."

I have tried to change the colorspace with imagemagick to YUV but it is not working at all well! The white backdrop goes to green and the rest of the colours are wrong, plus inverted luminance.

The following code is useless, but it shows a few of the things I have tried.

With the Google format created by mod_pagespeed the resultant images look fine in a browser. That is what I am hoping to achieve, images that are for the web.

    $image_info = getimagesize($source_file);
    $im = new Imagick();
    $im->readImage($source_file);
    $profiles = $im->getImageProfiles('*', false);
    $has_icc_profile = (array_search('icc', $profiles) !== false);
    if ($has_icc_profile === false) {
        $icc_srgb = file_get_contents(Mage::getBaseDir('var') . DS . 'metodo' . DS . 'demo_data' . DS . 'AdobeRGB1998.icc');
        $im->profileImage('icc', $icc_srgb);
        unset($icc_srgb);
    }
    //$im->setImageColorspace(1);
    $im->setInterlaceScheme(Imagick::INTERLACE_PLANE);
    $im->setImageCompressionQuality(85);
    //$im->stripImage(); 
    $im->setImageColorspace(11);
    $im->thumbnailImage($this->_imageSrcWidth, $this->_imageSrcHeight);
    //$im->negateImage(false, Imagick::CHANNEL_ALL);
    $im->stripImage();
    $im->writeImage($fileName);
  • 写回答

2条回答 默认 最新

  • duangan1945 2014-11-26 10:30
    关注

    Maybe not technically an answer, but too awkward to format as a comment, and it may help find the answer.

    You can do this sort of thing that may provide some insight as to how to use parameters and what they seem to do:

    convert input.jpg -colorspace YUV out.jpg
    convert input.jpg -colorspace YUV -sampling-factor 4:2:2 out442.jpg
    convert input.jpg -colorspace YUV -sampling-factor 4:2:0 out420.jpg
    

    then you can see the resulting filesizes for a variety of settings. It does seem that the output filesizes correlate pretty well with the sampling, and that IM's default sampling is 4:4:4 since that matches the size when I don't provide any -sampling-factor:

    ls -l out*
    -rw-r--r--  1 mark  staff  105563 26 Nov 10:23 out.jpg
    -rw-r--r--  1 mark  staff   77230 26 Nov 10:23 out410.jpg
    -rw-r--r--  1 mark  staff   81755 26 Nov 10:24 out411.jpg
    -rw-r--r--  1 mark  staff   82835 26 Nov 10:23 out420.jpg
    -rw-r--r--  1 mark  staff  105563 26 Nov 10:25 out444.jpg
    

    This link may help.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?