Visual Studio 2017 Python C脚本#

2024-06-01 08:23:10 发布

您现在位置: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);

         }

这是我的图片.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:'没有名为cv2的模块'


Tags: 文件pathimagenewvarresultfilenamecv2