1 year ago

#387140

test-img

abenfraj

How can I display a very large BMP file on PyQT5?

I am looking for a way to display a 46000x10000 BMP file in PyQT5, I have been looking for a way to do this for quite long, I tried using QPixmap, QImage, QGraphicsView etc... The goal for me first is to find a way to display it without losing pixels and being able to zoom on it to take samples.

This bit of code works for for small BMP files but when I try on the 46000x10000 one it just doesn't do anything (no errors either).

# To prevent the following error: "Image size (460000000 pixels) exceeds limit of 178956970 pixels, could be decompression bomb DOS attack"
Image.MAX_IMAGE_PIXELS = None


class UI(QMainWindow):
    # Initialize the main window's UI components
    def __init__(self):
        super(UI, self).__init__()

        # Initialize the UI component variables
        self.pixmap = None
        self.select_bitmap_button = None
        self.quit_button = None
        self.bitmap_label = None

        uic.loadUi(str(pathlib.Path(__file__).parent.resolve()) + "\\main.ui", self)  # Load the UI file
        self.retranslateUi()  # Translate the UI components into Python Objects
        self.connectAllWidgets()  # Connect all the UI components to their respective functions

        self.show()  # Show the main window

    # Function to translate the main window's UI components into Python Objects
    # @param self The object pointer
    def retranslateUi(self):
        self.quit_button = self.findChild(QPushButton, "quit_button")
        self.select_bitmap_button = self.findChild(QPushButton, "select_bitmap_button")
        self.bitmap_label = self.findChild(QLabel, "bitmap_label")

    # Function to connect all the UI components to their respective functions
    # @param self The object pointer
    def connectAllWidgets(self):
        self.select_bitmap_button.clicked.connect(
            self.openFileNameDialog)  # Connect the select bitmap button to the openFileNameDialog function
        self.quit_button.clicked.connect(self.close)  # Connect the quit button to the close function from QMainWindow

    # Select BMP file from file explorer and directly display it
    # @param self The object pointer
    def openFileNameDialog(self):
        fileName = QFileDialog.getOpenFileName(self,
                                               "Open File",
                                               "",
                                               "BMP files (*.bmp);;PNG files (*.png);;All Files (*)",
                                               )  # Get the file name from the file explorer
        self.pixmap = QPixmap(fileName[0])  # Create a QPixmap object from the selected file
        self.bitmap_label.setPixmap(self.pixmap)  # Set the bitmap label's pixmap to the QPixmap object
        self.bitmap_label.adjustSize()  # Adjust the bitmap label's size to fit the QPixmap object


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)  # Create the application
    ui = UI()  # Create the main window object
    sys.exit(app.exec_())  # Execute the application

# End of main

python

image

pyqt5

bmp

0 Answers

Your Answer

Accepted video resources