我试图使用SDL加载PNG图像,但程序不工作,这个错误出现在控制台中

libpng警告:iCCP:已知错误的sRGB配置文件

为什么会出现这个警告?我该怎么解决这个问题呢?


当前回答

PHP开发者使用imagecreatefromng函数时遇到的问题

您可以尝试使用@取消警告

$img = @imagecreatefrompng($file);

其他回答

解决方案

不正确的配置文件可以通过以下方法修复:

使用QPixmap::load打开带有错误配置文件的图像 使用QPixmap::save将图像保存回磁盘(已经具有正确的配置文件)

注意:此解决方案使用Qt库。

例子

下面是我用c++写的一个最小示例,以演示如何实现建议的解决方案:

QPixmap pixmap;
pixmap.load("badProfileImage.png");

QFile file("goodProfileImage.png");
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "PNG");

基于此示例的GUI应用程序的完整源代码可在GitHub上获得。

2019年12月5日更新:答案过去是有效的,现在仍然有效,但是我在GitHub上分享的GUI应用程序中有一个错误,导致输出图像为空。我刚修好,给您带来的不便深表歉意!

我在项目的根目录中运行了这两个命令,它已经修复了。

基本上是将“find”命令的输出重定向到一个文本文件,用作要处理的文件列表。然后你可以使用“@”标志将文本文件读入“mogrify”:

找到*.png -mtime -1 > list.txt Mogrify -resize 50% @list.txt

这将使用“find”来获取所有更新超过1天的*.png图像,并将它们打印到名为“list.txt”的文件中。然后“mogrify”读取该列表,处理图像,并用调整大小的版本覆盖原始图像。在不同的系统中,“find”的行为可能会有微小的差异,因此您必须检查手册页以了解确切的用法。

在Windows中使用IrfanView图像查看器,我简单地重新保存了PNG图像,这纠正了问题。

在Mac OS和Homebrew上有一个更简单的方法来修复这个问题:

如果还没有安装,请安装自制软件

$brew install libpng
$pngfix --strip=color --out=file2.png file.png

或者对当前目录中的每个文件都这样做:

mkdir tmp; for f in ./*.png; do pngfix --strip=color --out=tmp/"$f" "$f"; done

它将为当前目录中的每个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.

当我训练yolo时,warninglibpng警告:iCCP:已知不正确的sRGB配置文件发生每个纪元。然后我使用bash找到png,然后使用python3和opencv(cv2)重写png文件。因此,警告只在重写时出现。步骤如下:

步骤1。创建一个python文件:

# rewrite.py
import cv2, sys, os
fpath = sys.argv[1]
if os.path.exists(fpath):
    cv2.imwrite(fpath, cv2.imread(fpath))

步骤2。在bash中运行:

# cd your image dir
# then find and rewrite png file
find . -iname "*.png" | xargs python3 rewrite.py