1
from PIL import Image
def porcentaje(path):

    im = Image.open(path, "r")
    im.show()
    width, height = im.size
    type = im.mode
    format = im.format
    pixels=im.getdata()
    n = len(pixels)
   im=im.load()
   nblack = 0
   noblack=0

   for pixel in pixels:
       if pixel < 50:
           nblack += 1
       else:
           noblack+=1

porcentajefinal=(nblack*100)/n
print(porcentajefinal)
return(porcentajefinal)

(porcentaje(path))

mainwindow.cpp (QT Creator)

void MainWindow::on_pushButton_2_clicked()
{

    QString path = QFileDialog::getOpenFileName(this,
            tr("All Files (*)"));
     qDebug()<<path;

     QDir dir1("D:/QTCProjects/prueba");
     QFile file1("D:/QTCProjects/prueba/2_1.py");
     QString script1 = "D:/QTCProjects/prueba/2_1.py";
     QFile file2(script1);

     qDebug() << dir1.exists() << file1.exists() << file2.exists();
       //  these all result in true, true true
     QProcess *myProcess = new QProcess();
     myProcess->start("python.exe D:/QTCProjects/prueba/2_1.py" );
     myProcess->waitForFinished(-1);

      qDebug() << "myProcess:" << myProcess->readAll(); }

This python requires a variable called path to run and i get this variable in qtcreator with Qstring path, how i can give python this variable with Qprocess.

1
  • How do you pass the parameters to your script when you call it through cmd? It would help us if you show the part of how you read the parameters in your script. Commented Nov 14, 2017 at 2:58

1 Answer 1

2

If you want to pass arguments to a script through the terminal, the following structure is used:

python.exe /path/of/your_script.py arg1 arg2 ... argn

Then to obtain the parameter we must use sys.argv which is a list that stores the following:

['/path/of/your_script.py', 'arg1', 'arg2', ..., 'argn']

So in your case you should get the parameter through sys.argv[1]:

2_1.py

from PIL import Image
import sys

def porcentaje(path):

    im = Image.open(path, "r")
    im.show()
    width, height = im.size
    type = im.mode
    format = im.format
    pixels=im.getdata()
    n = len(pixels)
    im=im.load()
    nblack = 0
    noblack=0
    for pixel in pixels:
        if pixel < 50:
            nblack += 1
        else:
            noblack+=1

    porcentajefinal=(nblack*100.0)/n
    print(porcentajefinal)
    return(porcentajefinal)

path = sys.argv[1]
porcentaje(path)

On the side of Qt, python.exe is the main program, and the other parameters are the arguments, so your code must have the following structure:

void MainWindow::on_pushButton_2_clicked()
{
    QString path = QFileDialog::getOpenFileName(this, "All Files (*)");
    qDebug()<<path;
    if(!path.isEmpty()){
        QDir dir("D:/QTCProjects/prueba");
        QFileInfo info(dir, "2_1.py");
        qDebug() << dir.exists() << info.exists();
        QProcess process;
        process.start("python.exe", QStringList()<< info.absoluteFilePath() << path);
        process.waitForFinished(-1);
        qDebug() << "myProcess:" << process.readAll();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.