Pyqt5制作图片显示器

1、实际效果:

 2、Image_Show.py

import os

import cv2
from PIL import Image

import numpy as np
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtWidgets import QApplication, QFileDialog, QMessageBox
from PyQt5 import uic


class Image_Show:

    def __init__(self):
        # 动态加载UI文件
        self.ui = uic.loadUi('show_pic.ui')
        self.FileDirectory = ''
        self.file_list = []
        # 按钮绑定
        self.ui.pic_button.clicked.connect(self.show_img)
        self.ui.pics_button.clicked.connect(self.show_imgs)
        self.ui.down_pic.clicked.connect(self.down_img)
        self.ui.up_pic.clicked.connect(self.up_img)
        # 图片计数器
        self.count = 0
        # 图片总数
        self.pic_num = 0

    def show_img(self):
        """
        显示单张图像
        :return:
        """
        # 选择图片文件
        image_file, _ = QFileDialog.getOpenFileName(None, '选择图片', 'C:/',
                                                    'Image files (*.jpg *.gif *.png *.jpeg *.jfif)')

        # 自适应显示图片
        frame = QPixmap(image_file)
        self.ui.label.setScaledContents(True)
        # 设置标签图像
        self.ui.label.setPixmap(frame)
        # 设置进度条
        self.ui.pics_progressBar.setValue(100)

    def show_imgs(self):
        """
        显示多张图像
        :return:
        """
        # 选择文件夹
        FileDirectory = QFileDialog.getExistingDirectory(None, "选择图片文件夹", "C:/")
        # 获取目录列表
        file_list = [i for i in os.listdir(FileDirectory) if i.endswith('jpg') or i.endswith('png') or
                     i.endswith('gif') or i.endswith('jpeg') or i.endswith('.jfif')]

        if len(file_list) != 0:
            self.FileDirectory = FileDirectory
            self.file_list = file_list
            # 显示第一张图像
            first_file_path = os.path.join(FileDirectory, file_list[0])
            self.show_path_img(first_file_path)
            # 图片总数
            self.pic_num = len(file_list) + 1
            # 进度值
            progress = int((self.count + 1) / (self.pic_num - 1) * 100)
            # 更新进度条
            self.ui.pics_progressBar.setValue(progress)
        else:
            pass

    def down_img(self):
        """
        切换下一张图像
        :return:
        """
        if self.pic_num != 0 and self.count < len(self.file_list) - 1:
            self.count += 1
            now_file_path = os.path.join(self.FileDirectory, self.file_list[self.count])
            self.show_path_img(now_file_path)
            # 进度值
            progress = int((self.count + 1) / (self.pic_num-1) * 100)
            # 更新进度条
            self.ui.pics_progressBar.setValue(progress)
        else:
            pass

    def up_img(self):
        """
        切换上一张图像
        :return:
        """
        if self.count > 0:
            self.count -= 1
            now_file_path = os.path.join(self.FileDirectory, self.file_list[self.count])
            self.show_path_img(now_file_path)
            # 进度值
            progress = int((self.count + 1) / (self.pic_num-1) * 100)
            # 更新进度条
            self.ui.pics_progressBar.setValue(progress)
        else:
            pass

    def show_path_img(self, path):
        """
        显示路径图像
        :param path: 文件路径
        :return:
        """
        frame = QPixmap(path)
        # 自适应显示图片
        self.ui.label.setScaledContents(True)
        # 设置标签图像
        self.ui.label.setPixmap(frame)


app = QApplication([])
Image_Show = Image_Show()
Image_Show.ui.show()
app.exec_()
3、'show_pic.ui'
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>938</width>
    <height>568</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>图片显示器</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>70</y>
     <width>640</width>
     <height>480</height>
    </rect>
   </property>
   <property name="frameShape">
    <enum>QFrame::Box</enum>
   </property>
   <property name="text">
    <string>图像区域</string>
   </property>
   <property name="alignment">
    <set>Qt::AlignCenter</set>
   </property>
  </widget>
  <widget class="QPushButton" name="pic_button">
   <property name="geometry">
    <rect>
     <x>760</x>
     <y>270</y>
     <width>101</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>单张选择</string>
   </property>
   <property name="autoDefault">
    <bool>true</bool>
   </property>
  </widget>
  <widget class="QPushButton" name="pics_button">
   <property name="geometry">
    <rect>
     <x>760</x>
     <y>380</y>
     <width>101</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>批量选择</string>
   </property>
   <property name="autoDefault">
    <bool>true</bool>
   </property>
  </widget>
  <widget class="QPushButton" name="up_pic">
   <property name="geometry">
    <rect>
     <x>710</x>
     <y>430</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>上一张</string>
   </property>
   <property name="autoDefault">
    <bool>true</bool>
   </property>
  </widget>
  <widget class="QPushButton" name="down_pic">
   <property name="geometry">
    <rect>
     <x>840</x>
     <y>430</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>下一张</string>
   </property>
   <property name="autoDefault">
    <bool>true</bool>
   </property>
  </widget>
  <widget class="QProgressBar" name="pics_progressBar">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>40</y>
     <width>531</width>
     <height>20</height>
    </rect>
   </property>
   <property name="value">
    <number>0</number>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

 

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值