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

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

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


一些应用程序将警告视为错误;如果您正在使用这样的应用程序,则必须删除该块。你可以使用任何PNG编辑器,比如ImageMagick。

在Windows CMD提示符下,在使用下面列出的命令之前,您需要将cd(改变目录)放入您想要聚焦的图像的文件夹中。

Libpng-1.6在检查ICC配置文件方面比以前的版本更加严格;您可以忽略该警告。要摆脱它,请从PNG图像中删除iCCP块。

convert in.png out.png

要从文件夹(目录)中的所有PNG文件中删除无效的iCCP块,您可以使用mogrify from ImageMagick:

mogrify *.png

这要求您的ImageMagick是用libpng16构建的。你可以通过运行:

convert -list format | grep PNG

如果您想找出哪些文件需要修复,而不是盲目地处理所有文件,您可以运行

pngcrush -n -q *.png

其中-n表示不重写文件,-q表示除警告外抑制大部分输出。对不起,在pngcrush中还没有选项来压制除警告之外的所有内容。

注意:必须安装pngcrush。


ImageMagick的二进制版本在这里


对于Android项目(Android Studio)导航到res文件夹。

例如:

C:\{your_project_folder}\app\src\main\res\drawable-hdpi\mogrify *.png

使用pngcrush从png文件中删除不正确的sRGB配置文件:

pngcrush -ow -rem allb -reduce file.png

-ow将覆盖输入文件 -rem allb将删除除tRNS和gAMA之外的所有辅助块 -reduce用于无损颜色类型或位深度缩减

在控制台输出中,您应该看到Removed the sRGB块,可能还有更多关于块移除的消息。你将得到一个更小的、优化过的PNG文件。由于该命令将覆盖原始文件,请确保创建备份或使用版本控制。


下面是一个荒谬粗暴的回答:

我修改了gradlew脚本。下面是我的新exec命令在文件的末尾

exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" **| grep -v "libpng warning:"**

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


感谢Glenn的精彩回答,我使用了ImageMagik的“mogrify *.png”功能。然而,我的子文件夹中隐藏了图像,所以我使用了这个简单的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)

为了补充Glenn的精彩回答,以下是我找到错误文件的方法:

find . -name "*.png" -type f -print0 | xargs \
       -0 pngcrush_1_8_8_w64.exe -n -q > pngError.txt 2>&1

我使用find和xargs,因为pngcrush不能处理大量的参数(由**/*.png返回)。-print0和-0用于处理包含空格的文件名。

然后在输出中搜索这些行:iCCP:不识别已编辑的已知sRGB配置文件。

./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

对于每一个,运行mogrify来修复它们。

mogrify ./Installer/Images/installer_background.png

这样做可以防止提交更改存储库中的每个png文件,而实际上只修改了几个。另外,它的优点是可以准确地显示出哪些文件有错误。

我在Windows上用Cygwin控制台和zsh shell进行了测试。再次感谢格伦,他提供了上面的大部分内容,我只是添加了一个答案,因为它通常比评论更容易找到:)


解决方案

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

使用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应用程序中有一个错误,导致输出图像为空。我刚修好,给您带来的不便深表歉意!


在尝试了本页上的几个建议后,我最终使用了pngcrush解决方案。您可以使用下面的bash脚本递归地检测和修复错误的png配置文件。只需要将完整路径传递给你想要搜索png文件的目录。

fixpng "/path/to/png/folder"

脚本:

#!/bin/bash

FILES=$(find "$1" -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"

在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.


一些背景信息:

libpng 1.6+版本中的一些更改导致它发出警告或 甚至不能与原始HP/MS sRGB配置文件正确工作,领先 到以下stderr: libpng警告:iCCP:已知错误的sRGB 旧的配置文件使用D50白点,其中D65是标准的。 这个配置文件并不少见,虽然Adobe Photoshop正在使用 默认情况下,它没有嵌入到图像中。

(来源: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.

(来源:https://forum.qt.io/topic/58638/solved-libpng-warning-iccp-known-incorrect-srgb-profile-drive-me-nuts/16)


你也可以在photoshop中修复这个…

打开你的。png文件。 文件->另存为并在打开的对话框中取消选中“ICC配置文件:sRGB IEC61966-2.1” 取消勾选“作为副本”。 勇敢地保存你原来的。png文件。 继续你的生活,知道你已经从这个世界上除掉了那么一点点邪恶。


扩展friederbluemle解决方案,下载pngcrush,然后如果你在多个png文件上运行它,就像这样使用代码

path =r"C:\\project\\project\\images" # path to all .png images
import os

png_files =[]

for dirpath, subdirs, files in os.walk(path):
    for x in files:
        if x.endswith(".png"):
            png_files.append(os.path.join(dirpath, x))

file =r'C:\\Users\\user\\Downloads\\pngcrush_1_8_9_w64.exe' #pngcrush file 


for name in png_files:
    cmd = r'{} -ow -rem allb -reduce {}'.format(file,name)
    os.system(cmd)

这里所有与项目相关的PNG文件都在一个文件夹中。


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

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

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

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


一些建议的答案使用pngcrush和-rem allb选项,文档说这就像“用电锯做手术”。该选项删除了许多块。为了防止“iCCP:已知错误的sRGB配置文件”警告,删除iCCP块就足够了,如下所示:

pngcrush -ow -rem iCCP filename.png

当我训练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

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

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

$img = @imagecreatefrompng($file);