2016년 10월 5일 수요일

Fast screen capture in python.

I need to screen capture to make simple macro program.

First, I use ImageGrab in pillow or pyscreenshot.
But, These are too slow for my program.

Then, I found good library-wx.
wx is GUI development library based on C++.
Capture method in wx is more faster than ImageGrab.

But, on pixel processing using pillow methods, PNG is on error.
I don't know why it is, BMP is ok.

Sample code in python.

import time
import pyscreenshot as ImageGrab
import wx
from PIL import Image


def main():
    count = 10
 
    start = time.time()
    for i in xrange(count):
        im = ImageGrab.grab(bbox=(0, 0, 100, 100))
        im.save('ImageGrab.png')
     
    print time.time() - start

    start = time.time()
    app = wx.App()
    screen = wx.ScreenDC()
    for i in xrange(count):
        bmp = wx.EmptyBitmap(100, 100)
        mem = wx.MemoryDC(bmp)
        mem.Blit(0, 0, 100, 100, screen, 0, 0)
        del mem
        bmp.SaveFile('wx.bmp', wx.BITMAP_TYPE_BMP)
     
    print time.time() - start


if __name__ == '__main__':
    main()


Get 0,0 to 100,100 screen image and save it 10 times.

Execute results.

17.631000042
0.203000068665

In my computer, wx is faster than pyscreenshot about 80 times.
If you need to fast screen capture, use wx.

wx install url:
https://wxpython.org/download.php

댓글 없음:

댓글 쓰기