在一些特殊的业务场景中,我们需要一次性提取一张图片中的色卡信息,并且需要使用十六进制的颜色表示方法进行展示。

今天得空做了一个小工具,用来自定义的提取某一张图片中的色卡信息,需要提取某张图片中的色卡可以自行选择。

实现过程就是比较简单的,主要是通过extcolors的python非标准库来实现的。

另外的python非标准库就是PyQt5的使用,还有os、sys等系统或文件操作模块。

没有安装上述几个python非标准库的话,我们直接使用pip的方式安装一下即可。

pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple/pip install extcolors -i https://pypi.tuna.tsinghua.edu.cn/simple/

在安装完成相关的模块之后,将我们需要的python模块导入到开发的代码块中。

# It's a module that allows you to print the stack trace of an exception.
import traceback# It's a module that allows you to print the stack trace of an exception.
import extcolors# It's importing all the classes from the QtWidgets module.
from PyQt5.QtWidgets import *# It's importing all the classes from the QtGui module.
from PyQt5.QtGui import *# It's importing all the classes from the QtCore module.
from PyQt5.QtCore import *# It's importing the sys module.
import sys# It's importing the os module.
import os

在代码块中创建ColorUI作为UI组件及布局的使用类,将UI相关的操作和槽函数全部放到这个类中进行处理。

class ColorUI(QWidget):def __init__(self):"""A constructor. It is called when an object is created from a class and it allows the class to initialize theattributes of a class."""super(ColorUI, self).__init__()self.init_ui()def init_ui(self):"""This function initializes the UI."""self.setWindowTitle('图片颜色提取器 公众号:Python 集中营')self.setWindowIcon(QIcon('color.ico'))self.resize(500, 300)self.image_label = QLabel()self.image_label.setMinimumWidth(300)self.image_label.setMaximumHeight(300)self.image_label.setText('公众号:Python 集中营')self.image_label.setAlignment(Qt.AlignCenter)self.image_label.setStyleSheet('font-size:20px;color:blue;')self.image_label.setScaledContents(True)self.image_path_in = QLineEdit()self.image_path_in.setPlaceholderText('源图片路径')self.image_path_in.setReadOnly(True)self.image_path_btn = QPushButton()self.image_path_btn.setText('加载源图片')self.image_path_btn.clicked.connect(self.image_path_btn_click)self.set_color_num_label = QLabel()self.set_color_num_label.setText('设置提取色卡数量:')self.set_color_num_in = QLineEdit()self.set_color_num_in.setPlaceholderText('例如:10')self.start_btn = QPushButton()self.start_btn.setText('开始提取颜色')self.start_btn.clicked.connect(self.start_btn_click)self.brower = QTextBrowser()self.brower.setReadOnly(True)self.brower.setFont(QFont('宋体', 8))self.brower.setPlaceholderText('日志处理过程区域...')self.brower.ensureCursorVisible()hbox = QHBoxLayout()left_box = QVBoxLayout()right_box = QVBoxLayout()left_box.addWidget(self.image_label)right_form_box = QFormLayout()right_form_box.addRow(self.image_path_in, self.image_path_btn)right_form_box.addRow(self.set_color_num_label, self.set_color_num_in)right_form_box.addRow(self.start_btn)right_box.addLayout(right_form_box)right_box.addWidget(self.brower)hbox.addLayout(left_box)hbox.addLayout(right_box)self.thread_ = ColorWork(self)self.thread_.message.connect(self.show_message)self.setLayout(hbox)def show_message(self, text):"""It shows a message:param text: The text to be displayed"""cursor = self.brower.textCursor()cursor.movePosition(QTextCursor.End)self.brower.append(text)self.brower.setTextCursor(cursor)self.brower.ensureCursorVisible()def start_btn_click(self):"""A function that is called when the start button is clicked."""self.thread_.start()def image_path_btn_click(self):"""It opens a file dialog box to select the image file."""path = QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(), "Image File (*.jpg);;Image File (*.png)")self.image_path_in.setText(path[0])pixmap = QPixmap(path[0])self.image_label.clear()self.image_label.setPixmap(pixmap)

创建一个ColorWork类,继承自子线程QThread,将提取色卡的业务操作写到这个类中,和UI主线程分开处理保证不影响主线程的逻辑处理。

class ColorWork(QThread):message = pyqtSignal(str)def __init__(self, parent=None):"""A constructor that initializes the class.:param parent: The parent widget"""super(ColorWork, self).__init__(parent)self.working = Trueself.parent = parentdef __del__(self):"""A destructor. It is called when the object is destroyed."""self.working = Falsedef run(self):"""*|CURSOR_MARCADOR|*"""try:image_path_in = self.parent.image_path_in.text().strip()set_color_num_in = self.parent.set_color_num_in.text().strip()if image_path_in == '' or set_color_num_in == '':self.message.emit('系统参数设置不能为空,请检查参数设置!')returncolors_x = extcolors.extract_from_path(image_path_in, tolerance=12, limit=int(set_color_num_in))for turple_ in colors_x[0]:rgb_ = turple_[0]color_16 = ('{:02X}' * 3).format(rgb_[0], rgb_[1], rgb_[2])color_16 = ('#' + color_16)self.message.emit(color_16)except:traceback.print_exc()self.message.emit('系统运行出现错误,请检查相关参数是否正确!')

最后,我们按照main的标准处理方式,将整个页面应用启动起来就大功告成啦!

if __name__ == '__main__':app = QApplication(sys.argv)main = ColorUI()main.show()sys.exit(app.exec_())

下面使用一个动态图片简单演示一下提取色卡信息的处理过程。

色卡提取器应用color-catch.zip已打包成exe文件,公众号内回复 ‘色卡提取器’ 获取百度网盘的下载链接。

手撕一个图片色卡提取器,可自定义提取色卡数量!相关推荐

  1. JMeter关联:JMeter正则表达式提取器与JSON提取器

    JMeter使用正则表达式和JSON提取器实现关联 前言 1 关联的释义与示例 2 常用正则表达式详解 3 正则表达式提取器 3.1 参数详解 3.2 使用示例 4 JSON提取器 4.1 参数详解 ...

  2. jmeter的json提取器和json提取器取值

    前言: 本人觉得,json提取器比正则提取器的使用场景更多,大部分接口返回值其实是可以通过json提取器取值.正则表达式比我来说易用性不强,推荐直接学json提取器干活 思路: 1.试试怎么提取要取的 ...

  3. Free Icon Tool(icon图标提取器)绿色便携版V2.1.5 | 应用图标提取器下载 | 快速提取exe中的ico图标

    Free Icon Tool 是一款短小精悍且非常实用的应用图标提取器软件,能帮助大家非常方便的提取dll.ocx.cpl.cil.exe中的ico图标文件,所以也叫icon图标提取器,Free Ic ...

  4. jmeter如何通过后置处理器提取(正则提取器、json提取器)做接口关联?

    一.后置处理器-正则表达式提取器 1.添加正则表达式提取 2.正则表达式提取面板介绍: 引用名称:提取引用名称,下个请求要引用此参数名称,如填写token,下个请求中用${token} 正则表达式:( ...

  5. 性能测试之实现接口关联的两种方式:正则表达式提取器和json提取器

    关联通俗来讲就是把上一次请求的返回内容中的部分截取出来保存为参数,用来传递给下一个请求使用. 示例: 1.用户密码进行登录,登录后生成authentication 2.需要将登录接口响应结果中auth ...

  6. scala 提取器模式匹配_Scala提取器应用,取消应用和模式匹配

    scala 提取器模式匹配 Scala extractor is an object which has a method called unapply as one of its members. ...

  7. 手撕一个spirng IoC的过程

    概述 IoC(Inversion of Controller),即控制反转.它是一种设计思想,简单说就是创建Java对象的过程从之前new出来,变成了由Spring工厂创建出来(由Spring来负责控 ...

  8. 写一个图片预览器(react-native),温习一下初中数学

    statistics source download download/month npmjs.com npm.taobao.org cnpmjs.org 需求很简单: 可以放大任意一处我想放大的地方 ...

  9. local tomcat 找不到springmvc里的包_唰唰的手撕一个简单的Spring Mvc 框架

    @[TOC] 自定义实现Spring Mvc框架 前言 在使用Spring Mvc时候我们需要配置一个中央处理器DispatcherServlet用于分发请求,根据url查找处理器,然后再根据处理器 ...

最新文章

  1. STM32F030控制蜂鸣器定时响和控制LED亮
  2. python定义map数据_「每日一练」巧用Python处理列表中的数据
  3. linux内核分析与移植,内核分析移植
  4. 串口字符传输时间计算
  5. (day 52 - DFS) 剑指 Offer 68 - II. 二叉树的最近公共祖先
  6. linux uvc协议_linux uvc 深入理解(一)
  7. 小米笔记本 镜像_小米笔记本Air 13.3原装出厂WIN10 2004 ISO镜像下载
  8. ubuntu下Makefile:xxx: recipe for target ‘xxx‘ failed
  9. 金融学核心期刊有哪些?
  10. 高中数学怎么学好学好高中数学的技巧
  11. codeforce 760 B Frodo and pillows 二分搜索
  12. 5G大数据时代到来是全面多领域的变革
  13. 内存卡删除的视频能恢复吗?四个步骤
  14. 称为超级计算机,怎样的计算机被称为“巨型计算机”呢?
  15. 用中文日淘:“日亚”的近85万件商品进驻亚马逊中国
  16. windows 系统R配置默认多核运算
  17. 基础知识 | es6的知识点
  18. 位操作技巧:Bit Twiddling Hacks
  19. 5G时代下的室内定位技术--精准室内定位--新导智能
  20. excel统计求和:如何在合并后的单元格中复制求和公式

热门文章

  1. layui文件上传回调前对文件类型及大小判断
  2. JUC-Synchronized相关内容
  3. pandownload 替代品_Pandownload替代品(天翼云盘)
  4. PAT:06-图3 六度空间
  5. 暴力枚举——妖梦拼木棒
  6. django开发websocket接口
  7. AMD 移动显卡催化剂 (Catalyst Mobility) 12.10 正式版
  8. Proteus仿真及应用——51单片机系列
  9. 来说说我暑期找实习的事儿吧(搜狗、百度、网易等公司电面面经)
  10. 圆弧方向判断方法和三点确定一个圆的计算方法