Skip to content
Notifications You must be signed in to change notification settings

RetrogradeDev/pico-painter

Repository files navigation

Pico Ducky - Automated Image Drawing

This project converts an image into contour points and uses a Raspberry Pi Pico as a USB HID (Human Interface Device) to automatically draw the image in any paint application like MS Paint.

Screen.Recording.2025-10-16.120808.online-video-cutter.com.mp4

How It Works

The project consists of two main components:

  1. Image Processing (extract_contours.py): Converts an image into contour points using OpenCV
  2. Drawing Automation (code.py): Runs on Raspberry Pi Pico to simulate mouse movements and draw the contours

Requirements

For Image Processing (PC)

  • Python 3.x
  • OpenCV (opencv-python)

For Raspberry Pi Pico

  • Raspberry Pi Pico or Pico W
  • CircuitPython firmware
  • Adafruit HID library
  • USB cable to connect Pico to computer

Installation

Step 1: Install Python Dependencies (PC)

Install OpenCV for image processing:

pip install opencv-python

Step 2: Set Up Raspberry Pi Pico

  1. Install CircuitPython on your Pico:

    • Download CircuitPython from circuitpython.org
    • Hold the BOOTSEL button while plugging in the Pico to enter bootloader mode
    • Drag and drop the CircuitPython .uf2 file onto the RPI-RP2 drive
    • The Pico will reboot and appear as a CIRCUITPY drive
  2. Install Adafruit HID Library:

    • Download the Adafruit HID library from the CircuitPython Bundle
    • Make sure you download a version mathing the CircuitPython SDK version.
    • Copy the adafruit_hid folder to the lib folder on your CIRCUITPY drive
    • The required library files are already included in the lib folder of this repository
  3. Copy Files to Pico:

    • Copy code.py, data.py, and the lib folder to the root of your CIRCUITPY drive
    • If using a French keyboard layout, the keycode_win_fr.mpy and keyboard_layout_win_fr.mpy files are included
    • For other keyboard layouts, modify the import in code.py

Usage

Creating Contour Data from an Image

  1. Place your image file (PNG or JPG) in the project directory as image.png

  2. Run the contour extraction script:

python extract_contours.py
  1. This will:

    • Resize the image to 300x300 pixels
    • Convert it to grayscale
    • Apply binary thresholding
    • Extract contours
    • Save the contour points to data.py
    • Display a preview of detected contours
  2. Adjust threshold if needed:

    • Open extract_contours.py and modify the threshold_value parameter (0-255)
    • Lower values detect more details, higher values detect only prominent features
    • Default is 127

Running the Drawing Automation

  1. Safety Notice: The code.py file has a safety exit() line commented at line 15. Review the code before removing this safety feature.

  2. Open the code.py file and remove or comment out the safety exit:

    #exit() # For safety, remove this line to run the script
  3. Save code.py to your Pico. The script will automatically run when the Pico boots.

  4. What the script does:

    • Opens the Run dialog (Win+R) on Windows
    • Types "paint.net" to open Paint.NET (you can modify this for MS Paint or other apps)
    • Maximizes the window
    • Moves the mouse to the starting position
    • Draws all contours by:
      • Moving the mouse to each point in the contour
      • Pressing and holding the left mouse button
      • Following the contour points
      • Releasing the mouse button when the contour is complete
  5. Customization:

    • Modify the scale variable in code.py (line 61) to adjust drawing size
    • Change the keyboard commands to open different paint applications
    • Adjust time.sleep() values to change drawing speed

How the Drawing Works

Image to Contours (extract_contours.py)

  1. Load and Resize: Image is loaded and resized to 300x300 pixels
  2. Grayscale Conversion: Color image is converted to grayscale
  3. Binary Thresholding: Pixels above threshold become white, below become black
  4. Contour Detection: OpenCV finds the outlines of white regions
  5. Data Export: Contour points are saved as Python lists in data.py

Automated Drawing (code.py)

  1. Initialization: Sets up USB HID keyboard and mouse
  2. Application Launch: Opens paint application using keyboard shortcuts
  3. Window Setup: Maximizes the window and positions mouse
  4. Drawing Loop: For each contour:
    • Moves mouse to first point
    • Presses left mouse button
    • Moves through all points in the contour
    • Releases mouse button
    • Moves to next contour

The mouse movement is smoothed by breaking large movements into smaller steps to ensure accuracy.

Troubleshooting

Image Processing Issues

Problem: No contours detected

  • Solution: Adjust the threshold_value in extract_contours.py. Try values between 50-200.

Problem: Too many small contours

  • Solution: Increase the threshold value or pre-process your image to have clearer edges

Raspberry Pi Pico Issues

Problem: Pico not recognized as CIRCUITPY drive

  • Solution: Reinstall CircuitPython firmware. Ensure you're using the correct version for your Pico model.

Problem: Import errors for adafruit_hid

  • Solution: Ensure the adafruit_hid folder is in the lib directory on your Pico

Problem: Wrong keyboard layout

  • Solution: Modify line 4 in code.py to import the correct keycode module for your keyboard layout

Problem: Drawing is too small/large

  • Solution: Adjust the scale variable in code.py (line 61)

Problem: Drawing is too fast/slow

  • Solution: Adjust the time.sleep() values in code.py to control movement speed

Problem: Paint application doesn't open

  • Solution: Modify the keyboard commands in code.py to match your system:
    • For MS Paint (Windows): Replace "PAINT.NET" with "MSPAINT"
    • For other applications: Adjust the key sequence accordingly

General Tips

  • Start with simple images (silhouettes, logos) before trying complex ones
  • Ensure the paint application is the active window before drawing starts
  • The 5-second delay (line 39) gives you time to prepare the canvas
  • Test with the safety exit() enabled first to verify keyboard commands work

Keyboard Layout Support

The project currently uses French keyboard layout (keycode_win_fr). To use a different layout:

  1. Download the appropriate keyboard layout
  2. Place it in the lib folder
  3. Update line 4 in code.py:
    from keycode_win_us import Keycode  # For US keyboard

License

This project is open source. Feel free to modify and use it for your own projects.

Contributing

Contributions are welcome! Feel free to submit issues or pull requests to improve the project.

Credits

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages