Windows Print Image
Print an image file to a specified printer on Windows. Uses Python subprocess + mspaint /pt to avoid bash/git-bash Chinese path encoding problems.
Trigger
- User asks to print an image/file
- User mentions printing something on Windows
Steps
1. Confirm file exists
python -c "import os; print(os.path.exists(r'<absolute_path>'))"
2. List available printers (optional)
powershell -Command "Get-Printer | Select-Object Name, PrinterStatus | Format-Table -AutoSize"
Check default printer:
powershell -Command "(Get-WmiObject -Query 'SELECT * FROM Win32_Printer WHERE Default=True').Name"
3. Print the image
python -c "import subprocess; img = r'<absolute_path>'; prn = '<printer_name>'; subprocess.run(['mspaint', '/pt', img, prn])"
Critical: Use Python raw strings (r'...') for paths — this is what makes it work! Bash's cmd /c mspaint /pt fails with Chinese paths due to encoding corruption, but Python's raw strings preserve the exact bytes.
Example
python -c "import subprocess; img = r'E:\私域相关资料\南京泊宁资料\8888.jpg'; prn = 'Canon G3000 series Printer (副本 1)'; subprocess.run(['mspaint', '/pt', img, prn])"
Pitfalls
- Do NOT use
cmd /c mspaint /porcmd /c mspaint /ptfrom bash — Chinese path encoding gets mangled. - Do NOT use
powershell Start-Process -Verb Print— it pops up a dialog requiring manual confirmation. - Always use Python
r'...'raw strings for paths. mspaint /pttakes:mspaint /pt <filename> <printername>— just 2 params is enough, no need for driver/port.
Verification
returncode: 0= print job sent successfully.- Check the printer's physical output tray.