欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)可自定義大小的截屏功能

 更新時間:2018年01月20日 09:56:54   作者:Marksinoberg  
這篇文章主要介紹了Python實(shí)現(xiàn)可自定義大小的截屏功能,結(jié)合實(shí)例形式分析了Python針對指定范圍的截圖、保存等功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)可自定義大小的截屏功能。分享給大家供大家參考,具體如下:

蟈蟈這兩天正忙著收拾家當(dāng)去公司報道,結(jié)果做PHP的發(fā)小蛐蛐找到了他,說是想要一個可以截圖工具。

大致需要做出這樣的效果。

雖然已經(jīng)很久不寫Python代碼了,但是沒辦法,盛情難卻啊,只好硬著頭皮上了。

關(guān)于這個需求,蟈蟈想了想,腦海里大概有這么幾個實(shí)現(xiàn)的方式。

① 調(diào)用QQ的截圖工具。
② 自己寫一個。

這第一個嘛,應(yīng)了那句老話。理想很豐滿,現(xiàn)實(shí)很骨感。因?yàn)楸患傻木壒?,剖不出來是沒辦法用的,自認(rèn)為技術(shù)還不到家的蟈蟈很快放棄了這個方法。

那么只能自己寫一個了。從谷哥那了解到PIL的ImageGrab可以很方便的截圖,默認(rèn)截圖是全屏范圍,當(dāng)然也可以傳遞一個Bbox元組來實(shí)現(xiàn)截圖的范圍截圖。于是思路就很明確了:獲取鼠標(biāo)位置,調(diào)用ImageGrab截圖

獲取鼠標(biāo)位置

這個嘛,其實(shí)還是很簡單的。借助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

按下鼠標(biāo)的那一刻開始記錄初始坐標(biāo),然后鼠標(biāo)抬起的那一刻更新結(jié)束坐標(biāo)。這兩個坐標(biāo)的范圍就是要截圖的范圍。這里面需要注意的就是鼠標(biāo)坐標(biāo)默認(rèn)從左上角(0, 0)開始。

截圖的具體實(shí)現(xiàn)

關(guān)于具體實(shí)現(xiàn),無非是一個full標(biāo)記,默認(rèn)也是截全屏的圖,當(dāng)full為False的時候,按照兩次鼠標(biāo)的絕對位置實(shí)現(xiàn)范圍截圖。

# 劃屏
  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()

好啦,核心功能已經(jīng)做好啦。為了方便蛐蛐進(jìn)行自定義的拓展,蟈蟈把源碼發(fā)給了他。

# coding: utf8
# @Author: 郭 璞
# @File: capture.py
# @Time: 2017/7/24
# @Contact: 1064319632@qq.com
# @blog: http://blog.csdn.net/marksinoberg
# @Description: 根據(jù)鼠標(biāo)移動進(jìn)行劃屏截圖
import pyHook
import pythoncom
import win32gui
from PIL import Image, ImageGrab
from win32api import GetSystemMetrics as gsm
# 提前綁定鼠標(biāo)位置事件
old_x, old_y = 0, 0
new_x, new_y = 0, 0
def hotkey(key=None):
  """綁定熱鍵,開始進(jìn)行劃屏截圖操作"""
  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()

核心功能已經(jīng)算是完成了,雖然貌似并沒有什么太大的用處。

因?yàn)榫鸵吡?,所以蟈蟈沒有多少時間來潤色,只能這樣匆匆交差了。除了代碼,蟈蟈特意囑咐了下面這幾句話:

① 增加保存到本地功能。
② 綁定系統(tǒng)快捷鍵,這樣打游戲的時候也可以截圖。
③ 增加蒙層,截圖的時候提供更好的用戶體驗(yàn)。

蛐蛐聽完之后,貌似也有了自己的想法,然后就自己琢磨去了。其實(shí)他不知道的是,蟈蟈對于截到的圖的另一層處理。

簡易圖片相似度分析

# coding: utf8
# @Author: 郭 璞
# @File: similar.py
# @Time: 2017/7/23
# @Contact: 1064319632@qq.com
# @blog: http://blog.csdn.net/marksinoberg
# @Description: 兩張圖片相似度計算實(shí)現(xiàn)。
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))

圖片像素直方圖

# coding: utf8
# @Author: 郭 璞
# @File: pixel-compare.py
# @Time: 2017/7/24
# @Contact: 1064319632@qq.com
# @blog: http://blog.csdn.net/marksinoberg
# @Description: 計算RGB值相關(guān)
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()

還有很多更好玩的,但是有時候,話多,不是一件好事,想到這里,蟈蟈又不自覺的回憶起了那段不堪的幫忙的經(jīng)歷,無奈……

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論