Png pictures lack some libraries, resulting in damage, or redundant data, which will lead to the following errors:
libpng warning: iCCP: known incorrect sRGB profile
libpng warning iccp extra compressed data
Some possible solutions:
Libpng 1.6 and above enhances ICC profiles checking, so a warning is issued. Delete ICCP profiles from PNG images.
You can read it first and then save it again:
import cv2
from skimage import io
image = io.imread(path)
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGRA)
cv2.imencode('.png',image)[1].tofile(path)
solution
It is observed that the picture with error cannot be opened at all. Select to delete the corresponding picture.
File structure:
- all
- train
- folder1
- img1.png
- ....
- imgn.png
- foler2
- ...
- test
- folder1
- img1.png
- ....
- imgn.png
- foler2
- ...
Implementation functions:
Rename the folder, rename the file name (PS: originally in Chinese), and use the convert command to convert PNG to jpg
import os
import cv2
import random
dir_path = "all/train"
def rename_folder():
for idx, subdir in enumerate(os.listdir(dir_path)):
new_folder = "%s_" % (str(random.randint(0,10000))) + str(idx)
whole_subdir = os.path.join(dir_path, subdir)
whole_newdir = os.path.join(dir_path, new_folder)
# print(f"rename {whole_subdir} to {whole_newdir}")
os.rename(whole_subdir, whole_newdir)
def rename():
# rename files
for subdir in os.listdir(dir_path):
whole_path = os.path.join(dir_path, subdir)
for idx, img_name in enumerate(os.listdir(whole_path)):
postfix = "png" if img_name.endswith("png") else "jpg"
new_img_name = str(idx) + "." + postfix
org_path = os.path.join(whole_path, img_name)
new_path = os.path.join(whole_path, new_img_name)
# print(f"rename {org_path} to {new_path}")
os.rename(org_path, new_path)
def convert_png2jpg():
# convert
for subdir in os.listdir(dir_path):
whole_path = os.path.join(dir_path, subdir)
for img_name in os.listdir(whole_path):
img_whole_path = os.path.join(whole_path, img_name)
if img_name.endswith("png"):
# print(img_whole_path)
img = cv2.imread(img_whole_path)
cv2.imwrite(img_whole_path, img)
os.system(
"convert %s %s"
% (img_whole_path, img_whole_path.replace("png", "jpg"))
)
def main():
# run with nohup
# 1. rm nohup.out
# 2. nohup python convert_png.py
rename_folder()
rename()
convert_png2jpg()
if __name__ == "__main__":
main()
An error may occur at this time:
These files that cannot be converted are the objects to be deleted. The corresponding files are matched and deleted by regularization method.
import os
def main():
rm_list = greps()
rm(rm_list)
def greps():
import re
rm_list = []
file = "nohup.out"
f = open(file, "r")
for line in f.readlines():
res = re.findall("`.+'", line)
if res:
rm_list.append(*res)
return rm_list
def rm(rm_list):
rm_list = [item.lstrip("`").rstrip("'") for item in rm_list]
for item in rm_list:
print("rm: ", item)
os.system("rm %s" % item)
if __name__ == "__main__":
main()