最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Python制作一個(gè)圖片色卡提取器

 更新時(shí)間:2022年12月02日 09:11:07   作者:Sir 老王  
在一些特殊的業(yè)務(wù)場(chǎng)景中,我們需要一次性提取一張圖片中的色卡信息,并且需要使用十六進(jìn)制的顏色表示方法進(jìn)行展示。本文就來(lái)用Python做個(gè)圖片色卡提取器,感興趣的可以嘗試一下

在一些特殊的業(yè)務(wù)場(chǎng)景中,我們需要一次性提取一張圖片中的色卡信息,并且需要使用十六進(jìn)制的顏色表示方法進(jìn)行展示。

今天得空做了一個(gè)小工具,用來(lái)自定義的提取某一張圖片中的色卡信息,需要提取某張圖片中的色卡可以自行選擇。

實(shí)現(xiàn)過(guò)程就是比較簡(jiǎn)單的,主要是通過(guò)extcolors的python非標(biāo)準(zhǔn)庫(kù)來(lái)實(shí)現(xiàn)的。

另外的python非標(biāo)準(zhǔn)庫(kù)就是PyQt5的使用,還有os、sys等系統(tǒng)或文件操作模塊。

沒(méi)有安裝上述幾個(gè)python非標(biāo)準(zhǔn)庫(kù)的話,我們直接使用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)入到開(kāi)發(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組件及布局的使用類(lèi),將UI相關(guān)的操作和槽函數(shù)全部放到這個(gè)類(lèi)中進(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('開(kāi)始提取顏色')
????????self.start_btn.clicked.connect(self.start_btn_click)

????????self.brower?=?QTextBrowser()
????????self.brower.setReadOnly(True)
????????self.brower.setFont(QFont('宋體',?8))
????????self.brower.setPlaceholderText('日志處理過(guò)程區(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類(lèi),繼承自子線程QThread,將提取色卡的業(yè)務(wù)操作寫(xiě)到這個(gè)類(lèi)中,和UI主線程分開(kāi)處理保證不影響主線程的邏輯處理。

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è)面應(yīng)用啟動(dòng)起來(lái)就大功告成啦!

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)文章

  • PyCharm:method may be static問(wèn)題及解決

    PyCharm:method may be static問(wèn)題及解決

    這篇文章主要介紹了PyCharm:method may be static問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • python io.BytesIO簡(jiǎn)介及示例代碼

    python io.BytesIO簡(jiǎn)介及示例代碼

    io.BytesIO 是 Python 內(nèi)置的一個(gè) I/O 類(lèi),用于在內(nèi)存中讀寫(xiě)二進(jìn)制數(shù)據(jù),這篇文章主要介紹了python io.BytesIO簡(jiǎn)要介紹及示例,需要的朋友可以參考下
    2023-05-05
  • python生成word合同的實(shí)例方法

    python生成word合同的實(shí)例方法

    在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python生成word合同的實(shí)例方法相關(guān)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2021-01-01
  • 使用Python為中秋節(jié)繪制一塊美味的月餅

    使用Python為中秋節(jié)繪制一塊美味的月餅

    這篇文章主要介紹了使用Python為中秋節(jié)繪制一塊美味的月餅,,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Python如何獲取Win7,Win10系統(tǒng)縮放大小

    Python如何獲取Win7,Win10系統(tǒng)縮放大小

    這篇文章主要介紹了Python如何獲取Win7,Win10系統(tǒng)縮放大小,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Python中Pyspider爬蟲(chóng)框架的基本使用詳解

    Python中Pyspider爬蟲(chóng)框架的基本使用詳解

    這篇文章主要介紹了Python中Pyspider爬蟲(chóng)框架的基本使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • python基于ID3思想的決策樹(shù)

    python基于ID3思想的決策樹(shù)

    這篇文章主要為大家詳細(xì)介紹了python基于ID3思想的決策樹(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 詳解django+django-celery+celery的整合實(shí)戰(zhàn)

    詳解django+django-celery+celery的整合實(shí)戰(zhàn)

    這篇文章主要介紹了詳解django+django-celery+celery的整合實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 500行代碼使用python寫(xiě)個(gè)微信小游戲飛機(jī)大戰(zhàn)游戲

    500行代碼使用python寫(xiě)個(gè)微信小游戲飛機(jī)大戰(zhàn)游戲

    這篇文章主要介紹了500行代碼使用python寫(xiě)個(gè)微信小游戲飛機(jī)大戰(zhàn)游戲,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Python的三種主要模塊介紹

    Python的三種主要模塊介紹

    這篇文章介紹了Python的三類(lèi)主要模塊,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07

最新評(píng)論

格尔木市| 普洱| 勃利县| 阿图什市| 德安县| 六盘水市| 石屏县| 青海省| 道真| 同德县| 敦煌市| 望都县| 阿克苏市| 清远市| 岳西县| 中西区| 余庆县| 南昌市| 通辽市| 兴业县| 绥芬河市| 页游| 大余县| 塔河县| 贵德县| 陆良县| 化德县| 神农架林区| 清苑县| 合江县| 台北县| 阿拉善盟| 仁寿县| 平阳县| 小金县| 阿坝| 安溪县| 丰都县| 襄汾县| 苍南县| 安义县|