mkDoc.py
画面をキャプチャーします
キャプチャーされたファイルは、ソース(mkDoc.py)があるフォルダ
ファイル名は、月日_連番.pngです
例
1230_36.png
使い方
「取りたいウインドウをクリック」が表示されたらOKを押して、
ターゲットウインドウを表示させて、そのウインドウのどこかをクリックしてください。
画面キャプチャーは、シフト+ESCです
ポイント箇所は、シフト+Ctrlです、赤い円が表示されます
赤い円が表示されたら、入力領域がないところで、「i」を押します、
色がかわります。
「k」を押下で形が変わります。
円のサイズは、シフト+マウスのホィールで横方向変わります
縦方向は、ALT+マウスのホィールです。
mkDoc.pyのソース(コピーすれば、動作しますpython2.7)
kao.gifを用意してください(中身は、好きな画像を)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tkinter
import pyautogui
import mouse
import keyboard
import threading
import time,os
import datetime
import ctypes
from ctypes import sizeof
from ctypes.wintypes import RECT
import win32gui
from PIL import ImageGrab
from PIL import Image, ImageTk
TARGET_NAME = 'Python Debugger (NetPdb)'
TARGET_NAME = 'Yahoo! JAPAN'
def worker(title,close_until_seconds):
time.sleep(close_until_seconds)
wd=ctypes.windll.user32.FindWindowA(0,title)
ctypes.windll.user32.SendMessageA(wd,0x0010,0,0)
return
def AutoCloseMessageBoxW(text, title, close_until_seconds):
t = threading.Thread(target=worker,args=(title,close_until_seconds))
t.start()
ctypes.windll.user32.MessageBoxA(0, text, title, 0)
handle = None
text = ''
root = tkinter.Tk()
root.title("2")
root.geometry('300x600+10+10')
root2 = tkinter.Toplevel()
root2.wm_attributes("-topmost", True)
root2.configure(bg='white')
root2.wm_attributes("-transparentcolor", "white")
root2.overrideredirect(True)
canvas0 =""
fileIMG123 =""
def callback():
global canvas0, fileIMG123
return True
print("callbac kitayo")
img = Image.open( '1215_100.png' )
img2= ImageTk.PhotoImage(img)
canvas0.itemconfig( fileIMG123, image=img2, anchor=tkinter.NW )
return True
entry1 = tkinter.Entry(root, text="", validate="all", validatecommand=callback)
entry1.place(x=10,y=10,width=200)
text_widget = tkinter.Text(root)
text_widget.place(x=10,y=400)
def mouse_callback():
global handle
global text
handle = win32gui.GetForegroundWindow()
win32gui.SetForegroundWindow( handle )
text = win32gui.GetWindowText(handle)
titlebar = win32gui.GetWindowText( handle )
#classname = win32gui.GetClassName( handle )
print('撮りたいウィンドウをクリック')
ctypes.windll.user32.MessageBoxA(0, '撮りたいウィンドウをクリック', "", 0)
mouse.on_click(mouse_callback)
while not text:
mouse.wait(button='left', target_types=('up',))
print('このウィンドウを撮る: ' + text)
print('違うなら再起動')
#AutoCloseMessageBoxW( '', 'このウィンドウを撮る\n\n' + text, 0 )
time.sleep(1) # この隙に移したいウィンドウを最前面に持ってくる
#ウインドウの完全な情報を取ってくる、FindWindowで部分一致だったりした場合の補完用
win_x1,win_y1,win_x2,win_y2 = win32gui.GetWindowRect(handle)
###apw_x= win_x2 - win_x1
###apw_y= win_y2 - win_y1
rect = ( win_x1 +2, win_y1 -2, win_x2 +0, win_y2 +0 )
print rect
AutoCloseMessageBoxW( 'このウィンドウを撮る\r' + text, '', 1 )
text_widget.insert( 'end', u'This window is %s\n' %text )
lw =1000
lh =800
ctrlSW = 0
enX = 100
enY = 50
enShp = 0
sftSW = 0
altSW = 0
enColList = ["red","yellow","blue" ]
enColIx = 0
enCol = enColList[ enColIx ]
"""
"""
canvas0 = tkinter.Canvas(root, width=300, height=200, bg="green")
canvas0.place(x=5, y=30)
sf=0.5
canvas0.scale("all", 0, 0, sf, sf)
#canvas0.scale("all",0,0,2,2)
canvas = tkinter.Canvas(root2, width=lw, height=lh, bg="white")
canvas.place(x=-2, y=-2)
def dspEn( col ="white" ):
#def dspEn( col ):
if enShp == 0:
canvas.create_oval(10, 10, enX, enY, outline= col, width="10")
elif enShp == 1:
canvas.create_rectangle(10, 10, enX, enY, outline= col, width="10")
elif enShp == 2:
canvas.create_line(10, 10, enX, enY, fill= col, width="10")
def clrEn():
dspEn()
def getFileNo( kensaku ):
path = "./"
files = os.listdir(path)
#kensaku = '1224_'
kensaku += '_'
newlist = ['0']
for l in files:
if kensaku in l:
newlist.append(l.replace( kensaku,"").replace(".png","") )
print newlist
print max(newlist)
return 1 + int( max(newlist) )
def print_screen():
global sftSW, enCol, enColIx, enShp, ctrlSW, altSW
global canvas0
ymd = datetime.datetime.now().strftime('%m%d')
no = getFileNo( ymd )
fname = "%s_%02d.png" %( ymd, no )
print( "print screen", fname )
clrEn()
ctrlSW = 0
ImageGrab.grab(bbox=rect).save( fname )
AutoCloseMessageBoxW( '%s\rScreenCap OK' %fname, 'print screen', 1 )
# 「描く」ボタン
def draw(event):
""" Click Event
"""
print("draw")
global imgData,canvas0 # <- グローバル参照を利用する
img = Image.open( '1215_100.png' )
imgData= ImageTk.PhotoImage(img )
canvas0.scale("all", 0, 0, 0.5, 0.5)
event.widget.itemconfig("fileIMG", image=imgData, anchor=tkinter.NW)
def print_screen2():
global sftSW, enCol, enColIx, enShp, ctrlSW, altSW
global canvas0,fileIMG123,root,imgData99
ymd = datetime.datetime.now().strftime('%m%d')
no = getFileNo( ymd )
fname = "%s_%02d.png" %( ymd, no )
text_widget.insert( 'end', '%s\rScreenCap OK\n' %fname )
entry1.insert("end", '%s\rScreenCap OK\n' %fname)
print( "print screen", fname )
img = ImageGrab.grab(bbox=rect)
img.save( fname )
img = Image.open( fname )
img = img.resize((600, 400))
imgData99 = ImageTk.PhotoImage(img)
canvas0.itemconfig( "fileIMG", image=imgData99, anchor=tkinter.NW )
#canvas0.create_text(70, 30, text="old text")
#canvas0.delete( fileIMG123 )
#canvas0.create_image(40, 40, image=img)
#clrEn()
ctrlSW = 0
return
def keyCallback( event ):
global sftSW, enCol, enColIx, enShp, ctrlSW, altSW
print( event.name, event.event_type )
if event.name == "print screen" and event.event_type == "up":
print_screen()
if event.name == "shift":
if event.event_type == "down": sftSW = 1
else: sftSW = 0
if event.name == "alt":
if event.event_type == "down": altSW = 1
else: altSW = 0
#print( "altSW=", altSW )
if sftSW == 1 and event.name == "ctrl" and event.event_type == "up":
print( "ctrlSW-dayo" )
if ctrlSW == 1:
ctrlSW = 0
clrEn()
else:
ctrlSW = 1
dspEn( enCol )
if ctrlSW == 0:
return
if event.name == "i" and event.event_type == "up":
#print( "enCol-dayo" )
if enColIx == len( enColList )-1:
enColIx = 0
else:
enColIx += 1
enCol = enColList[ enColIx ]
clrEn()
dspEn( enCol )
# Change Shape
if event.name == "k" and event.event_type == "up":
clrEn()
if enShp == 2:
enShp = 0
else:
enShp += 1
dspEn( enCol )
def mouseCallback( event ):
global enX,enY
"""
if type( event ) == mouse.ButtonEvent:
dspEn( enCol )
print_screen()
"""
if ctrlSW == 0:
return
print('mouseCallback-Event:', event, type(event))
if ( sftSW == 1 or altSW == 1) and type( event ) == mouse.WheelEvent:
#print("aaaaaaaa",event.time, event.delta)
#円を描く
dspEn()
if sftSW == 1 : enX += 25* event.delta
if altSW == 1 : enY += 25* event.delta
if ctrlSW == 1:
dspEn( enCol )
def left_mouse(event,e):
print('Position:', mouse.get_position())
print('Event:', event)
def timer():
px,py = pyautogui.position()
#print( '{},{}'.format(px,py) )
px -= (enX // 2)+5
py -= (enY // 2)+5
root2.geometry(str(lw)+"x"+str(lh)+"+"+str(px)+"+"+str(py) )
root2.after(10, timer)
canvas.create_oval(10, 10, enX, enY, outline="white", width="10")
if sftSW == 1:
canvas.create_oval(10, 10, enX, enY, outline= enCol, width="10")
shortcut = "shift+esc"
keyboard.add_hotkey(shortcut, print_screen2, suppress=True)
timer()
#mouse.on_click(left_mouse, args=('mouse_left', 'mouse_up'))
mouse.hook( mouseCallback )
keyboard.hook( keyCallback )
#dspEn("red")
img = Image.open( 'kao.gif' )
img = img.resize((600, 400))
imgData = ImageTk.PhotoImage(img)
fileIMG123 = canvas0.create_image(0, 0, image=imgData, anchor=tkinter.NW, tag="fileIMG")
canvas0.bind('<1>', draw)
root.mainloop()