简体   繁体   English

为什么python中的像素值会自动变化?

[英]Why the automatic change in pixel value in python?

I am extracting frames from video with the help of VideoCapture.我在 VideoCapture 的帮助下从视频中提取帧。 Extracted the first frame converted the frame into an image with the help of PIL.在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?在位置 (1,1) 打印先前的像素值 在新创建图像的位置 (1,1) 打印像素值 谁能解释为什么?

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)

The answer is very simple.答案很简单。 You saved your data in a lossy format, namely JPEG, and it lost data.您以有损格式(即 JPEG)保存数据,但它丢失了数据。

Use a lossless format like PNG if every bit is important to you.如果每一点对您都很重要,请使用像 PNG 这样的无损格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM