-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_image.py
40 lines (31 loc) · 1.19 KB
/
load_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
import io
class ImageDisplay(QtWidgets.QMainWindow):
def __init__(self):
super(ImageDisplay, self).__init__()
self.image_label = QtWidgets.QLabel()
self.setCentralWidget(self.image_label)
self.init_ui()
def init_ui(self):
self.setWindowTitle("Image Display")
self.setGeometry(100, 100, 400, 400)
def open_image(self):
file_dialog = QtWidgets.QFileDialog()
file_path, _ = file_dialog.getOpenFileName(self, "Select Image")
if file_path:
with open(file_path, 'rb') as f:
img_data = f.read()
pixmap = QtGui.QPixmap()
pixmap.loadFromData(img_data)
if not pixmap.isNull():
self.image_label.setPixmap(pixmap.scaled(self.image_label.size(), QtCore.Qt.KeepAspectRatio))
else:
print("Failed to load image:", file_path)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = ImageDisplay()
window.show()
# Open the image when the application starts
window.open_image()
sys.exit(app.exec_())