python中保存图片像素值发生变化,为什么python中的像素值会自动改变?

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)

解决方案

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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值