Today a funny trick using Python that allow you to control the mouse movements (move, click, drag, recognize RGB and much more…).
Configure the environment
I assume that python is installed on your local machine, you know virtualenv and pip.
I want to introduce :
Python AutoGUI is a native library used to control the mouse in every environments (Windows, Linux, macOS).
Install it using pip if needed:
$ pip install pyautogui
Start with a simple example:
(env) [~/Desktop/PythonEXL/env]$ python Python 3.8.5 (default, Aug 20 2020, 11:17:08) [Clang 11.0.3 (clang-118.104.22.168)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import pyautogui >>> pyautogui.position() Point(x=-741, y=953)
We have now the mouse position in the screen. (In my case the value is negative, due to multiple monitors installed).
You can read more here: https://pyautogui.readthedocs.io/en/latest/quickstart.html.
Move the mouse
You can use the moveTo or moveRel where the first one move the mouse to a position, the second one move the mouse pointer relatively starting from your current position.
>>> import pyautogui >>> pyautogui.moveTo(100, 100) >>> pyautogui.moveTo(100, 100, duration=1.5) >>> pyautogui.moveRel(200, 0) >>> pyautogui.moveRel(200, 0, duration=2)
The duration is a sort of animation between transitions.
You can click using left, middle and right button. Single click, multiple clicks. Drag.
>>> import pyautogui >>> pyautogui.click() >>> pyautogui.position() Point(x=-824, y=417) >>> pyautogui.click(-824,417) >>> pyautogui.doubleClick() >>> pyautogui.rightClick() >>> pyautogui.middleClick() >>> pyautogui.dragRel(0, 100, duration=100, button='left'
Photoshop drawing using Python
This is the final scope of my article, how to automatically draw complex shapes in photoshop using math and trigonometric formulae.
Of course this example is a funny one, but I’ve achieved that can be done. Now I need to review the trigonometry exams.
A useful method you can use to decide the color of your shapes is:
that print the current mouse position in real-time al also the RGB color founded below the cursor pointer:
>>> pyautogui.displayMousePosition() Press Ctrl-C to quit. X: -1196 Y: 823 RGB: (128, 100, 200) >>>
Draw a 3D pipe, more-or-less
In a completely unmanaged and random way…
Here the code to take inspiration and make something better than this:
import pyautogui import math import random distance = 200 delta = 5 button='left' delay=0.1 pyautogui.click() # click on photoshop for docus while distance > 0: pyautogui.dragRel(distance, random.randint(0, 10), duration=delay, button=button) distance = distance - delta pyautogui.dragRel(random.randint(0, 10), distance, duration=delay, button=button) pyautogui.dragRel(-distance, random.randint(0, 10), duration=delay, button=button) distance = distance - delta pyautogui.dragRel(random.randint(0, 10), -distance, duration=delay, button=button)
Enjoy code drawing!