Created
April 12, 2025 10:09
-
-
Save AlexanderProd/31c4928ede1f0c3718fab8a7e36ccad9 to your computer and use it in GitHub Desktop.
Print an image directly on windows and fill out the page, without borders, if supported.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import win32ui, win32con | |
from PIL import Image, ImageWin | |
import win32print | |
def get_printer_by_name(printer_name): | |
"""Get a printer by its name. Returns None if printer not found.""" | |
try: | |
# Open the printer | |
hprinter = win32print.OpenPrinter(printer_name) | |
# Get printer info | |
printer_info = win32print.GetPrinter(hprinter, 2) | |
# Close the printer | |
win32print.ClosePrinter(hprinter) | |
return printer_info | |
except Exception as e: | |
print(f"Error getting printer '{printer_name}': {str(e)}") | |
return None | |
def list_available_printers(): | |
"""List all available printers.""" | |
printers = win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL | win32print.PRINTER_ENUM_CONNECTIONS) | |
return [printer[2] for printer in printers] # printer[2] contains the printer name | |
def print_test(printer_name): | |
try: | |
filename = "F:\\Landscape\\Südtirol 21. August 2016 - 045.jpg" | |
img = Image.open(filename, 'r') | |
except: | |
print("error") | |
return | |
# Open printer and get printer info | |
hprinter = win32print.OpenPrinter(printer_name) | |
printer_info = win32print.GetPrinter(hprinter, 2) | |
win32print.ClosePrinter(hprinter) | |
# Create DC and set up printer | |
hdc = win32ui.CreateDC() | |
hdc.CreatePrinterDC(printer_name) | |
# Enable borderless printing by setting margins to 0 | |
hdc.SetMapMode(win32con.MM_ISOTROPIC) | |
hdc.SetViewportExt((hdc.GetDeviceCaps(win32con.HORZRES), hdc.GetDeviceCaps(win32con.VERTRES))) | |
hdc.SetWindowExt((hdc.GetDeviceCaps(win32con.HORZRES), hdc.GetDeviceCaps(win32con.VERTRES))) | |
hdc.SetWindowOrg((0, 0)) # Set origin to (0,0) for borderless printing | |
horzres = hdc.GetDeviceCaps(win32con.HORZRES) | |
vertres = hdc.GetDeviceCaps(win32con.VERTRES) | |
landscape = horzres > vertres | |
if landscape: | |
if img.size[1] > img.size[0]: | |
print('Landscape mode, tall image, rotate bitmap.') | |
img = img.rotate(90, expand=True) | |
else: | |
if img.size[1] < img.size[0]: | |
print('Portrait mode, wide image, rotate bitmap.') | |
img = img.rotate(90, expand=True) | |
img_width = img.size[0] | |
img_height = img.size[1] | |
if landscape: | |
#we want image width to match page width | |
ratio = vertres / horzres | |
max_width = img_width | |
max_height = (int)(img_width * ratio) | |
else: | |
#we want image height to match page height | |
ratio = horzres / vertres | |
max_height = img_height | |
max_width = (int)(max_height * ratio) | |
# Calculate scaling to fit the page while maintaining aspect ratio | |
scale_x = horzres / img_width | |
scale_y = vertres / img_height | |
scale = min(scale_x, scale_y) | |
new_width = int(img_width * scale) | |
new_height = int(img_height * scale) | |
# Center the image on the page | |
x = (horzres - new_width) // 2 | |
y = (vertres - new_height) // 2 | |
hdc.StartDoc('Result') | |
hdc.StartPage() | |
dib = ImageWin.Dib(img) | |
dib.draw(hdc.GetHandleOutput(), (x, y, x + new_width, y + new_height)) | |
hdc.EndPage() | |
hdc.EndDoc() | |
hdc.DeleteDC() | |
print( 'Debug info:' ) | |
print( 'Landscape: %d' % landscape ) | |
print( 'horzres: %d' % horzres ) | |
print( 'vertres: %d' % vertres ) | |
print( 'img_width: %d' % img_width ) | |
print( 'img_height: %d' % img_height ) | |
print( 'new_width: %d' % new_width ) | |
print( 'new_height: %d' % new_height ) | |
print( 'x: %d' % x ) | |
print( 'y: %d' % y ) | |
if __name__ == "__main__": | |
# List all available printers | |
available_printers = list_available_printers() | |
print("Available printers:") | |
for printer in available_printers: | |
print(f" - {printer}") | |
# Get default printer | |
default_printer = win32print.GetDefaultPrinter() | |
print(f"\nDefault printer: {default_printer}") | |
# Example of getting a specific printer by name | |
# You can replace "Microsoft Print to PDF" with any printer name from the list above | |
specific_printer = "Canon SELPHY CP1500" | |
printer_info = get_printer_by_name(specific_printer) | |
if printer_info: | |
print(f"\nSuccessfully found printer: {specific_printer}") | |
print_test(specific_printer) | |
else: | |
print(f"\nCould not find printer: {specific_printer}") | |
# Fall back to default printer | |
print(f"Using default printer instead: {default_printer}") | |
print_test(default_printer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment