The latest, python, opencv read in and output jpg, bmp, png, pny format file speed test analysis

1) Directly upload the code

import numpy as np,cv2,time,os,pdb

inp="dataset/1.jpg"
t1=time.time()
img1 = cv2.imread(inp, flags=cv2.IMREAD_COLOR)
t2=time.time()
cv2.imwrite("out1/1.jpg",img1)
t3=time.time()
ss=os.path.getsize(inp)/1024/1024
print('jpg read & save time=',t2-t1,t3-t2,'size(M)=',ss)

cv2.imwrite("dataset/1.bmp",img1)
cv2.imwrite("dataset/1.png",img1)
np.save("dataset/1.npy",img1)


inp="dataset/1.bmp"
t1=time.time()
img2 = cv2.imread(inp, flags=cv2.IMREAD_COLOR)
t2=time.time()
cv2.imwrite("out1/1.bmp",img1)
t3=time.time()
ss=os.path.getsize(inp)/1024/1024
print('bmp read & save time=',t2-t1,t3-t2,'size(M)=',ss)

inp="dataset/1.png"
t1=time.time()
img3 = cv2.imread(inp, flags=cv2.IMREAD_COLOR)
t2=time.time()
cv2.imwrite("out1/1.png",img1)
t3=time.time()
ss=os.path.getsize(inp)/1024/1024
print('png read & save time=',t2-t1,t3-t2,'size(M)=',ss)


inp="dataset/1.npy"
t1=time.time()
img4 = np.load(inp)
t2=time.time()
np.save("out1/1.npy",img1)
t3=time.time()
ss=os.path.getsize(inp)/1024/1024
print('npy read & save time=',t2-t1,t3-t2,'size(M)=',ss)

# pdb.set_trace()
# 正确性验证,没有问题,只是很慢
# for i in range(4000):
#     for j in range(3000):
#         for k in range(3):
#             if abs(img1[i,j,k]-img4[i,j,k])>0:
#                 print(i,j,k,img1[i,j,k]-img4[i,j,k])

The content is not explained.

1) Test platform

Huawei's Matebook, this book sucks. Execute in conda. 

2)

Test Results

jpg read & save time= 0.3670051097869873 0.5111029148101807 size(M)= 5.2205610275268555
bmp read & save time= 0.061330318450927734 0.024112701416015625 size(M)= 34.332326889038086
png read & save time= 0.4295041561126709 0.9660918712615967 size(M)= 15.812897682189941
npy read & save time= 0.020075082778930664 0.05786633491516113 size(M)= 34.3323974609375

3) Result analysis and conclusion

It can be seen that npy is a better choice for reading data, and saving data in bmp files is faster, but both have large amounts of data.

The size of jpg is 5.22M, while the size of npy and bmp is 34M, and Png is double large and slow. This may be the reason for the underlying library. When the hard disk is limited, jpg is a better choice in terms of size. The hard disk is sufficient, and both npy and bmp are good in terms of speed. But in terms of compatibility and visualization, bmp is at least recognized by Windows computers, and npy may not be necessarily. It is also an option to do a conversion or a partial conversion.

Guess you like

Origin blog.csdn.net/anlongstar/article/details/130755884