SVG格式图片,属于矢量图形,
在放大或缩小时不会失真
,与
位图图像相比
,文件通常较小。
编程的初衷是为了看股票K线图,在网上搜索了现有的软件,没有找到满足需求的,自个用python编写了一个,AI辅助,迄今用的还行。
显示器分辨率是3840X2160,用在其它分辨率的显示器上请自行调整程序里的一些参数。
要打开svg格式图片,需要用打开方式选择程序,关联编译好的程序svg_viewer.exe。
本人非专业程序人员,贻笑大方,请各位大佬嘴下留情,如能帮助优化,不胜感激!
功能介绍:按钮 P(prev) 上一个;
按钮N(next) 下一个;
按钮B(browse)打开文件夹;
按钮S(save) 保存到某一文件夹;
按钮+(add) 添加到选股.csv文件,东方财富格式,可以自选导入股票软件;
按钮D(delete) 删除图片;
按住鼠标右键移动,可以画水平线,观察支撑和阻力位;
按住ctrl键和鼠标左键,可以画斜线,用于观察趋势;
鼠标滚轮,可以以鼠标指针处为中心,对图片进行放大和缩小,比例因子可以在程序中调整;
按住鼠标左键,可以对图片拖拽,方便观察;
[Python] 纯文本查看 复制代码# !/usr/bin/env python
# coding=utf-8
from __future__ import (absolute_import, division, print_function, unicode_literals)
import glob
import os
import shutil
import sys
import csv
from PyQt5.QtCore import QTimer, QSize, QPoint, QPointF, QLineF
from PyQt5.QtCore import Qt, QVariant, pyqtSlot, QModelIndex
from PyQt5.QtGui import QFont, QColor, QBrush, QPainter, QPixmap, QIcon, QPen
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtSvg import QSvgRenderer, QGraphicsSvgItem
from PyQt5.QtWidgets import QApplication, QTableView, QStyledItemDelegate, QLabel, QWidget, QVBoxLayout, QScrollArea, \
QGraphicsView, QGraphicsScene, QPushButton, QComboBox, QFileDialog, QListWidget, QGraphicsLineItem
class MyGraphicsView(QGraphicsView):
def __init__(self, parent=None):
super(MyGraphicsView, self).__init__(parent)
self.setTransformationAnchor(QGraphicsView.NoAnchor)
self.setResizeAnchor(QGraphicsView.NoAnchor)
self.setDragMode(QGraphicsView.NoDrag) # 禁用默认的拖动模式
self.is_dragging = False # 用于跟踪是否正在拖动
self.last_mouse_pos = None # 用于存储上一次鼠标位置
self.drawing = False
self.start_point = None
self.current_line = None
def wheelEvent(self, event):
factor = 1.2 # 定义缩放因子
centerPoint = event.pos() # 获取鼠标事件的位置,即为放大或缩小的中心点
sceneCenter = self.mapToScene(centerPoint) # 将视图中的点转换为场景中的点
# 保存旧的场景坐标系中的中心点
oldPos = self.mapToScene(event.pos())
# 根据鼠标滚轮的方向放大或缩小
if event.angleDelta().y() > 0:
self.scale(factor, factor) # 向上滚动时放大
else:
self.scale(1 / factor, 1 / factor) # 向下滚动时缩小
# 获取新的坐标系中的中心点
newPos = self.mapToScene(event.pos())
# 计算旧中心到新中心的偏移
delta = newPos - oldPos
self.translate(delta.x(), delta.y()) # 对视图进行平移,保持鼠标位置下的图像点不动
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.ControlModifier:
self.drawing = True
self.last_mouse_pos = event.pos()
self.start_point = self.mapToScene(event.pos())
self.current_line = QGraphicsLineItem(QLineF(self.start_point, self.start_point))
pen = QPen(QColor(255, 0, 0), 2) # 红色线,线宽为2
self.current_line.setPen(pen)
self.scene().addItem(self.current_line)
event.accept()
else:
self.is_dragging = True
self.setCursor(Qt.OpenHandCursor)
self.last_mouse_pos = event.pos() # 存储鼠标点击时的位置
if event.button() == Qt.RightButton:
self.drawing = True
self.last_mouse_pos = event.pos()
self.start_point = self.mapToScene(event.pos())
self.current_line = QGraphicsLineItem(QLineF(self.start_point, self.start_point))
pen = QPen(QColor(255, 0, 0), 2) # 红色线,线宽为2
self.current_line.setPen(pen)
self.scene().addItem(self.current_line)
event.accept()
def mouseMoveEvent(self, event):
if self.is_dragging:
self.setCursor(Qt.ClosedHandCursor)
# 计算鼠标移动的偏移量
offset = event.pos() - self.last_mouse_pos
# 将视图相对当前位置进行平移,这样做可以使得拖动方向与鼠标移动方向一致
self.horizontalScrollBar().setValue(self.horizontalScrollBar().value() - offset.x())
self.verticalScrollBar().setValue(self.verticalScrollBar().value() - offset.y())
self.last_mouse_pos = event.pos() # 更新鼠标位置,以便下次计算偏移时使用
if self.drawing and self.current_line:
modifiers = QApplication.keyboardModifiers()
if modifiers == Qt.ControlModifier:
mapped_end_point = self.mapToScene(event.pos())
self.current_line.setLine(QLineF(self.start_point, mapped_end_point))
event.accept()
else:
end_point = QPointF(event.pos().x(), self.last_mouse_pos.y())
mapped_end_point = self.mapToScene(end_point.toPoint())
self.current_line.setLine(QLineF(self.start_point, mapped_end_point))
event.accept()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
if self.drawing:
self.drawing = False
self.current_line = None
event.accept()
else:
self.setCursor(Qt.ArrowCursor)
self.is_dragging = False
self.last_mouse_pos = None
if event.button() == Qt.RightButton and self.drawing:
self.drawing = False
self.current_line = None
event.accept()
class SVGImageWindow(QWidget):
def __init__(self, svg_file):
super().__init__()
self.setWindowTitle("SVG Viewer")
self.setFixedSize(3840, 2060) # 设置窗口的固定大小
# self.setFixedSize(2560, 1440)
layout = QVBoxLayout(self)
# 使用自定义的MyGraphicsView来显示SVG图像
self.graphics_view = MyGraphicsView()
layout.addWidget(self.graphics_view)
# 创建一个QGraphicsScene
self.graphics_scene = QGraphicsScene()
self.graphics_view.setScene(self.graphics_scene)
# 创建前翻和后翻按钮
self.prev_button = QPushButton("P", self)
self.prev_button.resize(40, 40) # 设置按钮的大小
self.prev_button.move(30, 1010) # 设置按钮的位置
# self.prev_button.move(30, 700)
self.prev_button.setStyleSheet("""
QPushButton {
background-color: lightblue; /* 背景色 */
color: green; /* 字体颜色 */
border-radius: 10px; /* 圆角效果 */
border: 2px outset lightblue;
font-size: 14px; /* 字体大小 */
font-family: Arial; /* 字体族 */
font-weight: bold; /* 字重 */
}
QPushButton:hover {
background-color: #0056b3; /* 鼠标悬停时的背景色 */
color: white;
}
QPushButton:pressed {
border: 2px inset lightblue;
}
""")
self.prev_button.clicked.connect(self.load_prev_image)
self.next_button = QPushButton("N", self)
self.next_button.resize(40, 40) # 设置按钮的大小
self.next_button.move(3750, 1010) # 设置按钮的位置
# self.next_button.move(2470, 700)
self.next_button.setStyleSheet("""
QPushButton {
background-color: lightblue; /* 背景色 */
color: green; /* 字体颜色 */
border-radius: 10px; /* 圆角效果 */
border: 2px outset lightblue;
font-size: 14px; /* 字体大小 */
font-family: Arial; /* 字体族 */
font-weight: bold; /* 字重 */
}
QPushButton:hover {
background-color: #0056b3; /* 鼠标悬停时的背景色 */
color: white;
}
QPushButton:pressed {
border: 2px inset lightblue;
}
""")
self.next_button.clicked.connect(self.load_next_image)
# 获取当前目录下所有SVG文件
folderPath, file_name = os.path.split(svg_file)
self.current_file = svg_file
self.svg_files = [os.path.normpath(path) for path in glob.glob(folderPath + "/" + "*.svg")]
self.current_index = self.svg_files.index(os.path.normpath(self.current_file))
# self.svg_files = svg_file glob.glob("D:\\akstock\\draw\\板块\\rps\\" + "*.svg")
# self.current_index = 0
# self.current_file = ''
# 创建浏览按钮
self.browseButton = QPushButton("B", self)
self.browseButton.resize(40, 40) # 设置按钮的大小
self.browseButton.move(30, 1080) # 设置按钮的位置
# self.browseButton.move(30, 760)
self.browseButton.setStyleSheet("""
QPushButton {
background-color: yellow; /* 背景色 */
color: green; /* 字体颜色 */
border-radius: 10px; /* 圆角效果 */
border: 2px outset lightblue;
font-size: 14px; /* 字体大小 */
font-family: Arial; /* 字体族 */
font-weight: bold; /* 字重 */
}
QPushButton:hover {
background-color: #0056b3; /* 鼠标悬停时的背景色 */
color: white;
}
QPushButton:pressed {
border: 2px inset lightblue;
}
""")
self.browseButton.clicked.connect(self.browseFolders)
self.saveButton = QPushButton("S", self)
self.saveButton.resize(40, 40) # 设置按钮的大小
self.saveButton.move(3750, 1080) # 设置按钮的位置
# self.saveButton.move(2470, 760)
self.saveButton.setStyleSheet("""
QPushButton {
background-color: yellow; /* 背景色 */
color: green; /* 字体颜色 */
border-radius: 10px; /* 圆角效果 */
border: 2px outset lightblue;
font-size: 14px; /* 字体大小 */
font-family: Arial; /* 字体族 */
font-weight: bold; /* 字重 */
}
QPushButton:hover {
background-color: #0056b3; /* 鼠标悬停时的背景色 */
color: white;
}
QPushButton:pressed {
border: 2px inset lightblue;
}
""")
self.saveButton.clicked.connect(self.saveSVG)
self.addButton = QPushButton("+", self)
self.addButton.resize(40, 40) # 设置按钮的大小
self.addButton.move(3750, 1150) # 设置按钮的位置
# self.saveButton.move(2470, 820)
self.addButton.setStyleSheet("""
QPushButton {
background-color: lime; /* 背景色 */
color: black; /* 字体颜色 */
border-radius: 10px; /* 圆角效果 */
border: 2px outset lightblue;
font-size: 14px; /* 字体大小 */
font-family: Arial; /* 字体族 */
font-weight: bold; /* 字重 */
}
QPushButton:hover {
background-color: #0056b3; /* 鼠标悬停时的背景色 */
color: white;
}
QPushButton:pressed {
border: 2px inset lightblue;
}
""")
self.addButton.clicked.connect(self.saveCSV)
self.deleteButton = QPushButton("D", self)
self.deleteButton.resize(40, 40) # 设置按钮的大小
self.deleteButton.move(3750, 1250) # 设置按钮的位置
# self.deleteButton.move(2470, 820)
self.deleteButton.setStyleSheet("""
QPushButton {
background-color: red; /* 背景色 */
color: green; /* 字体颜色 */
border-radius: 10px; /* 圆角效果 */
border: 2px outset lightblue;
font-size: 14px; /* 字体大小 */
font-family: Arial; /* 字体族 */
font-weight: bold; /* 字重 */
}
QPushButton:hover {
background-color: #0056b3; /* 鼠标悬停时的背景色 */
color: white;
}
QPushButton:pressed {
border: 2px inset lightblue;
}
""")
self.deleteButton.clicked.connect(self.deleteSVG)
self.setLayout(layout)
# 加载第一个SVG图像
self.load_svg_image()
def update_scene(self, svg_path):
self.graphics_scene.clear() # 清除现有的图像
# 加载SVG图像
svg_renderer = QSvgRenderer(svg_path)
if svg_renderer.isValid(): # 检查SVG是否载入成功
svg_item = QGraphicsSvgItem()
svg_item.setSharedRenderer(svg_renderer)
self.graphics_scene.addItem(svg_item) # 将SVG图像添加到场景中
# 调整SVG图像的大小以适应窗口
scale_factor = min(self.width() / svg_renderer.defaultSize().width(),
self.height() / svg_renderer.defaultSize().height())
svg_item.setScale(scale_factor)
# 设置SVG图像的中心对齐
svg_item.setPos((self.width() - svg_item.boundingRect().width()) / 2,
(self.height() - svg_item.boundingRect().height()) / 2)
def load_svg_image(self):
if self.svg_files: # 检查是否有SVG文件
svg_path = self.svg_files[self.current_index]
self.current_file = svg_path
self.update_scene(svg_path)
def load_prev_image(self):
if self.current_index > 0:
self.prev_button.clearFocus()
self.current_index -= 1
self.load_svg_image()
def load_next_image(self):
if self.current_index 1:
svg_file = sys.argv[1]
else:
svg_file = None
window = SVGImageWindow(svg_file)
window.show()
sys.exit(app.exec_())
通过网盘分享的文件:SVG图像浏览器
链接: https://pan.baidu.com/s/1kJJxXOpL_LIipJMDCVBTQw?pwd=52pj 提取码: 52pj