Python自定義大小截圖
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
蟈蟈這兩天正忙著收拾家當去公司報道,結果做PHP的發小蛐蛐找到了他,說是想要一個可以截圖工具。
大致需要做出這樣的效果。
雖然已經很久不寫Python程式碼了,但是沒辦法,盛情難卻啊,只好硬著頭皮上了。
關於這個需求,蟈蟈想了想,腦海裡大概有這麼幾個實現的方式。
- 呼叫QQ的截圖工具。
- 自己寫一個。
這第一個嘛,應了那句老話。理想很豐滿,現實很骨感。因為被整合的緣故,剖不出來是沒辦法用的,自認為技術還不到家的蟈蟈很快放棄了這個方法。
那麼只能自己寫一個了。從谷哥那瞭解到PIL的ImageGrab可以很方便的截圖,預設截圖是全屏範圍,當然也可以傳遞一個Bbox元組來實現截圖的範圍截圖。於是思路就很明確了:獲取滑鼠位置,呼叫ImageGrab截圖
獲取滑鼠位置
這個嘛,其實還是很簡單的。藉助pyHook
global old_x, old_y, new_x, new_y, full, hm if event.MessageName == "mouse left down": old_x, old_y = event.Position if event.MessageName == "mouse left up": new_x, new_y = event .Position
- 1
- 2
- 3
- 4
- 5
按下滑鼠的那一刻開始記錄初始座標,然後滑鼠擡起的那一刻更新結束座標。這兩個座標的範圍就是要截圖的範圍。這裡面需要注意的就是滑鼠座標預設從左上角(0, 0)開始。
截圖的具體實現
關於具體實現,無非是一個full標記,預設也是截全屏的圖,當full為False的時候,按照兩次滑鼠的絕對位置實現範圍截圖。
# 劃屏 if full: image = ImageGrab.grab((0, 0, gsm(0), gsm(1))) else: image = ImageGrab.grab((old_x, old_y, new_x, new_y)) image.show()
- 1
- 2
- 3
- 4
- 5
- 6
好啦,核心功能已經做好啦。為了方便蛐蛐進行自定義的拓展,蟈蟈把原始碼發給了他。
# coding: utf8# @Author: 郭 璞# @File: capture.py # @Time: 2017/7/24 # @Contact: [email protected]# @blog: http://blog.csdn.net/marksinoberg# @Description: 根據滑鼠移動進行劃屏截圖import pyHookimport pythoncomimport win32guifrom PIL import Image, ImageGrabfrom win32api import GetSystemMetrics as gsm# 提前繫結滑鼠位置事件old_x, old_y = 0, 0new_x, new_y = 0, 0def hotkey(key=None): """繫結熱鍵,開始進行劃屏截圖操作""" passdef on_mouse_event(event): global old_x, old_y, new_x, new_y, full, hm if event.MessageName == "mouse left down": old_x, old_y = event.Position if event.MessageName == "mouse left up": new_x, new_y = event.Position # 解除事件繫結 hm.UnhookMouse() hm = None # 劃屏 if full: image = ImageGrab.grab((0, 0, gsm(0), gsm(1))) else: image = ImageGrab.grab((old_x, old_y, new_x, new_y)) image.show()full = Falsehm = Nonedef capture(): hm = pyHook.HookManager() hm.SubscribeMouseAll(on_mouse_event) hm.HookMouse() pythoncom.PumpMessages()capture()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
核心功能已經算是完成了,雖然貌似並沒有什麼太大的用處。
因為就要走了,所以蟈蟈沒有多少時間來潤色,只能這樣匆匆交差了。除了程式碼,蟈蟈特意囑咐了下面這幾句話:
- 增加儲存到本地功能。
- 繫結系統快捷鍵,這樣打遊戲的時候也可以截圖。
- 增加蒙層,截圖的時候提供更好的使用者體驗。
蛐蛐聽完之後,貌似也有了自己的想法,然後就自己琢磨去了。其實他不知道的是,蟈蟈對於截到的圖的另一層處理。
簡易圖片相似度分析
# coding: utf8# @Author: 郭 璞# @File: similar.py # @Time: 2017/7/23 # @Contact: [email protected]# @blog: http://blog.csdn.net/marksinoberg# @Description: 兩張圖片相似度計算實現。from PIL import Imagedef pixel_way(img1, img2): image1 = Image.open(img1, 'r') image2 = Image.open(img2, 'r') return get_pixel_details(image1)==get_pixel_details(image2)def get_pixel_details(img): pixels = img.load() r, g, b = 0, 0, 0 counter = 0 for x in range(img.size[0]): for y in range(img.size[1]): counter += 1 r1, g1, b1 = pixels[x, y] r += r1 g += g1 b += b1 return (r/counter, g/counter, b/counter)if __name__ == '__main__': image1 = r'./1.png' image2 = r'./1.png' img = Image.open(image1, 'r') img.resize((256,256)).convert("RGB") print(pixel_way(image1, image2))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
圖片畫素直方圖
# coding: utf8# @Author: 郭 璞# @File: pixel-compare.py # @Time: 2017/7/24 # @Contact: [email protected]# @blog: http://blog.csdn.net/marksinoberg# @Description: 計算RGB值相關from PIL import Imagefrom PIL import ImageDrawim = Image.open('1.png')im = im.convert("L")width, height = im.sizepix = im.load()a = [0]*256for w in range(width): for h in range(height): p = pix[w, h] a[p] = a[p] + 1x = max(a)print(a, "---", len(a), '-----', x)image = Image.new('RGB', (256, 256), (255, 255, 255))draw = ImageDraw.Draw(image)for k in range(256): a[k] = a[k]*200/x source = (k, 255) target = (k, 255-a[k]) draw.line([source, target], (100, 100, 100))image.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
還有很多更好玩的,但是有時候,話多,不是一件好事,想到這裡,蟈蟈又不自覺的回憶起了那段不堪的幫忙的經歷,無奈……
the end.
- 1