本文實例講述了Python實現可自定義大小的截屏功能。分享給大家供大家參考,具體如下:
蟈蟈這兩天正忙著收拾家當去公司報道,結果做PHP的發小蛐蛐找到了他,說是想要一個可以截圖工具。
大致需要做出這樣的效果。
雖然已經很久不寫Python代碼了,但是沒辦法,盛情難卻啊,只好硬著頭皮上了。
關于這個需求,蟈蟈想了想,腦海里大概有這么幾個實現的方式。
① 調用QQ的截圖工具。
② 自己寫一個。
這第一個嘛,應了那句老話。理想很豐滿,現實很骨感。因為被集成的緣故,剖不出來是沒辦法用的,自認為技術還不到家的蟈蟈很快放棄了這個方法。
那么只能自己寫一個了。從谷哥那了解到PIL的ImageGrab可以很方便的截圖,默認截圖是全屏范圍,當然也可以傳遞一個Bbox元組來實現截圖的范圍截圖。于是思路就很明確了:獲取鼠標位置,調用ImageGrab截圖
獲取鼠標位置
這個嘛,其實還是很簡單的。借助pyHook就可以啦。
1
2
3
4
5
|
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 |
按下鼠標的那一刻開始記錄初始坐標,然后鼠標抬起的那一刻更新結束坐標。這兩個坐標的范圍就是要截圖的范圍。這里面需要注意的就是鼠標坐標默認從左上角(0, 0)開始。
截圖的具體實現
關于具體實現,無非是一個full標記,默認也是截全屏的圖,當full為False的時候,按照兩次鼠標的絕對位置實現范圍截圖。
1
2
3
4
5
6
|
# 劃屏 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
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
|
# coding: utf8 # @Author: 郭 璞 # @File: capture.py # @Time: 2017/7/24 # @Contact: [email protected] # @blog: http://blog.csdn.net/marksinoberg # @Description: 根據鼠標移動進行劃屏截圖 import pyHook import pythoncom import win32gui from PIL import Image, ImageGrab from win32api import GetSystemMetrics as gsm # 提前綁定鼠標位置事件 old_x, old_y = 0 , 0 new_x, new_y = 0 , 0 def hotkey(key = None ): """綁定熱鍵,開始進行劃屏截圖操作""" pass def 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 = False hm = None def 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
|
# coding: utf8 # @Author: 郭 璞 # @File: similar.py # @Time: 2017/7/23 # @Contact: [email protected] # @blog: http://blog.csdn.net/marksinoberg # @Description: 兩張圖片相似度計算實現。 from PIL import Image def 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
|
# 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 Image from PIL import ImageDraw im = Image. open ( '1.png' ) im = im.convert( "L" ) width, height = im.size pix = im.load() a = [ 0 ] * 256 for w in range (width): for h in range (height): p = pix[w, h] a[p] = a[p] + 1 x = 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() |
還有很多更好玩的,但是有時候,話多,不是一件好事,想到這里,蟈蟈又不自覺的回憶起了那段不堪的幫忙的經歷,無奈……
希望本文所述對大家Python程序設計有所幫助。
原文鏈接:http://blog.csdn.net/marksinoberg/article/details/76039134