基于Python制作一个图片色卡提取器
作者:Sir 发布时间:2023-06-15 14:25:26
标签:Python,图片,色卡,提取
在一些特殊的业务场景中,我们需要一次性提取一张图片中的色卡信息,并且需要使用十六进制的颜色表示方法进行展示。
今天得空做了一个小工具,用来自定义的提取某一张图片中的色卡信息,需要提取某张图片中的色卡可以自行选择。
实现过程就是比较简单的,主要是通过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 the
attributes 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 = True
self.parent = parent
def __del__(self):
"""
A destructor. It is called when the object is destroyed.
"""
self.working = False
def 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('系统参数设置不能为空,请检查参数设置!')
return
colors_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_())
来源:https://mp.weixin.qq.com/s/vhCjnrTx3gZWI7Hr2MzOvQ


猜你喜欢
- http://swik.net/Ajax/Ajax+Mistakes在某网站瞎逛时,发现这个链接,进去逛了逛,觉得很有意思,大家也可以去看看
- python-redis-lock官方文档不错的博文可参考问题背景在使用celery执行我们的异步任务时,为了提高效率,celery可以开启
- 一、登录以信号与系统课程为例,直接输入网址则出现登录界面:由于学号登录需要验证码,因此选择电话登录:直接在开发者工具中找到手机号输入框、密码
- 数据采集XPath,XML路径语言的简称。XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某
- 具体代码如下所示:#字符串反转def reverse (s): rt = '' for i in r
- 我们经常会遇到数据库磁盘空间爆满的问题,或由于归档日志突增、或由于数据文件过多、大导致磁盘使用紧俏。这里主要说的场景是磁盘空间本身很大,但表
- 前言:现在写爬虫,入门已经不是一件门槛很高的事情了,网上教程一大把,但很多爬虫新手在爬取数据的过程中依旧会遇到这样那样的问题。今天整理了部分
- 用JDBC实现对MySQL的“增删改查”:import java.sql.Connection;im
- 本博客详细为你解释 Python Flask 框架下的 HTML 文件压缩内容,其第三方模块也可用在其他框架中。本案例是基于 Python
- 使用drop函数删除dataframe的某列或某行数据:drop(labels, axis=0, level=None, inplace=F
- 上一次,我们谈到在ASP中如何利用“正则表达式”对象来实现各种数据的校验,文中描述了正则表达式对象的强大功能,接下来,我们来看看有关“正则表
- 大家好,之前分享过很多关于 Pandas 的文章,今天我给大家分享5个小而美的 Pandas 实战案例。内容主要分为:如何自行模拟数据多种数
- 1、背景我们先谈谈为什么在Python编码过程中强烈推荐使用类型注解 ?Python对于初学者来说是非常好上手,原因是在于对计算机底层原理的
- 本文实例讲述了微信小程序实现动态获取元素宽高的方法。分享给大家供大家参考,具体如下:我以前一直以为微信小程序不能动态获取view元素的宽高。
- 使用RS232串口线或者是RS232转USB的这一类的接口,会需要com口作为接口来进行输入输出调式,写了个脚本来控制COM口,用到了Pyt
- 可能是我“火星”了,不过在 空虚 的 Blog 中学到的一招。这个技巧的原理是利用 iframe 载入本机各盘符的根目录,然后判断 ifra
- 新建py文件,在里面写入Python代码,代码的功能是打印10次“Hello!”,将代码文件保存到桌面上:在命令行中运行Python脚本,并
- 本文实例讲述了python 装饰器功能与用法。分享给大家供大家参考,具体如下:1、必备#### 第一波 ####def foo(): &nb
- 1、首先模拟python类似shell命令行操作的接口:python安装subprocess(本地)、paramiko(SSH远程)#-*-
- 今天在学习dubbo-go的时候,下载了dubbo-go的example,依赖的包太多了,之前都是手动下载某个依赖的包,现在手动一个一个 g