基于Python制作一個(gè)圖片色卡提取器
在一些特殊的業(yè)務(wù)場(chǎng)景中,我們需要一次性提取一張圖片中的色卡信息,并且需要使用十六進(jìn)制的顏色表示方法進(jìn)行展示。
今天得空做了一個(gè)小工具,用來自定義的提取某一張圖片中的色卡信息,需要提取某張圖片中的色卡可以自行選擇。
實(shí)現(xiàn)過程就是比較簡(jiǎn)單的,主要是通過extcolors的python非標(biāo)準(zhǔn)庫來實(shí)現(xiàn)的。
另外的python非標(biāo)準(zhǔn)庫就是PyQt5的使用,還有os、sys等系統(tǒng)或文件操作模塊。
沒有安裝上述幾個(gè)python非標(biāo)準(zhǔn)庫的話,我們直接使用pip的方式安裝一下即可。
pip?install?PyQt5?-i?https://pypi.tuna.tsinghua.edu.cn/simple/ pip?install?extcolors?-i?https://pypi.tuna.tsinghua.edu.cn/simple/
在安裝完成相關(guān)的模塊之后,將我們需要的python模塊導(dǎo)入到開發(fā)的代碼塊中。
#?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
在代碼塊中創(chuàng)建ColorUI作為UI組件及布局的使用類,將UI相關(guān)的操作和槽函數(shù)全部放到這個(gè)類中進(jìn)行處理。
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('圖片顏色提取器?公眾號(hào):Python 集中營(yíng)') ????????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('公眾號(hào):Python 集中營(yíng)') ????????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('設(shè)置提取色卡數(shù)量:') ????????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('日志處理過程區(qū)域...') ????????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)
創(chuàng)建一個(gè)ColorWork類,繼承自子線程QThread,將提取色卡的業(yè)務(wù)操作寫到這個(gè)類中,和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('系統(tǒng)參數(shù)設(shè)置不能為空,請(qǐng)檢查參數(shù)設(shè)置!') ????????????????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('系統(tǒng)運(yùn)行出現(xiàn)錯(cuò)誤,請(qǐng)檢查相關(guān)參數(shù)是否正確!')
最后,我們按照main的標(biāo)準(zhǔn)處理方式,將整個(gè)頁面應(yīng)用啟動(dòng)起來就大功告成啦!
if?__name__?==?'__main__': ????app?=?QApplication(sys.argv) ????main?=?ColorUI() ????main.show() ????sys.exit(app.exec_())
到此這篇關(guān)于基于Python制作一個(gè)圖片色卡提取器的文章就介紹到這了,更多相關(guān)Python圖片色卡提取器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
scrapy在python爬蟲中搭建出錯(cuò)的解決方法
在本篇文章里小編給大家整理了一篇關(guān)于scrapy在python爬蟲中搭建出錯(cuò)的解決方法,有需要的朋友們可以學(xué)習(xí)參考下。2020-11-11Python實(shí)現(xiàn)的生成自我描述腳本分享(很有意思的程序)
這篇文章主要介紹了Python實(shí)現(xiàn)的生成自我描述腳本分享,很有意思的程序,繞的人有點(diǎn)頭暈,需要的朋友參考下吧2014-07-07Python數(shù)據(jù)分析入門之教你怎么搭建環(huán)境
本篇文章要有一定的Python基礎(chǔ),知道列表,字符串,函數(shù)等的用法. 文中有非常詳細(xì)的代碼示例,對(duì)正在入門python數(shù)據(jù)分析的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05Python學(xué)習(xí)筆記(一)(基礎(chǔ)入門之環(huán)境搭建)
本系列為Python學(xué)習(xí)相關(guān)筆記整理所得,IT人,多學(xué)無害,多多探索,激發(fā)學(xué)習(xí)興趣,開拓思維,不求高大上,只求懂點(diǎn)皮毛,作為知識(shí)儲(chǔ)備,不至于落后太遠(yuǎn)。本文主要介紹Python的相關(guān)背景,環(huán)境搭建。2014-06-06python機(jī)器學(xué)習(xí)GCN圖卷積神經(jīng)網(wǎng)絡(luò)原理解析
這篇文章主要為大家介紹了GCN圖卷積神經(jīng)網(wǎng)絡(luò)原理及代碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05