基于Python制作一个图片色卡提取器
作者:Sir 时间:2023-06-15 14:25:26
在一些特殊的业务场景中,我们需要一次性提取一张图片中的色卡信息,并且需要使用十六进制的颜色表示方法进行展示。
今天得空做了一个小工具,用来自定义的提取某一张图片中的色卡信息,需要提取某张图片中的色卡可以自行选择。
实现过程就是比较简单的,主要是通过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
标签:Python,图片,色卡,提取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python深度学习tensorflow安装调试教程
2021-06-28 23:03:51
![](https://img.aspxhome.com/file/2023/9/127599_0s.png)
keras实现多种分类网络的方式
2023-07-10 13:37:49
有感用户体验规划与系统实现
2009-11-27 18:33:00
![](https://img.aspxhome.com/file/UploadPic/200911/27/01-48s.jpg)
Python爬虫番外篇之Cookie和Session详解
2022-02-09 18:56:44
![](https://img.aspxhome.com/file/2023/9/89209_0s.jpg)
Git的撤销、修改和回退命令
2022-12-05 14:10:12
一文理解MySQL数据库的约束与表的设计
2024-01-21 08:31:12
![](https://img.aspxhome.com/file/2023/2/70372_0s.png)
python urllib.request模块的使用详解
2023-03-27 18:19:15
![](https://img.aspxhome.com/file/2023/7/117287_0s.png)
在Pandas中导入CSV数据时去除默认索引的方法汇总
2023-03-16 02:35:53
Web2.0视觉风格进化论 之二
2007-11-03 20:10:00
![](https://img.aspxhome.com/file/UploadPic/200711/3/2007113201853604s.jpg)
javascript 密码强度验证规则、打分、验证
2010-05-18 19:58:00
PyTorch简单手写数字识别的实现过程
2021-07-12 17:17:52
![](https://img.aspxhome.com/file/2023/2/124332_0s.png)
网页设计的12种颜色
2011-05-21 08:40:00
100 个 Python 小例子(练习题二)
2021-02-09 10:15:31
python+opencv实现高斯平滑滤波
2023-04-02 15:56:55
![](https://img.aspxhome.com/file/2023/1/82531_0s.jpg)
Python实战之基于OpenCV的美颜挂件制作
2022-08-30 20:46:30
![](https://img.aspxhome.com/file/2023/9/97279_0s.jpg)
PHP 巧用数组降低程序的时间复杂度
2023-11-15 09:40:02
微信公众平台开发入门教程(图文详解)
2023-06-21 11:10:05
![](https://img.aspxhome.com/file/2023/6/82406_0s.jpg)
举例讲解Python程序与系统shell交互的方式
2021-10-29 14:43:17
解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题
2022-10-24 05:34:54
![](https://img.aspxhome.com/file/2023/7/67727_0s.jpg)
python变量赋值方法(可变与不可变)
2021-02-14 08:51:55
![](https://img.aspxhome.com/file/2023/9/69559_0s.jpg)