Exemplo n.º 1
0
    def processThreshold(self):
        # Clearing right canvas before processing image
        self.canvasRight.delete("all")
        processValue = self.thresholdEntry.get()

        # Testing if entry in threshold entry box is digit
        try:
            if processValue.isdigit():
                if 0 <= int(processValue) <= 255:
                    self.filePath.config(text=self.file_chosen)
                    # Testing image type
                    if self.typeOfImage == "Greyscale Image":
                        # Binarising image and giving output for display to binaryOutput variable.
                        self.binaryOutput = GreyScaleImage().binariseImage(
                            self._imagedata, self.thresholdEntry.get())
                        # Passing binaryOutput variable to BinaryImage class to determine each pixel colour
                        self._processedData = BinaryImage(
                        ).determinePixelValue(self.binaryOutput)
                        # Passing binaryImageDisplay variable to display function in order to display on right canvas
                        self._display(self.canvasRight, self._processedData)
                    elif self.typeOfImage == "Colour Image":
                        self.binaryOutput = ColourImage().binariseImage(
                            self._imagedata, self.thresholdEntry.get())
                        self._processedData = BinaryImage(
                        ).determinePixelValue(self.binaryOutput)
                        self._display(self.canvasRight, self._processedData)
                else:
                    self.filePath.config(
                        text="Please input digits between 0 and 255.")
            else:
                # Returning error message to user on label if entry box does not contain only digits
                self.filePath.config(text="Please input digits.")
        except AttributeError:
            self.filePath.config(text="Please open image before processing.")
Exemplo n.º 2
0
    def _openFile(self):
        try:
            values = []
            filename = askopenfilename(initialdir="/",
                                       title="Select file",
                                       filetypes=(("Text files", "*.txt"),
                                                  ("All files", "*.*")))
            self.file_label.config(text=filename)

            # Open appropriate text file as object for reading and transform into Python-ready list
            with open(filename, "r") as inFile:
                linesList = inFile.readlines()  # a container of lines

                # Create a list containing just the values from the file container
                for eachLine in linesList[1:]:
                    itemsLine = eachLine.split(
                        ",")  # separates each item in a line
                    for each_item in itemsLine:
                        values.append(
                            int(each_item)
                        )  # file items need to be converted to integer

                # Ensure the values, passed through subclass, become an image data object
                if linesList[0].strip() == "Greyscale Image":
                    self._imagedata = GreyScaleImage(values)
                elif linesList[0].strip() == "Colour Image":
                    self._imagedata = ColourImage(values)

            # Call dataForDisplay and getThreshold methods on imagedata object
            # Display object and suggested transformation value
            self._display(self.canvasLeft, self._imagedata.dataForDisplay())
            self.threshValue.set(self._imagedata.getThreshold())
        except ValueError:
            showerror(
                "Invalid input",
                "Please load comma delimited document of the correct file type."
            )
        except:
            showerror("Ooops", "Something wasn't right! Please bear with us.")
Exemplo n.º 3
0
    def openFile(self):
        # Open the file dialog to select an image file
        self.file_chosen = askopenfilename()
        self._imagedata = []
        self.typeOfImage = ""
        self.meanIntensity = []

        # Handling output when 'Cancel' is selected in open file dialog
        if not self.file_chosen:
            self.filePath.config(text="Please select a file")
            self.canvasLeft.delete("all")
            self.canvasRight.delete("all")
            self.thresholdEntry.delete(0, 'end')
            self.thresholdEntry.insert(0, "0")

        # Statement to run once open file dialog has finished
        if self.file_chosen[-4:] == ".txt":
            try:
                with open(self.file_chosen) as input_file:
                    fline = input_file.readline()
                    self.typeOfImage = fline.strip()
                    for line in input_file:
                        self._imagedata.append(line.split())
                input_file.close()
            except Exception:
                self.filePath.config(
                    text=
                    "Unknown file type was selected. Please select txt image.")

        try:
            if self.typeOfImage == "Greyscale Image":
                # Updating label to display file location
                self.filePath.config(text=self.file_chosen)
                # Testing if first line is either Greyscale or colour image
                # Clearing canvas before displaying image
                self.canvasLeft.delete("all")
                self.canvasRight.delete("all")
                # Accessing and passing file location to _openGreyScaleImage
                self.vals = GreyScaleImage().dataForDisplay(self._imagedata)
                # passing vals to display image on left canvas
                self._display(self.canvasLeft, self.vals)

                # Extracting intensity values from each sublist and adding to threshold list
                for i in self._imagedata:
                    for index in range(2, len(i), 3):
                        self.meanIntensity.append(i[index])

                # Converting threshold list elements to ints
                self.meanIntensity = list(map(int, self.meanIntensity))
                # Getting average threshold across image and adding value to greyThreshold variable
                self.greyThreshold = GreyScaleImage().getThreshold(
                    self.meanIntensity)
                # Clearing the threshold entry box
                self.thresholdEntry.delete(0, 'end')
                # Inserting the system selected threshold into entry box
                self.thresholdEntry.insert(0, str(self.greyThreshold))
            elif self.typeOfImage == "Colour Image":
                self.filePath.config(text=self.file_chosen)
                # Clearing both left and right canvas before displaying image
                self.canvasLeft.delete("all")
                self.canvasRight.delete("all")
                self.vals = ColourImage().dataForDisplay(self._imagedata)
                self._display(self.canvasLeft, self.vals)
                # Stripping commas from sublist
                self._imagedata = [[x.strip(",") for x in group]
                                   for group in self._imagedata]
                # Converting all elements in sublist to ints
                self.meanIntensity = [[int(x) for x in group[2:]]
                                      for group in self._imagedata]
                # Getting mean of each sublist
                self.meanIntensity = [
                    int(sum(x) // len(x)) for x in self.meanIntensity
                ]
                self.colourThreshold = ColourImage().getThreshold(
                    self.meanIntensity)
                # Clearing the threshold entry box
                self.thresholdEntry.delete(0, 'end')
                # Inserting the system selected threshold into entry box
                self.thresholdEntry.insert(0, str(self.colourThreshold))
            else:
                self.filePath.config(
                    text=
                    "Unknown file type was selected. Please select txt image.")
        except ValueError:
            self.filePath.config(text="Unable to read data in file.")
        except Exception:
            self.filePath.config(
                text="Unknown file type was selected. Please select txt image."
            )