aPe*_*ere 6 .net python arrays numpy python.net
我尝试在使用 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;
}
Run Code Online (Sandbox Code Playgroud)
当我在 python 中使用 Python.Net 时,我执行以下操作:
import clr
clr.AddReference('MyAssembly')
from MyAssembly import MyClass
print(MyClass.Return_Image_As_Byte())
Run Code Online (Sandbox Code Playgroud)
这给了我输出:
<System.Byte[] at 0xb7ba20c080>
Run Code Online (Sandbox Code Playgroud)
有没有办法将此图像从 C# 转换为本机 python 类型(如 numpy 数组)?
如果您正在寻找性能:
我发现np.fromiter()(numpy) 比上面 @dlammy url 链接中提供的答案快 6 倍,作为对问题的答复。使用的函数是asNumpyArray()
url: https: //github.com/pythonnet/pythonnet/issues/514
由于时间上存在这样的差异,我认为只需要一次定时运行就足够了,而不是对结果进行平均。我的结果::
np.fromiter()67.914736s
asNumpyArray():334.5198s
解决方案在这里找到:/sf/answers/4275409001/