C++ libpng 警告:iCCP:已知不正确的 sRGB 配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22745076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
libpng warning: iCCP: known incorrect sRGB profile
提问by rkarami
I'm trying to load a PNG image using SDL but the program doesn't work and this error appears in the console
我正在尝试使用 SDL 加载 PNG 图像,但该程序不起作用并且此错误出现在控制台中
libpng warning: iCCP: known incorrect sRGB profile
libpng 警告:iCCP:已知不正确的 sRGB 配置文件
Why does this warning appear? What should I do to solve this problem?
为什么会出现这个警告?我应该怎么做才能解决这个问题?
回答by Glenn Randers-Pehrson
Libpng-1.6 is more stringent about checking ICC profiles than previous versions. You can ignore the warning. To get rid of it, remove the iCCP chunk from the PNG image.
与以前的版本相比,Libpng-1.6 在检查 ICC 配置文件方面更加严格。您可以忽略该警告。要摆脱它,请从 PNG 图像中删除 iCCP 块。
Some applications treat warnings as errors; if you are using such an application you do have to remove the chunk. You can do that with any of a variety of PNG editors such as ImageMagick's
一些应用程序将警告视为错误;如果您正在使用这样的应用程序,则必须删除该块。您可以使用各种 PNG 编辑器(例如 ImageMagick 的
convert in.png out.png
To remove the invalid iCCP chunk from all of the PNG files in a folder (directory), you can use mogrify
from ImageMagick:
要从文件夹(目录)中的所有 PNG 文件中删除无效的 iCCP 块,您可以mogrify
从 ImageMagick使用:
mogrify *.png
This requires that your ImageMagick was built with libpng16. You can easily check it by running:
这要求你的 ImageMagick 是用 libpng16 构建的。您可以通过运行轻松检查它:
convert -list format | grep PNG
If you'd like to find out which files need to be fixed instead of blindly processing all of them, you can run
如果您想找出哪些文件需要修复而不是盲目地处理所有文件,您可以运行
pngcrush -n -q *.png
where the -n
means don't rewrite the files and -q
means suppress most of the output except for warnings. Sorry, there's no option yet in pngcrush to suppress everything but the warnings.
其中-n
手段不重写文件,-q
手段抑制除警告外的大部分输出。抱歉,pngcrush 中还没有选项可以抑制除警告之外的所有内容。
Binary Releases of ImageMagick are here
ImageMagick 的二进制版本在这里
For Android Projects (Android Studio) navigate into res
folder.
对于 Android 项目 (Android Studio),导航到res
文件夹。
For example:
例如:
C:\{your_project_folder}\app\src\main\res\drawable-hdpi\mogrify *.png
回答by friederbluemle
Use pngcrush
to remove the incorrect sRGB profile from the png file:
用于pngcrush
从 png 文件中删除不正确的 sRGB 配置文件:
pngcrush -ow -rem allb -reduce file.png
-ow
will overwrite the input file-rem allb
will remove all ancillary chunks except tRNS and gAMA-reduce
does lossless color-type or bit-depth reduction
-ow
将覆盖输入文件-rem allb
将删除除 tRNS 和 gAMA 之外的所有辅助块-reduce
无损颜色类型或位深度减少
In the console output you should see Removed the sRGB chunk
, and possibly more messages about chunk removals. You will end up with a smaller, optimized PNG file. As the command will overwrite the original file, make sure to create a backup or use version control.
在控制台输出中,您应该看到Removed the sRGB chunk
,可能还有更多关于块删除的消息。您最终会得到一个更小、经过优化的 PNG 文件。由于该命令会覆盖原始文件,因此请务必创建备份或使用版本控制。
回答by scopchanov
Solution
解决方案
The incorrect profile could be fixed by:
错误的配置文件可以通过以下方式修复:
- Opening the image with the incorrect profile using QPixmap::load
- Saving the image back to the disk (already with the correct profile) using QPixmap::save
- 使用QPixmap::load打开配置文件不正确的图像
- 使用QPixmap::save将图像保存回磁盘(已经使用正确的配置文件)
Note:This solution uses the Qt Library.
注意:此解决方案使用Qt 库。
Example
例子
Here is a minimal example I have written in C++ in order to demonstrate how to implement the proposed solution:
这是我用 C++ 编写的一个最小示例,用于演示如何实现建议的解决方案:
QPixmap pixmap;
pixmap.load("badProfileImage.png");
QFile file("goodProfileImage.png");
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "PNG");
The complete source code of a GUI application based on this example is available on GitHub.
GitHub 上提供了基于此示例的 GUI 应用程序的完整源代码。
UPDATE FROM 05.12.2019:The answer was and is still valid, however there was a bug in the GUI application I have shared on GitHub, causing the output image to be empty. I have just fixed it and apologise for the inconvenience!
2019 年 12 月 5 日更新:答案过去并且仍然有效,但是我在 GitHub 上共享的 GUI 应用程序中存在一个错误,导致输出图像为空。我刚刚修复了它,并为给您带来的不便表示歉意!
回答by Spencer
You can also just fix this in photoshop...
您也可以在photoshop中修复此问题...
- Open your .png file.
- File -> Save As and in the dialog that opens up uncheck "ICC Profile: sRGB IEC61966-2.1"
- Uncheck "As a Copy".
- Courageously save over your original .png.
- Move on with your life knowing that you've removed just that little bit of evil from the world.
- 打开您的 .png 文件。
- 文件 -> 另存为并在打开的对话框中取消选中“ICC 配置文件:sRGB IEC61966-2.1”
- 取消选中“作为副本”。
- 勇敢地保存原始的 .png。
- 继续你的生活,知道你已经从世界上消除了一点点邪恶。
回答by Uflex
To add to Glenn's great answer, here's what I did to find which files were faulty:
为了补充 Glenn 的精彩回答,我做了以下工作来找出哪些文件有问题:
find . -name "*.png" -type f -print0 | xargs \
-0 pngcrush_1_8_8_w64.exe -n -q > pngError.txt 2>&1
I used the find and xargs because pngcrush could not handle lots of arguments (which were returned by **/*.png
). The -print0
and -0
is required to handle file names containing spaces.
我使用了 find 和 xargs 因为 pngcrush 无法处理很多参数(由 返回**/*.png
)。在-print0
和-0
需要包含空格处理文件名。
Then search in the output for these lines: iCCP: Not recognizing known sRGB profile that has been edited
.
然后在输出这些线路搜索:iCCP: Not recognizing known sRGB profile that has been edited
。
./Installer/Images/installer_background.png:
Total length of data found in critical chunks = 11286
pngcrush: iCCP: Not recognizing known sRGB profile that has been edited
And for each of those, run mogrify on it to fix them.
对于每一个,运行 mogrify 来修复它们。
mogrify ./Installer/Images/installer_background.png
Doing this prevents having a commit changing every single png file in the repository when only a few have actually been modified. Plus it has the advantage to show exactly which files were faulty.
这样做可以防止在只有少数文件被实际修改时提交更改存储库中的每个 png 文件。此外,它还具有准确显示哪些文件有问题的优势。
I tested this on Windows with a Cygwin consoleand a zsh shell. Thanks again to Glennwho put most of the above, I'm just adding an answer as it's usually easier to find than comments :)
我使用Cygwin 控制台和 zsh shell在 Windows 上对此进行了测试。再次感谢Glenn提出了上述大部分内容,我只是添加了一个答案,因为它通常比评论更容易找到:)
回答by Devan Williams
Thanks to the fantastic answerfrom Glenn, I used ImageMagik's "mogrify *.png" functionality. However, I had images buried in sub-folders, so I used this simple Pythonscript to apply this to all images in all sub-folders and thought it might help others:
多亏了梦幻般的答案,从格伦,我用ImageMagik的‘mogrify *巴纽’功能。但是,我将图像隐藏在子文件夹中,因此我使用这个简单的Python脚本将其应用于所有子文件夹中的所有图像,并认为它可能对其他人有所帮助:
import os
import subprocess
def system_call(args, cwd="."):
print("Running '{}' in '{}'".format(str(args), cwd))
subprocess.call(args, cwd=cwd)
pass
def fix_image_files(root=os.curdir):
for path, dirs, files in os.walk(os.path.abspath(root)):
# sys.stdout.write('.')
for dir in dirs:
system_call("mogrify *.png", "{}".format(os.path.join(path, dir)))
fix_image_files(os.curdir)
回答by Adriel Jr
There is an easier way to fix this issue with Mac OS and Homebrew:
使用 Mac OS 和 Homebrew 可以更简单地解决此问题:
Install homebrew if it is not installed yet
如果尚未安装,请安装自制软件
$brew install libpng
$pngfix --strip=color --out=file2.png file.png
or to do it with every file in the current directory:
或者对当前目录中的每个文件执行此操作:
mkdir tmp; for f in ./*.png; do pngfix --strip=color --out=tmp/"$f" "$f"; done
It will create a fixed copy for each png file in the current directory and put it in the the tmp subdirectory. After that, if everything is OK, you just need to override the original files.
它将为当前目录中的每个 png 文件创建一个固定副本并将其放在 tmp 子目录中。之后,如果一切正常,您只需要覆盖原始文件即可。
Another tip is to use the Keynote and Preview applications to create the icons. I draw them using Keynote, in the size of about 120x120 pixels, over a slide with a white background (the option to make polygons editable is great!). Before exporting to Preview, I draw a rectangle around the icon (without any fill or shadow, just the outline, with the size of about 135x135) and copy everything to the clipboard. After that, you just need to open it with the Preview tool using "New from Clipboard", select a 128x128 pixels area around the icon, copy, use "New from Clipboard" again, and export it to PNG. You won't need to run the pngfix tool.
另一个技巧是使用 Keynote 和 Preview 应用程序来创建图标。我使用 Keynote 在一张白色背景的幻灯片上绘制它们,大小约为 120x120 像素(使多边形可编辑的选项很棒!)。在导出到预览之前,我在图标周围绘制一个矩形(没有任何填充或阴影,只有轮廓,大小约为 135x135)并将所有内容复制到剪贴板。之后,您只需使用“从剪贴板新建”使用预览工具打开它,在图标周围选择一个 128x128 像素的区域,复制,再次使用“从剪贴板新建”,然后将其导出为 PNG。您不需要运行 pngfix 工具。
回答by George Birbilis
some background info on this:
关于此的一些背景信息:
Some changes in libpng version 1.6+ cause it to issue a warning or even not work correctly with the original HP/MS sRGB profile, leading to the following stderr: libpng warning: iCCP: known incorrect sRGB profile The old profile uses a D50 whitepoint, where D65 is standard. This profile is not uncommon, being used by Adobe Photoshop, although it was not embedded into images by default.
libpng 1.6+ 版中的一些更改导致它发出警告,甚至无法与原始 HP/MS sRGB 配置文件正常工作,从而导致以下标准错误: libpng 警告:iCCP:已知不正确的 sRGB 配置文件 旧配置文件使用 D50 白点,其中 D65 是标准的。这个配置文件并不少见,被 Adobe Photoshop 使用,尽管默认情况下它没有嵌入到图像中。
(source: https://wiki.archlinux.org/index.php/Libpng_errors)
(来源:https: //wiki.archlinux.org/index.php/Libpng_errors)
Error detection in some chunks has improved; in particular the iCCP chunk reader now does pretty complete validation of the basic format. Some bad profiles that were previously accepted are now rejected, in particular the very old broken Microsoft/HP sRGB profile. The PNG spec requirement that only grayscale profiles may appear in images with color type 0 or 4 and that even if the image only contains gray pixels, only RGB profiles may appear in images with color type 2, 3, or 6, is now enforced. The sRGB chunk is allowed to appear in images with any color type.
改进了某些块中的错误检测;特别是 iCCP 块读取器现在对基本格式进行了非常完整的验证。以前接受的一些不良配置文件现在被拒绝,特别是非常旧的损坏的 Microsoft/HP sRGB 配置文件。PNG 规范要求只有灰度配置文件可以出现在颜色类型 0 或 4 的图像中,并且即使图像只包含灰色像素,只有 RGB 配置文件可以出现在颜色类型 2、3 或 6 的图像中,现在已强制执行。sRGB 块可以出现在任何颜色类型的图像中。
(来源:https: //forum.qt.io/topic/58638/solved-libpng-warning-iccp-known-incorrect-srgb-profile-drive-me-nuts/16)
回答by slurry
After trying a couple of the suggestions on this page I ended up using the pngcrush solution. You can use the bash script below to recursively detect and fix bad png profiles. Just pass it the full path to the directory you want to search for png files.
在尝试了此页面上的一些建议后,我最终使用了 pngcrush 解决方案。您可以使用下面的 bash 脚本递归地检测和修复错误的 png 配置文件。只需将完整路径传递给您要搜索 png 文件的目录即可。
fixpng "/path/to/png/folder"
The script:
剧本:
#!/bin/bash
FILES=$(find "" -type f -iname '*.png')
FIXED=0
for f in $FILES; do
WARN=$(pngcrush -n -warn "$f" 2>&1)
if [[ "$WARN" == *"PCS illuminant is not D50"* ]] || [[ "$WARN" == *"known incorrect sRGB profile"* ]]; then
pngcrush -s -ow -rem allb -reduce "$f"
FIXED=$((FIXED + 1))
fi
done
echo "$FIXED errors fixed"
回答by Neil Roy
Using IrfanView image viewer in Windows, I simply resaved the PNG image and that corrected the problem.
在 Windows 中使用 IrfanView 图像查看器,我只需重新保存 PNG 图像并纠正了问题。