-2

I am making some kind of basic image filter app. I have a function to open and initialize image but variables stay just in function and I can't get them from another function, so I need to define variables global?

I tried to define variables globally and initialize them with example image, then in function I assigned new data to that variables (or not?) But it seems function that opens the file does not rewrite global variable, so my filter functions applies to my example image, not target image I opened.

image = Image.open("test.jpg")
draw = ImageDraw.Draw(image)  
width = image.size[0]  
height = image.size[1]      
pix = image.load()

class ExampleApp(QtWidgets.QMainWindow, design.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.load_file.triggered.connect(self.load_image) #Can I here call load_image with arguments? How?
        self.grayscale.triggered.connect(self.Grayscale)
    def browse_file(self):
        file_name = QtWidgets.QFileDialog.getOpenFileName(self, 'Pick a picture',"","JPEG (*.jpg;*.jpeg);;PNG (*.png);;All Files (*)")[0]
        if file_name:
            print (file_name)
            return file_name
        else:
            print("File couldn't be open")
            return 0
    def load_image(self): 
        file_name = self.browse_file()
        pixmap = QPixmap(file_name)
        self.pic_box.setPixmap(pixmap)
        self.pic_box.resize(pixmap.width(), pixmap.height())
        print(pixmap.width(), pixmap.height())
        self.resize(pixmap.width(), pixmap.height())
        image = Image.open(file_name) #Here I'm trying assign new image and it's properties to variables I defined on the first lines
        draw = ImageDraw.Draw(image) 
        width = image.size[0]  
        height = image.size[1]      
        pix = image.load()
        self.show()
    def Grayscale(self): #Function works with test.jpg, not with file I'm trying to load
        for i in range(width):
            for j in range(height):
                a = pix[i, j][0]
                b = pix[i, j][1]
                c = pix[i, j][2]
                S = (a + b + c) // 3
                draw.point((i, j), (S, S, S))
        image.save("Grayscale.jpg", "JPEG")

My goal is to somehow pass string with file name to the global variable, so every function could access it. There is other design.py file, I made from .ui file of QtDesigner but I think problem does not depends on it

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
AWRDev.
  • 45
  • 8

2 Answers2

3

If you really want to use a global variable, than can't you just do

filename = "test.jpg"
image = Image.Open(filename)
...

at the top?

Alex F
  • 239
  • 1
  • 7
  • I am just second day in Python) Maybe I didn't get you but your advice has no help for my situation. How can I from the file opening function rewrite globals? Is it possible? – AWRDev. Jun 01 '19 at 18:32
2

To override a global variable from a function, you need to have a line above it explicitly stating you are trying to change the global variable, not create local one. In your function change:

image = Image.open(file_name)

to:

global image
image = Image.open(file_name)

Python function global variables?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241