为什么PIL的Image.fromarray()会使我的图像颜色失真? [英] Why is PIL's Image.fromarray() distorting my image color?

查看:1112
本文介绍了为什么PIL的Image.fromarray()会使我的图像颜色失真?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码为mp4视频生成缩略图:

I am generating thumbnails for mp4 videos using the following code:

import cv2 as cv
from PIL import Image 

vidcap = cv.VideoCapture(videoPath)
vidcap.set(cv.CAP_PROP_POS_MSEC, millisecond)

#Turn video frame into numpy ndarray
success, image = vidcap.read()
cv.imwrite('fromImage.jpg', image)   #line to be replaced

从高预算,专业拍摄的视频生成的缩略图如下所示:

不幸的是,在我的应用程序上下文中,我将无法直接将图像框架写入文件。相反,我必须将cv生成的图像数组转换为PIL图像,然后从那里开始。看起来像这样:

The thumbnail generated from a high budget, professionally shot video looks like this: Unfortunately in my application context, I will not be able to write the image frame directly to a file. Instead I must convert the image array generated by cv into a PIL image and then go from there. It looks something like this:

# Turn numpy ndarray int PIL image
img = Image.fromarray(image)
img.save('fromArray.jpg')    #Saving it for stackoverflow

但输出来自同一mp4视频的缩略图完全失真,因为它似乎已交换红色和蓝色,看起来像这样:
谁是罪魁祸首?图像失真吗?

But the outputted thumbnail from the same mp4 video is completely distorted as it seems to have swapped red and blue and looks like this: Who or what is the culprit in this image distortion?

推荐答案

> https://note.nkmk.me/en/python-opencv-bgr-rgb-cvtcolor/

imageRGB = cv.cvtColor(image, cv.COLOR_BGR2RGB)

img = Image.fromarray(imageRGB)

img.save('fromArray.jpg')

这篇关于为什么PIL的Image.fromarray()会使我的图像颜色失真?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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