【发布时间】:2016-06-16 12:52:09
【问题描述】:
我尝试在使用 Python.NET 的 Python 应用程序中使用 .NET 程序集。 C# 代码捕获我想与 python 一起使用的图像。假设我有以下 C# 方法:
public static byte[] Return_Image_As_Byte_Array()
{
Image image = Image.FromFile("C:\path\to\an\image");
ImageConverter imageConverter = new ImageConverter();
byte[] ByteArray = (byte[])imageConverter.ConvertTo(image, typeof(byte[]));
return ByteArray;
}
当我在 python 中使用 Python.Net 时,我会执行以下操作:
import clr
clr.AddReference('MyAssembly')
from MyAssembly import MyClass
print(MyClass.Return_Image_As_Byte())
这给了我输出:
<System.Byte[] at 0xb7ba20c080>
有没有办法将此图像从 C# 转换为原生 python 类型,如 numpy 数组?
【问题讨论】:
-
这看起来在这里解决了:github.com/pythonnet/pythonnet/issues/174
-
你可以直接用
list(System.Byte[])包装 -
@denfromufa 这将有一个非常糟糕的表现
-
@dlammy 如果您需要性能,请看这里:github.com/pythonnet/pythonnet/issues/514
-
@denfromufa 是的,事实上我使用了 robbmcleod 解决方案,而且速度非常快。谢谢
标签: python .net arrays numpy python.net