LoginSignup
1
2

More than 1 year has passed since last update.

PyAutoGUIでお絵描き

Last updated at Posted at 2021-06-12

ペイントで正弦波を描きます。

paint1.gif

曲線は折れ線(直線ツール)でなく鉛筆ツールで描いています。
ペイントで折れ線を描くのは難しいので。

ソース(座標軸部分は省略)

from ctypes import *
import math
import numpy as np
import pyautogui

FindWindowEx = windll.user32.FindWindowExW
SetForegroundWindow = windll.user32.SetForegroundWindow

hwnd = FindWindowEx(None, None, None, "無題 - ペイント")
if hwnd == 0:
    raise Exception

# ペイントを前面に出す
SetForegroundWindow(hwnd)

# 鉛筆ツール
pyautogui.moveTo(500, 160) # アイコンの座標(PCに依存)
pyautogui.click()

# 原点(キャンバス上の適当な点)
[x0, y0] = [400, 400]
pyautogui.moveTo(x0, y0)

R = 80
for t in np.arange(0.0, 2.1*math.pi, 0.1):
    [x, y] = [x0 + 40*t, y0 - R*math.sin(t)]
    pyautogui.dragTo(x, y)

こんなことも。

paint2.gif

こういうシーケンス図を生成できるツールがないかと思ってるんだけど、さすがにこれは無理がある...

きっかけ

今年のMATLAB EXPOのライトニングトーク MATLABとArduinoのRPA活用

ターゲット(Windows XPなどの古いPC)のマウス入力を、MATLABとArduinoでエミュレートして自動化するというもので、ターゲット上のペイントでお絵描きをしていました。
それが面白かったので、PyAutoGUIでやってみようかなと。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2