我试图使用SDL加载PNG图像,但程序不工作,这个错误出现在控制台中
libpng警告:iCCP:已知错误的sRGB配置文件
为什么会出现这个警告?我该怎么解决这个问题呢?
我试图使用SDL加载PNG图像,但程序不工作,这个错误出现在控制台中
libpng警告:iCCP:已知错误的sRGB配置文件
为什么会出现这个警告?我该怎么解决这个问题呢?
当前回答
感谢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)
其他回答
在尝试了本页上的几个建议后,我最终使用了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"
感谢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)
当我训练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
我在项目的根目录中运行了这两个命令,它已经修复了。
基本上是将“find”命令的输出重定向到一个文本文件,用作要处理的文件列表。然后你可以使用“@”标志将文本文件读入“mogrify”:
找到*.png -mtime -1 > list.txt Mogrify -resize 50% @list.txt
这将使用“find”来获取所有更新超过1天的*.png图像,并将它们打印到名为“list.txt”的文件中。然后“mogrify”读取该列表,处理图像,并用调整大小的版本覆盖原始图像。在不同的系统中,“find”的行为可能会有微小的差异,因此您必须检查手册页以了解确切的用法。
一些应用程序将警告视为错误;如果您正在使用这样的应用程序,则必须删除该块。你可以使用任何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