python實現qq截圖

有人找我要,我覺得發一下吧,雖然代碼挺丑的,但總有人問。

UI用wx寫的,核心庫就是PIL庫

# -*- coding: utf8 -*-

import wx

import os

from PIL import ImageDraw,ImageEnhance

from PIL import ImageGrab,Image

im = None #截屏圖片

im2 = None #im的部分截圖

im3 = None #截屏調暗了的圖片

def grabim():

global im,im3

#獲取屏幕截屏out.jpg

im = ImageGrab.grab()

addr = os.getcwd() + \out.jpg

im.save(addr,jpeg)

#enhance = ImageEnhance.Color(im)

#使用enhance處理截得的圖片out.jpg,調暗另存為out3.jpg

enhance = ImageEnhance.Brightness(im)

im3 = enhance.enhance(0.2)

im3.save(out3.jpg)

def cropIm(box):

#根據box的位置參數信息,截取im的一部分作為BitMap返回

#box為一個四元組(x1,y1,x2,y2)

global im

#im = Image.open(out.jpg)

im3 = im.crop(box)

return getBitmap(im3)

def getBitmap(pilImage):

#將Image 轉換為wx能夠識別的格式

image = wx.EmptyImage(pilImage.size[0], pilImage.size[1])

bitmap = image.CovertToBitmap()

return bitmap

class PaintWindow(wx.Window):

def __init__(self, parent, id):

wx.Window.__init__(self, parent, id,size=(2048,1080))

#self.SetBackgroundColour("Red")

self.parent = parent

self.color = "Green"

self.thickness = 1

self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)

self.lines = []

self.curLine = []

self.pos = (0, 0)

self.InitBuffer()

#綁定滑鼠動作

self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)

self.Bind(wx.EVT_MOTION, self.OnMotion)

self.Bind(wx.EVT_SIZE, self.OnSize)

self.Bind(wx.EVT_IDLE, self.OnIdle)

self.Bind(wx.EVT_PAINT, self.OnPaint)

def InitBuffer(self):

size = self.GetClientSize()

self.buffer = wx.EmptyBitmap(size.width, size.height)

self.reInitBuffer = False

dc = wx.BufferedDC(None, self.buffer)

dc.DrawBitmap(wx.Bitmap(out.jpg),0.,0,True)

#dc.Clear()

def GetLinesData(self):

return self.lines[:]

def SetLinesData(self, lines):

pass

def OnLeftDown(self, event):

self.pos = event.GetPositionTuple()

self.CaptureMouse()

def OnLeftUp(self, event):

global im,im2

if self.HasCapture():

x, y = event.GetPositionTuple()

x2, y2 = self.pos

if x>x2:

t = x

x=x2

x2=t

if y > y2:

t = y

y = y2

y2 = t

#im = Image.open(out.jpg)

box = (x,y,x2,y2)

im2 = im.crop(box)

im2.save(out2.jpg)

#result()

dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)

dc.Clear()

self.ReleaseMouse()

self.parent.Destroy()

#wx.Exit()

def OnMotion(self, event):

if event.Dragging() and event.LeftIsDown():

dc = wx.BufferedDC(wx.ClientDC(self), self.buffer)

self.drawMotion(dc, event)

event.Skip()

def drawMotion(self, dc, event):

dc.SetPen(self.pen)

dc.SetBrush(wx.TRANSPARENT_BRUSH)

x,y = event.GetPositionTuple()

x2,y2 = self.pos

width = abs(x-x2)

height = abs(y-y2)

size = (width,height)

if x > x2:

t = x

x = x2

x2 = t

if y > y2:

t = y

y = y2

y2 = t

box = (x, y, x2, y2)

pos = (x,y)

dc.Clear()

dc.DrawBitmap(wx.Bitmap(out3.jpg), 0., 0, True)

dc.SetClippingRegion(x,y,width,height)

dc.DrawBitmap(wx.Bitmap(out.jpg), 0., 0, True)

dc.DrawRectanglePointSize(pos,size)

def OnSize(self, event):

self.reInitBuffer = True

def OnIdle(self, event):

if self.reInitBuffer:

self.InitBuffer()

self.Refresh(False)

def OnPaint(self, event):

dc = wx.BufferedPaintDC(self, self.buffer)

def DrawLines(self, dc):

for colour, thickness, line in self.lines:

pen = wx.Pen(colour, thickness, wx.SOLID)

dc.SetPen(pen)

for coords in line:

dc.DrawLine(*coords)

def SetColor(self, color):

self.color = color

self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)

def SetThickness(self, num):

self.thickness = num

self.pen = wx.Pen(self.color, self.thickness, wx.SOLID)

class PaintFrame(wx.Frame):

def __init__(self, parent):

#這裡我手動將窗口設置為自己的解析度大小,可以改一下

#syle=0即無邊框

wx.Frame.__init__(self, parent, -1, "Panit Frame",stylex=0, size=(1366, 768))

self.paint = PaintWindow(self, -1)

if __name__ == __main__:

grabim()

app = wx.PySimpleApp()

frame = PaintFrame(None)

frame.Show(True)

app.MainLoop()


推薦閱讀:

杭州保姆縱火案、羅一笑事件——2017年這些網路熱議話題你還記得嗎?
浙江天搜科技:多元布局能夠為發展帶來更多可能性
2017 年總結——工程之美
谷歌搜索是什麼?
讓我們來看好一下南京的互聯網

TAG:Python | 互聯網 | QQ截圖 |