如何使用Python现有包制作按键可独立上色的键盘图形
实现自定义按键颜色的键盘图像
方案1:基于keyboardlayout包修改
keyboardlayout底层依赖Pillow绘图,我们可以通过重写Key类的绘制逻辑来支持自定义按键颜色,步骤如下:
- 安装依赖
pip install keyboardlayout pillow
- 自定义带颜色支持的按键并渲染
from keyboardlayout import Layout, Key from PIL import ImageDraw, Image # 定义按键-颜色映射(键名对应RGB颜色值) key_colors = { "Q": (255, 0, 0), "W": (0, 255, 0), "E": (0, 0, 255), "A": (255, 255, 0), "S": (255, 0, 255), "D": (0, 255, 255), # 可继续添加其他按键的颜色 } # 重写Key类,新增颜色参数并修改绘制逻辑 class ColoredKey(Key): def __init__(self, *args, color=(200, 200, 200), **kwargs): super().__init__(*args, **kwargs) self.color = color def draw(self, draw: ImageDraw.ImageDraw) -> None: # 绘制按键背景(用自定义颜色) draw.rectangle(self.rect, fill=self.color, outline=(0,0,0), width=2) # 绘制按键文本 draw.text(self.center, self.label, fill=(0,0,0), font=self.font, anchor="mm") # 创建布局时指定自定义Key类 layout = Layout( layout_name="ansi", # 可选布局:ansi, iso, etc. key_class=ColoredKey, key_size=(60, 60), # 调整按键大小 key_spacing=(5, 5) # 调整按键间距 ) # 为每个按键分配颜色(未指定的用默认灰色) for key in layout.keys: key.color = key_colors.get(key.label, (200, 200, 200)) # 渲染并保存图像 img = layout.render() img.save("colored_keyboard.png") img.show()
方案2:用Pillow完全自定义绘制
如果需要更灵活的布局控制,直接用Pillow手动绘制键盘,无需依赖keyboardlayout:
from PIL import Image, ImageDraw, ImageFont # 键盘基础参数 key_width = 60 key_height = 60 spacing = 5 margin = 20 # 定义键盘布局(可按需修改) keyboard_layout = [ ["Esc", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"], ["~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace"], ["Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\"], ["Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter"], ["Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "Shift"], ["Ctrl", "Win", "Alt", "Space", "Alt", "Win", "Menu", "Ctrl"] ] # 按键颜色映射 key_colors = { "Q": (255, 0, 0), "W": (0, 255, 0), "E": (0, 0, 255), "A": (255, 255, 0), "S": (255, 0, 255), "D": (0, 255, 255) } # 计算图像尺寸 max_cols = max(len(row) for row in keyboard_layout) img_width = margin * 2 + max_cols * key_width + (max_cols - 1) * spacing img_height = margin * 2 + len(keyboard_layout) * key_height + (len(keyboard_layout) - 1) * spacing # 创建空白图像 img = Image.new("RGB", (img_width, img_height), (255, 255, 255)) draw = ImageDraw.Draw(img) # 替换为你系统中存在的字体路径 font = ImageFont.truetype("arial.ttf", 20) # 遍历绘制每个按键 y = margin for row in keyboard_layout: x = margin for key_label in row: # 计算按键矩形区域 rect = (x, y, x + key_width, y + key_height) # 获取颜色(默认灰色) color = key_colors.get(key_label, (200, 200, 200)) # 绘制按键背景 draw.rectangle(rect, fill=color, outline=(0,0,0), width=2) # 绘制居中按键文本 text_bbox = draw.textbbox((0,0), key_label, font=font) text_x = x + (key_width - (text_bbox[2]-text_bbox[0])) // 2 text_y = y + (key_height - (text_bbox[3]-text_bbox[1])) // 2 draw.text((text_x, text_y), key_label, fill=(0,0,0), font=font) # 更新x坐标 x += key_width + spacing # 更新y坐标 y += key_height + spacing # 保存并显示图像 img.save("custom_colored_keyboard.png") img.show()
内容的提问来源于stack exchange,提问作者ranlot
相关产品推荐
相关产品推荐

