首页 > 解决方案 > C# 中的 Visual Studio 2017 Python 脚本

问题描述

这是我的主文件

        String filePath, path;
        OpenFileDialog ofd = new OpenFileDialog();
        Nullable<bool> result = ofd.ShowDialog();
        if (result == true)
        {
            filePath = ofd.FileName;
            var sections = filePath.Split('\\');
            var fileNam = sections[sections.Length - 1];
            path=Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
            string destFile = System.IO.Path.Combine(path, fileNam);
            System.IO.File.Copy(filePath, destFile, true);
            var engine = Python.CreateEngine(); 
            var scope = engine.CreateScope(); 
            ScriptSource source = engine.CreateScriptSourceFromFile(path + "\\Image.py");
            source.Execute(scope);
             Object myclass = engine.Operations.Invoke(scope.GetVariable("pythonScriptClass"));
             object[] parameters = new object[] { fileNam };
             engine.Operations.InvokeMember(myclass, "Image", parameters);

         }

这是我的 Image.py 文件

import cv2  
import numpy  
class pythonScriptClass:   
    def Image(filename):  
       img=cv2.imread(filename)  
       gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)   
       cv2.imwrite("result.png",gray)   
Image(filename)

我收到错误“IronPython.Runtime.Exceptions.ImportException:'No module named cv2'”

标签: c#pythonironpython

解决方案


推荐阅读