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

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

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


当前回答

在尝试了本页上的几个建议后,我最终使用了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"

其他回答

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

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

pngcrush -ow -rem iCCP filename.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

扩展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文件都在一个文件夹中。