In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to make a picture viewer for Python+PyQt5. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Mode of realization
A basic photo viewer should have the following functions:
Load Ima
Zoom Ima
Allow you to drag and drop an image when the window size is smaller than the image
Loading images can be solved by using QGraphicsPixmapItem, zooming images using QGraphicsView's scale (sx, sy), and moving images simply by setting the dragMode of QGraphicsView to QGraphicsView.ScrollHandDrag. Because you often use the mouse wheel to zoom the image, you also need to rewrite the wheelEvent that overrides the following QGraphicsView.
In fact, due to the zoom of the window, the size of the viewport changes, and there are some details to deal with. The specific code is as follows:
# coding:utf-8import sysfrom PyQt5.QtCore import QRect, QRectF, QSize, Qtfrom PyQt5.QtGui import QPainter, QPixmap, QWheelEventfrom PyQt5.QtWidgets import (QApplication, QGraphicsItem, QGraphicsPixmapItem, QGraphicsScene, QGraphicsView) class ImageViewer (QGraphicsView): "" Picture Viewer "" def _ _ init__ (self) Parent=None): super (). _ init__ (parent=parent) self.zoomInTimes = 0 self.maxZoomInTimes = 22 # create scene self.graphicsScene = QGraphicsScene () # Picture self.pixmap = QPixmap (ruddy:\ hzz\ picture\ nitrate\ nitrate (2). Jpg') self.pixmapItem = QGraphicsPixmapItem (self.pixmap) self.displayedImageSize = QSize (0) 0) # initialize widget self.__initWidget () def _ _ initWidget (self): "initialize widget" self.resize (1200) # Hidden scroll bar self.setVerticalScrollBarPolicy (Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy (Qt.ScrollBarAlwaysOff) # Zoom self.setTransformationAnchor (self.AnchorUnderMouse) # smooth scaling self.pixmapItem.setTransformationMode (Qt.SmoothTransformation) self.setRenderHints (QPainter.Antialiasing | QPainter. SmoothPixmapTransform) # set scene self.graphicsScene.addItem (self.pixmapItem) self.setScene (self.graphicsScene) def wheelEvent (self E: QWheelEvent): "scroll the mouse wheel to zoom the picture"if e.angleDelta () .y () > 0: self.zoomIn () else: self.zoomOut () def resizeEvent (self)" E): "" Zoom the picture "" super () .resizeEvent (e) if self.zoomInTimes > 0: return # resize the picture ratio = self.__getScaleRatio () self.displayedImageSize = self.pixmap.size () * ratio if ratio
< 1: self.fitInView(self.pixmapItem, Qt.KeepAspectRatio) else: self.resetTransform() def setImage(self, imagePath: str): """ 设置显示的图片 """ self.resetTransform() # 刷新图片 self.pixmap = QPixmap(imagePath) self.pixmapItem.setPixmap(self.pixmap) # 调整图片大小 self.setSceneRect(QRectF(self.pixmap.rect())) ratio = self.__getScaleRatio() self.displayedImageSize = self.pixmap.size()*ratio if ratio < 1: self.fitInView(self.pixmapItem, Qt.KeepAspectRatio) def resetTransform(self): """ 重置变换 """ super().resetTransform() self.zoomInTimes = 0 self.__setDragEnabled(False) def __isEnableDrag(self): """ 根据图片的尺寸决定是否启动拖拽功能 """ v = self.verticalScrollBar().maximum() >0 h = self.horizontalScrollBar () .maximum () > 0 return v or h def _ setDragEnabled (self) IsEnabled: bool): "" sets whether the drag starts "" self.setDragMode (self.ScrollHandDrag if isEnabled else self.NoDrag) def _ _ getScaleRatio (self): "gets the zoom ratio of the displayed image and the original image" if self.pixmap.isNull (): return 1 pw = self.pixmap.width ( ) ph = self.pixmap.height () rw = min (1 Self.width () / pw) rh = min (1, self.height () / ph) return min (rw, rh) def fitInView (self, item: QGraphicsItem, mode=Qt.KeepAspectRatio): "" scale the scene to fit the window size "" super (). FitInView (item, mode) self.displayedImageSize = self.__getScaleRatio () * self.pixmap.size () self.zoomInTimes = 0 def zoomIn (self) ViewAnchor=QGraphicsView.AnchorUnderMouse): "" enlarge image "if self.zoomInTimes = = self.maxZoomInTimes: return self.setTransformationAnchor (viewAnchor) self.zoomInTimes + = 1 self.scale (1.1,1.1) self.__setDragEnabled (self.__isEnableDrag ()) # restore anchor self.setTransformationAnchor (self.AnchorUnderMouse) def zoomOut (self) ViewAnchor=QGraphicsView.AnchorUnderMouse): "" reduce the image "if self.zoomInTimes = = 0 and not self.__isEnableDrag (): return self.setTransformationAnchor (viewAnchor) self.zoomInTimes-= 1 # the size of the original image pw = self.pixmap.width () ph = self.pixmap.height () # the actual width of the image displayed Degree w = self.displayedImageSize.width () * 1.1**self.zoomInTimes h = self.displayedImageSize.height () * 1.1**self.zoomInTimes if pw > self.width () or ph > self.height (): # when the window size is smaller than the original image, it is forbidden to continue to shrink the image smaller than the window if w
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.