在使用google或者baidu搜图的时候会发现有一个图片颜色选项,感觉非常有意思,有人可能会想这肯定是人为的去划分的,呵呵,有这种可能,但是估计人会累死, 开个玩笑,当然是通过机器识别的,海量的图片只有机器识别才能做到。
那用python能不能实现这种功能呢?答案是:能
利用python的PIL模块的强大的图像处理功能就可以做到,下面上代码:
import colorsys def get_dominant_color(image): #颜色模式转换,以便输出rgb颜色值 image = image.convert('RGBA') #生成缩略图,减少计算量,减小cpu压力 image.thumbnail((200, 200)) max_score = None dominant_color = None for count, (r, g, b, a) in image.getcolors(image.size[0] * image.size[1]): # 跳过纯黑色 if a == 0: continue saturation = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)[1] y = min(abs(r * 2104 + g * 4130 + b * 802 + 4096 + 131072) >> 13, 235) y = (y - 16.0) / (235 - 16) # 忽略高亮色 if y > 0.9: continue # Calculate the score, preferring highly saturated colors. # Add 0.1 to the saturation so we don't completely ignore grayscale # colors by multiplying the count by zero, but still give them a low # weight. score = (saturation + 0.1) * count if score > max_score: max_score = score dominant_color = (r, g, b) return dominant_color
如何使用:
from PIL import Image print get_dominant_color(Image.open('logo.jpg'))
这样就会返回一个rgb颜色,但是这个值是很精确的范围,那我们如何实现百度图片那样的色域呢??
其实方法很简单,r/g/b都是0-255的值,我们只要把这三个值分别划分相等的区间,然后组合,取近似值。例如:划分为0-127,和128-255,然后自由组 合,可以出现八种组合,然后从中挑出比较有代表性的颜色即可。
当然我只是举一个例子,你也可以划分的更细,那样显示的颜色就会更准确~~大家赶快试试吧
PS:通过pil生成缩略图的简单代码
如果是单纯地生成缩略图,我们可以通过pil很简单地办到,这段代码会强行将图片大小修改成250x156:
from PIL import Image img = Image.open('sharejs.jpg') img = img.resize((250, 156), Image.ANTIALIAS) img.save('sharejs_small.jpg')

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

Inpython,一個“列表” isaversatile,mutableSequencethatCanholdMixedDatateTypes,而“陣列” isamorememory-sepersequeSequeSequeSequeSequeRingequiringElements.1)列表

pythonlistsandArraysareBothable.1)列表Sareflexibleandsupportereceneousdatabutarelessmory-Memory-Empefficity.2)ArraysareMoremoremoremoreMemoremorememorememorememoremorememogeneSdatabutlesserversEversementime,defteringcorcttypecrecttypececeDepeceDyusagetoagetoavoavoiDerrors。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

禪工作室 13.0.1
強大的PHP整合開發環境

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

Atom編輯器mac版下載
最受歡迎的的開源編輯器