为什么python中的像素值会自动改变? [英] Why the automatic change in pixel value in python?

查看:250
本文介绍了为什么python中的像素值会自动改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在借助VideoCapture从视频中提取帧. 提取第一帧,借助PIL将帧转换为图像. 在位置(1,1)打印前一个像素值 在新创建的图像的位置(1,1)打印像素值 谁能解释为什么?

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?

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)保存了数据,并且丢失了数据.

The answer is very simple. You saved your data in a lossy format, namely JPEG, and it lost data.

如果每一点对您都很重要,请使用PNG等无损格式.

Use a lossless format like PNG if every bit is important to you.

这篇关于为什么python中的像素值会自动改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆