神奇的python自动绘画

654 阅读1分钟

python 自动化绘图

使用python自动化库 clicknium ,自动化操作鼠标键盘绘制像素风格的logo。该库提供了完整的自动化功能,支持桌面软件、web页面、java应用、sap应用、甚至是图像识别,而且使用起来非常直观方便,只要你会python,那么基本上手没有任何障碍,详情可以参考官方文档。使用python 自动化来实现一个有趣的绘图过程。

步骤

  1. 环境准备:系统 >win7 、vscode、python
  2. 安装 clicknium vscode扩展
  3. pip install clicknium
  4. pip install opencv-python
  5. ctrl+shift+p ,选择 Clicknium:Sample, 选择目录,创建sample项目,删除main中代码
  6. 录制流程中需要用到的控件,编写自动化流程python代码
  7. F5 运行sample.py

录制元素

使用Ctrl+F10 唤起录制器,录制需要使用的元素如下(做好分类和命名,方便阅读):

locator.png

流程结构和部分代码

# open and init mspaint
open_mspaint()
set_client_size()
set_backgroun_color()
...
# translate image to a binarization image by python module opencv
binarization(sourceImgPath,binarizationSavePath)
...

# get start position of mspaint client area
rect = ui(locator.mspaint.pane_clientarea).get_property('BoundingRectangle')
lsRect = rect.split('{')[1].partition('}')[0].split(',')
clientRectX = int(lsRect[0].split('=')[1])
clientRectY = int(lsRect[1].split('=')[1])
...
# read image and get RGB of each point
img = read_picture(binarizationSavePath)
...
# control the mouse and draw on mspaint
draw(dict_x_y,dict_y_x)

源码

mspaint-draw-img

效果

result.gif