问题
I am extracting frames from video with the help of VideoCapture. Extracted the first frame converted the frame into an image with the help of PIL. Printed the previous pixel value at position (1,1) Printed the pixel value at position(1,1) of the newly created image Can anyone explain why?
Function to extract frames
import cv2
from PIL import Image
def FrameCapture(path):
# Path to video file
vidObj = cv2.VideoCapture(path)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
width = cv2.CAP_PROP_FRAME_WIDTH
height = cv2.CAP_PROP_FRAME_HEIGHT
fps = cv2.CAP_PROP_FPS
out = cv2.VideoWriter("D:\Funny 3 second video.mp4", fourcc, fps, (width, height))
cnt = int(0)
while 1:
# vidObj object calls read
# function extract frames
success, arrayframe = vidObj.read()
if success == 0:
break
if cnt == 0:
#IF FIRST FRAME SAVE IT
sp = Image.fromarray(arrayframe)
sp.save("D:\sp2.jpg")
fp = "D:\sp2.jpg"
im = Image.open(fp, mode='r')
im = im.convert('RGB')
print("Old Value: ",arrayframe[1][1])
print("New Value: ",im.getpixel((1, 1)))
out.write(arrayframe)
cnt += 1
vidObj.release()
out.release()
cv2.destroyAllWindows()
# Driver Code
if __name__ == '__main__':
# Calling the function
FrameCapture("D:\Funny 2 second video.mp4")
Output
Old Value: [94 95 90]
New Value: (94, 95, 89)
回答1:
The answer is very simple. You saved your data in a lossy format, namely JPEG, and it lost data.
Use a lossless format like PNG if every bit is important to you.
来源:https://stackoverflow.com/questions/52640023/why-the-automatic-change-in-pixel-value-in-python