如何阻止 GD2 在调整图像大小时洗掉颜色?

发布于 2024-11-03 10:00:08 字数 1589 浏览 5 评论 0原文

我使用 CodeIgniter 1.7 开发了一个照片共享社区网站。成员上传的照片会自动调整为多种格式的大小,为此我使用 CodeIgniter Image Manipulation 类。该类内置于框架中,基本上是多个图像处理库的包装器,例如 GD、GD2、ImageMagick 和 NETPBM。在我的主机上,我只能使用 GD2,所以这就是这个问题的所在。

说到我的问题。这是我网站上调整大小的照片的示例。请注意,原件非常大,超过 3000 像素宽:

http://www.jungledragon .com/image/195/female_impala_close-up.html

现在,在 Flickr 上看一下同一张图片,也调整了大小,只是大了一点:

http://www.flickr.com/photos/fledder/3763538865/in/set -72157621744113979

看到显着的差异了吗?我正在努力弥合这个巨大的差距。我做的第一件事就是对图像应用锐化滤镜。你可以在这里看到结果:

在此处输入图像描述

虽然仍然不完美,但它至少接近 Flickr 的清晰度水平图像。剩下的问题是颜色被洗掉了,就好像它们的饱和度降低了一样。这已经发生在锐化过滤器之前,因此它必须在 GD2 中。

这个问题对我来说非常重要,但我不知道该去哪里寻找。我发现一些 .NET 线程讨论色度子采样,但我不知道在我的设置中如何处理这些信息。我正在寻找在我的设置限制内有效的任何解决方案。

更新:原始文件,与我上传到我的网站和 Flickr 的完全相同:

http://www.jungledragon.com/img/DSC07275.jpg

更新2:我很震惊。以一种好的方式。我花了很多时间安装 ImageMagick,但是切换到它之后(这是将“imagemagick”设置为在 Code Igniter 图像处理类中使用的库的问题,测试图像的结果如下:

在此处输入图像描述

ImageMagick 的大小调整完全按照预期进行,并且清晰度也得到了保留。禁用了我的自定义锐化例程,因为 ImageMagick 不再需要它。最重要的是,该过程更快,占用的内存也更少。这是另一个重要部分:我无法解释它,但我绝对没有做任何事情。告诉 ImageMagick 使用特定的颜色配置文件,这是用户 @Alix 所建议的。到目前为止,无论是否嵌入配置文件,颜色信息都受到尊重。输出只是 ImageMagick 的较小版本。真的那么聪明还是我做梦?

I have developed a photo sharing community site using CodeIgniter 1.7. Photos that are uploaded by members are automatically resized in a number of formats, for which I use the CodeIgniter Image Manipulation class. This class is built into the framework and basically a wrapper around multiple image manipulation libraries, such as GD, GD2, ImageMagick, and NETPBM. On my host, I can only make use of GD2, so that's where this question will be about.

On to my problem. Here is an example of a resized photo on my site. Note that the original was very large, over 3000px wide:

http://www.jungledragon.com/image/195/female_impala_close-up.html

Now, look at that same image, also resized, just a bit larger at Flickr:

http://www.flickr.com/photos/fledder/3763538865/in/set-72157621744113979

See the dramatic difference? I'm trying to bridge that huge gap. The first thing I did was to apply a sharpen filter to the images. You can see the result here:

enter image description here

Although still not perfect, it at least approaches the sharpness level of the Flickr image. The remaining problem is that the colors are washed away, as if their saturation is decreased. This happens before the sharpening filter already, so it must be in GD2.

This issue is vitally important to me, but I don't know where to look. I've found some .NET threads talking about chroma sub sampling but I don't know what to do with that information in my setup. I'm looking for any solution that works within the constraints of my setup.

Update: Hereby the original file, exactly as I uploaded it both to my site and Flickr:

http://www.jungledragon.com/img/DSC07275.jpg

Update 2: I'm shocked. In a good way. It took me a lot of pain to install ImageMagick but after switching to it (which was a matter of setting 'imagemagick' as the library to use at the Code Igniter image manipulation class, the result of the test image is as follow:

enter image description here

ImageMagick's resizing is doing it exactly as intended. The colors are preserved, and the sharpness is there. Yes, I disabled my custom sharpening routine since it is no longer needed due to ImageMagick. On top of that, the process is a lot faster and less memory hungry too. And here comes another great part: I cannot explain it, but I did absolutely nothing to tell ImageMagick to use a specific color profile, which was suggested by user @Alix. In my testing so far it looks like the color information is respected with or without an embedded profile. The output simply is a smaller version of the input. Is ImageMagick really that smart or am I dreaming?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我一向站在原地 2024-11-10 10:00:08

我已经设法用 Imagick 进一步测试:

Imagick sRGB Test

图像的左半部分是用 Imagick 处理的, sRGB_IEC61966-2-1_no_black_scaling.icc 颜色配置文件,右半部分没有关联的颜色配置文件,如果使用 Imagick 或 GD 处理,则显示完全相同;这是我使用的代码:

header('Content-type: image/jpeg');

$image = new Imagick('/path/to/DSC07275.jpg');

if (($srgb = file_get_contents('http://www.color.org/sRGB_IEC61966-2-1_no_black_scaling.icc')) !== false)
{
    $image->profileImage('icc', $srgb);
    $image->setImageColorSpace(Imagick::COLORSPACE_SRGB);
}

$image->thumbnailImage(1024, 0);

echo $image;

这是 color.org 网站上提供的几个 sRGB 配置文件的比较:

sRGB Comparison

在我看来,第三种配置文件产生了最生动的结果,除此之外我不知道如何做出明确的选择。


编辑:显然,Imagick 附带了捆绑的 sRGB 配置文件,因此您无需从图像颜色联盟网站下载该配置文件,以下代码应该可以处理所有情况:

header('Content-type: image/jpeg');

$image = new Imagick('/path/to/DSC07275.jpg');
$version = $image->getVersion();
$profile = 'http://www.color.org/sRGB_IEC61966-2-1_no_black_scaling.icc';

if ((is_array($version) === true) && (array_key_exists('versionString', $version) === true))
{
    $version = preg_replace('~ImageMagick ([^-]*).*~', '$1', $version['versionString']);

    if (is_file(sprintf('/usr/share/ImageMagick-%s/config/sRGB.icm', $version)) === true)
    {
        $profile = sprintf('/usr/share/ImageMagick-%s/config/sRGB.icm', $version);
    }
}

if (($srgb = file_get_contents($profile)) !== false)
{
    $image->profileImage('icc', $srgb);
    $image->setImageColorSpace(Imagick::COLORSPACE_SRGB);
}

$image->thumbnailImage(1024, 0);

echo $image;

I've managed to further test this with Imagick:

Imagick sRGB Test

The left half of the image was processed with Imagick and the sRGB_IEC61966-2-1_no_black_scaling.icc color profile, the right half has no color profile associated and shows exactly the same if processed with Imagick or GD; here is the code I used:

header('Content-type: image/jpeg');

$image = new Imagick('/path/to/DSC07275.jpg');

if (($srgb = file_get_contents('http://www.color.org/sRGB_IEC61966-2-1_no_black_scaling.icc')) !== false)
{
    $image->profileImage('icc', $srgb);
    $image->setImageColorSpace(Imagick::COLORSPACE_SRGB);
}

$image->thumbnailImage(1024, 0);

echo $image;

Here is a comparison of the several sRGB profiles available on the color.org website:

sRGB Comparison

It seems to me that the third profile produces the most vivid results, other than that I have no idea how one would make a definitive choice.


EDIT: Apparently, Imagick comes with a bundled sRGB profile, so you don't need to download the one from the Image Color Consortium website, the following code should handle all scenarios:

header('Content-type: image/jpeg');

$image = new Imagick('/path/to/DSC07275.jpg');
$version = $image->getVersion();
$profile = 'http://www.color.org/sRGB_IEC61966-2-1_no_black_scaling.icc';

if ((is_array($version) === true) && (array_key_exists('versionString', $version) === true))
{
    $version = preg_replace('~ImageMagick ([^-]*).*~', '$1', $version['versionString']);

    if (is_file(sprintf('/usr/share/ImageMagick-%s/config/sRGB.icm', $version)) === true)
    {
        $profile = sprintf('/usr/share/ImageMagick-%s/config/sRGB.icm', $version);
    }
}

if (($srgb = file_get_contents($profile)) !== false)
{
    $image->profileImage('icc', $srgb);
    $image->setImageColorSpace(Imagick::COLORSPACE_SRGB);
}

$image->thumbnailImage(1024, 0);

echo $image;
梦里泪两行 2024-11-10 10:00:08

您的原始图像附有 Adobe RGB (1998) ICC 配置文件。我认为 GD 由于不了解配置文件,因此错误地解释了图像数据。这是确认这一点的相关 PHP 错误

您需要准备具有正确配置文件的图像,很可能是 sRGB。
如果您有可以执行此操作的应用程序,请尝试将其转换为 sRGB 并重新上传。

如果您需要永久的服务器端解决方案来解决问题,我认为您将需要一个可以处理配置文件的图像处理库。说实话,我不知道ImageMagick是如何处理这些事情的,但至少它熟悉颜色配置文件的基本概念。

相关:sRGB 和 Adob​​e RGB 的比较

Your original image has a Adobe RGB (1998) ICC profile attached. I think GD, not knowing about profiles, is interpreting the image data incorrectly. Here's a related PHP bug confirming this.

You would need to prepare the image with the correct profile, most likely sRGB.
If you have an application that can do that, try converting it to sRGB and re-uploading.

If you need a permanent server-side solution to the problem, I think you will need an image processing library that can deal with profiles. To be honest, I don't know how ImageMagick deals with these things, but at least it is familiar with the basic concept of colour profiles.

Related: Comparison of sRGB and Adobe RGB

玩套路吗 2024-11-10 10:00:08

我有一点信息可以为这个线程做出贡献。我是一名摄影师,而不是网络开发人员,所以我的技术知识不是很好,但是,我一直在处理这个问题,所以我希望这篇文章能够对其他人有所帮助。

我使用在线照片销售工具,使用 GD 对我的所有图像进行重新采样。在我的广色域显示器上查看时,即使使用正确的 sRGB 转换和附加的 ICC 配置文件上传,我也遇到了图像看起来很时髦的问题。

我发现问题是 GD 从原始文件中删除了所有元数据和 ICC 配置文件。然后浏览器看不到任何配置文件,因此无法正确显示它们。它在标准色域显示器上很轻微,但在广色域上非常明显。

如果您遇到问题,您可以使用 Firefox 并更改 about:config 中的设置来测试我的理论。将“gfx.color_management.mode”的值从默认值“2”更改为“1”。此设置将强制 Firefox 假定任何没有 ICC 配置文件的图像都是 sRGB 并按此显示。然后图像应该如您所期望的那样显示,并且与 Photoshop/Lightroom/等相同。为什么所有浏览器都不使用这种常识性方法作为默认方法,这超出了我的理解范围。

不幸的是,我的购物车仅设置为使用 PHP GD,因此我目前无法获得良好的结果。我真的很希望看到 GD 更新以保留附加的 ICC 配置文件,或者可以选择在导出时添加简单的 sRGB 配置文件。

更多信息请点击这里:
http://www.gballard.net/psd/go_live_page_profile/embeddedJPEGprofiles.html#

I have a small bit of info to contribute to this thread. I'm a photographer and not a web developer so my technical knowledge is not great, however, I have been dealing with this issue so I hope this post will help someone else down the line.

I use an online photo sales tool that resamples all my images using GD. I was having issues with images looking way funky even when uploaded with proper sRGB conversion and ICC profiling attached, when viewing on my wide-gamut monitor.

What I found the problem to be is that GD strips all metadata and ICC profiles from the original files. Then the browsers, not seeing any profile, are not displaying them correctly. It's slight on a standard-gamut monitor but very obvious on wide-gamut.

If you're having problems with this, you can test my theory by using Firefox and changing a setting in about:config. Change the value of "gfx.color_management.mode" from the default of "2" to "1". This setting will force Firefox to assume any image without an ICC profile is sRGB and will display it as such. Images should then appear as you would expect and identical to Photoshop/Lightroom/etc. Why all browsers do not use this common-sense approach as their default is beyond me.

Unfortunately, my shopping cart is only setup to use PHP GD so I cannot get good results at this time. I would really like to see GD updated to leave ICC profiles attached or to have the option of adding a simple sRGB profile on export.

More info here:
http://www.gballard.net/psd/go_live_page_profile/embeddedJPEGprofiles.html#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文