简体   繁体   English

如何将 PIL 图像转换为 cv2/numpy 数组

[英]How can i convert a PIL image to cv2/numpy array

Im trying to convert a pil image to a numpy array but i want the image to keep it's transperancy, how can i do that?我试图将 pil 图像转换为 numpy 数组,但我希望图像保持其透明性,我该怎么做?

Iv'e tried using numpy.array() But it doesnt keep the transperancy我试过使用 numpy.array() 但它没有保持透明性

I'm not quite sure what you mean by it doesn't keep its transparency.我不太确定你所说的它不保持透明度是什么意思。 If you convert a PIL image with transparency using numpy.array() it should return a numpy array with shape width, height, 4 where the 4th channel will represent the alpha channel values.如果您使用numpy.array()转换具有透明度的 PIL 图像,它应该返回一个形状width, height, 4的 numpy 数组width, height, 4其中第 4 个通道将表示 alpha 通道值。 And after whatever processing you need to do, if you convert it back to a PIL image using Image.fromarray() and perhaps saving with Image.save() you should get back an image with the same transparency.而经过任何处理,你需要做的,如果你使用它转换回PIL图像Image.fromarray()也许是拯救Image.save()你应当得到的图像具有相同的透明度。 Can't help you much more without seeing an actual snippet of the code and possibly the image.如果没有看到实际的代码片段和可能的图像,就无法为您提供更多帮助。

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

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