用plt.savefig或者cv.imwrite保存图片时,图片的尺寸发生变化:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
# 读取文件
cap = cv.VideoCapture("cut.mp4")
# 获取宽高
w = int(cap.get(3))
h = int(cap.get(4))
fps = int(cap.get(5))
all_pic = int(cap.get(7))
print(w, h, all_pic)
# 是否读取成功
while (cap.isOpened()):
ret, frame = cap.read()
if ret:
plt.figure(figsize=(8,4),dpi=100)
frame = cv.cvtColor(frame,cv.COLOR_BGR2RGB)
plt.axis('off')
plt.savefig('mix2.jpg',bbox_inches='tight',pad_inches = 0)
print(frame.shape)
plt.show()
break
else:
break
pass
cap.release()
cv.destroyAllWindows()
查看图片:大小不符
解决:
plt.figure(figsize=(8,4),dpi=100)
#加上这两句
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
frame = cv.cvtColor(frame,cv.COLOR_BGR2RGB)
...
其实如果plt.savefig(‘xx.jpg’)不设置其他参数也可以保持图片相同大小,但是这样保存的图片会有白边。