pyqt5指定路径打开图片的代码
时间: 2024-05-09 18:17:45 浏览: 79
打开图片的代码
Sure, here is the code for opening an image with a specified path using PyQt5:
```
from PyQt5.QtGui import QPixmap, QPainter
from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene, QGraphicsView, QGraphicsPixmapItem, QVBoxLayout, QPushButton
import os
class App(QMainWindow):
def __init__(self, path):
super().__init__()
self.setWindowTitle("Image Viewer")
# Create graphics scene
self.scene = QGraphicsScene(self)
self.view = QGraphicsView(self.scene)
self.view.setRenderHint(QPainter.Antialiasing)
self.view.setRenderHint(QPainter.SmoothPixmapTransform)
# Load image from path and add to scene
if os.path.isfile(path):
pixmap = QPixmap(path)
item = QGraphicsPixmapItem(pixmap)
self.scene.addItem(item)
self.view.fitInView(item, 1)
# Add view to layout
layout = QVBoxLayout()
layout.addWidget(self.view)
# Add layout to main window
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
# Specify image path here
image_path = "<path_to_image>"
# Create and show application
ex = App(image_path)
ex.show()
sys.exit(app.exec_())
```
Hope this helps!
阅读全文