Pyqt5 and openCv question

I took a py program to crop an image to smaller images defined by user by using mouseclick events using cv2. Then I created a GUI using Pyqt5 to take the input image and the output folder path where my smaller images will be saved but I am unable to merge both of these. I took the image cropper program from this website

while I created a simple GUI using pyqt5.
could you please help me as I am unable to merge them.

Howdy Hafsa,

what exactly do you want to ‘merge’? The filename and the filepath?

If so, you might want to have a look at os.path. join and some other os.path methods…:

Cheers, Dominik

CV2 is probably overkill for image cropping. Check out Pillow

1 Like

Hi hafsa ahmed,

You asked:

“I am taking the input directory path from the user in the GUI and i
want to pass these paths to another py file. Can you tell how can I pass
the path names from one py file to another ?”

That depends on how you are using the second py file. If you are using
it as a module, then you do this:

import second  # load as a module
pathname = "path/to/file"  # get this from the user
second.function(pathname)  # call the function with the path

That’s the way .py files are intended to be used.

The other way is to use the second .py file as a script.

# Don't do this! This is wrong and bad.
# (for experts only: this is **almost** always wrong and bad)
import os
pathname = "path/to/file"  # get this from the user
commandline = 'python3 /path/to/ "%s"' % pathname

This is wrong because it is slow, complicated and easy to make errors.
It is bad because unless you are really, really careful, it is
vulnerable to code injection attacks.

(If this program is only for you, and will never be shared with anyone
else, you might not care about code injection attacks. But as soon as
you allow anyone else to use it, you should care.)

Apart from os.system, there are safer, but more complicated and less
convenient, ways to call external programs and scripts but I don’t
remember what they are. The documentation for os.system should link to
those alternative ways.

This method also requires the module to be written as a script
that reads input from the command line. So it must have a section that
looks something like this:

if __name__ == '__main__':
    import sys
    args = sys.argv[1:]
    pathname = args[0]

otherwise it won’t work. There are lots of ways for a Python file to be
written to take arguments from the command line when called as a script,
so it might not look exactly like that.

Calling a Python module as a script is complicated and slow, the right
way to solve this problem is to import the module and call the function
you want directly.

Hi Hafsa,

The recommended way strongly depends on how your python files / modules are entangled. Steven already has told you, how information normally is passed between files/modules.

Sometimes, if you need some informations in several files/modules, it can happen, that this does not work due to the import order your files/modules-arrangement needs; or you REALLY want/need some sort of globals (this is just recommended for few exceptions as globals mostly are deemed to be bad programming style), then you could do something alike the following:
# -- coding: utf-8 --

class Globals(object):

      value1 = 111

      value2 = 222
# -- coding: utf-8 --

from common  import Globals

from fileB   import askUser

if __name__ == "__main__":

   Globals.value1 = 333


   print (Globals.value1)

   print (Globals.value2)
# -- coding: utf-8 --

from common import Globals

def askUser():

    global Globals

    Globals.value2 = 444

The outcome when executing is:


Cheers, Dominik

PS: note that Globals.value1 is a CLASS attribute here…