簡體   English   中英

為什么python中的像素值會自動變化?

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

我在 VideoCapture 的幫助下從視頻中提取幀。 在PIL的幫助下提取第一幀將幀轉換為圖像。 在位置 (1,1) 打印先前的像素值 在新創建圖像的位置 (1,1) 打印像素值 誰能解釋為什么?

提取幀的函數

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")

輸出

Old Value:   [94 95 90]
New Value:   (94, 95, 89)

答案很簡單。 您以有損格式(即 JPEG)保存數據,但它丟失了數據。

如果每一點對您都很重要,請使用像 PNG 這樣的無損格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM