Hi there, I am a beginner in GUI formation with PyQt 5. The thing is I have some raster files that I want to display in my GUI, though the code is not giving any error but still its not displaying my file as well. I am trying to display it in QGraphicsView(I don’t know if its the right way to display or not).My GUI displays multiple things like a matrix and some txt result and check buttons to switch on an off the raster files. But I don’t know how to o it an I’m stuck at first step now. Please help me out!
and the code is
import sys
import rasterio
from PyQt5 import QtWidgets, uic
from PyQt5.QtGui import QImage, QPixmap
from rasterio.plot import show
class FinalApp(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
uic.loadUi("Final.ui", self)
self.pushButton.clicked.connect(self.display_tiff)
def display_tiff(self):
file_path = 'C:\\Users\\Hp\\Downloads\\Geology_img.tif' # Provide the path to your TIFF file
try:
# Open the TIFF file
raster = rasterio.open(file_path)
# Convert raster image to QImage
img = raster.read().transpose([1, 2, 0]) # Read the raster data
q_img = QImage(img.data, img.shape[1], img.shape[0], img.shape[1] * 3, QImage.Format_RGB888)
# Convert QImage to QPixmap
pixmap = QPixmap.fromImage(q_img)
# Display the QPixmap in the QGraphicsView
scene = QtWidgets.QGraphicsScene()
scene.addPixmap(pixmap)
self.graphicsView.setScene(scene)
except Exception as e:
print(f"Error processing {file_path}: {e}")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = FinalApp()
window.show()
sys.exit(app.exec_())