【教学类-120-03】20251029十个数字横排1*10切割,5-35的边距,切割10次,手工挑选
背景需求
之前做了2*5的数字,但等分切割,发现很难等切。

https://blog.csdn.net/reasonsummer/article/details/153980463?spm=1011.2415.3001.5331
https://blog.csdn.net/reasonsummer/article/details/153980463?spm=1011.2415.3001.5331
换思路,图片做成0-9横版的样式
简笔画,白色背景,数字0-9,空心,没有颜色,排成一排,正面图,


'''
20250802通义万相2.2下载通义照片 copy
Python下载通义万相的图片(存在问题,不能停止,只能默认下载300张,删除多余)
星火讯飞,阿夏
谷歌页面打开通义万相,页面放大到200%
20251003
'''import os,time
import pyautogui
import pyperclip
import re
import win32apiimport win32con
import sys
import ctypesimport timename='20251028十个数字横排'
# 先打开微信num=1
zs=50
# 实际157def minimize_active_window():try:if sys.platform == 'win32':# 获取当前活动窗口的句柄hwnd = ctypes.windll.user32.GetForegroundWindow()# 最小化窗口ctypes.windll.user32.ShowWindow(hwnd, 6) # 6 对应 SW_MINIMIZEreturn Trueelse:print("此功能仅支持Windows系统")return Falseexcept Exception as e:print(f"最小化窗口时出错: {e}")return Falseprint("程序运行中...")
time.sleep(2) # 等待2秒,让你有时间切换到VS Code窗口# 尝试最小化活动窗口
if minimize_active_window():print("窗口已最小化")
else:print("无法最小化窗口")# 读取文件名称和路径path=fr'D:\{name}'os.makedirs(path,exist_ok=True)for i in range(num,num+zs):# 下载按钮 pyautogui.moveTo(1569, 302)pyautogui.click() time.sleep(1)# 点击有,无水印要包月 pyautogui.moveTo(1573, 373)pyautogui.click() time.sleep(2)# 输入图片名称,复制中文内容到剪贴板name=path+fr'\{i:03}'pyperclip.copy(name)# 黏贴图片地址pyautogui.hotkey('ctrl', 'v')time.sleep(1)pyautogui.press('enter')
# # 图片显示需要时间time.sleep(1)# 模拟按键“右箭头”pyautogui.moveTo (989, 628)pyautogui.click() time.sleep(2)# 'left'(左箭头)# 'up'(上箭头)# 'down'(下箭头)import sys
import ctypes
import timedef minimize_active_window():try:if sys.platform == 'win32':# 获取当前活动窗口的句柄hwnd = ctypes.windll.user32.GetForegroundWindow()# 最小化窗口ctypes.windll.user32.ShowWindow(hwnd, 6) # 6 对应 SW_MINIMIZEreturn Trueelse:print("此功能仅支持Windows系统")return Falseexcept Exception as e:print(f"最小化窗口时出错: {e}")return Falseprint("程序运行中...")
time.sleep(2) # 等待2秒,让你有时间切换到VS Code窗口# 尝试最小化活动窗口
if minimize_active_window():print("窗口已最小化")
else:print("无法最小化窗口")
![]()
、
删除不要的

'''
十个数字横版 图片处理,转黑白色,切边,统一大小(背景一样大,图像拉伸撑满)
Deepseek,阿夏
20251028
'''
import os
from PIL import Image
import numpy as np# 全局配置参数
CONFIG = {'margin': 20, # 裁剪边距'bw_threshold': 128, # 黑白化阈值(0-255)'transparency_tolerance': 30, # 透明化容差'target_size': (1000, 200) # 精确拉伸到400×300
}def convert_to_pure_bw(image_path, output_path, threshold=128):"""将图片转为纯黑白两色(黑色0,0,0和白色255,255,255)参数:image_path: 输入图片路径output_path: 输出图片路径threshold: 二值化阈值"""try:img = Image.open(image_path)img = img.convert('L') # 转为灰度图# 二值化处理binary_img = img.point(lambda x: 0 if x < threshold else 255, '1')binary_img = binary_img.convert('RGB') # 转回RGB模式# 保存黑白图片binary_img.save(output_path)print(f"黑白图生成: {os.path.basename(image_path)}")return output_pathexcept Exception as e:print(f"处理图片 {image_path} 时出错: {str(e)}")return Nonedef crop_transparent_edges(image):"""裁剪掉图片的透明边缘,并保留指定的间距参数:image: PIL Image对象(RGBA模式)返回:裁剪后的PIL Image对象"""# 转换为numpy数组data = np.array(image)# 获取alpha通道alpha = data[:, :, 3]# 找到非透明像素的位置non_transparent = np.where(alpha > 0)if len(non_transparent[0]) == 0:# 如果全是透明像素,返回原图return image# 获取非透明区域的边界top = np.min(non_transparent[0])bottom = np.max(non_transparent[0])left = np.min(non_transparent[1])right = np.max(non_transparent[1])# 添加间距margin = CONFIG['margin']top = max(0, top - margin)bottom = min(image.height - 1, bottom + margin)left = max(0, left - margin)right = min(image.width - 1, right + margin)# 裁剪图片cropped_image = image.crop((left, top, right + 1, bottom + 1))return cropped_imagedef make_background_transparent(bw_image_path, output_path):"""将黑白图片的白色背景变为透明并裁剪透明边缘参数:bw_image_path: 黑白图片路径output_path: 输出图片路径"""# 打开黑白图片并转换为RGBA模式with Image.open(bw_image_path) as img:# 转换为RGBA模式img = img.convert('RGBA')# 获取图片数据data = np.array(img)red, green, blue, alpha = data.T# 创建白色背景掩码:判断像素是否为白色(在容差范围内)tolerance = CONFIG['transparency_tolerance']white_mask = ((red >= 255 - tolerance) & (red <= 255) &(green >= 255 - tolerance) & (green <= 255) &(blue >= 255 - tolerance) & (blue <= 255))# 将白色背景像素的alpha通道设为0(透明)data[white_mask.T] = (255, 255, 255, 0)# 转换回Imageresult = Image.fromarray(data)# 裁剪透明边缘并保留间距cropped_result = crop_transparent_edges(result)# 保存结果cropped_result.save(output_path, 'PNG')def resize_to_exact_size(image_path, output_path):"""将图片拉伸到精确的400×300尺寸参数:image_path: 输入图片路径output_path: 输出图片路径"""with Image.open(image_path) as img:if img.mode != 'RGBA':img = img.convert('RGBA')# 直接拉伸到目标尺寸target_size = CONFIG['target_size']resized_img = img.resize(target_size, Image.Resampling.LANCZOS)# 保存结果resized_img.save(output_path, 'PNG')def process_single_image(image_path, output_path):"""完整处理单张图片:黑白化 → 透明化 → 精确拉伸到400×300参数:image_path: 输入图片路径output_path: 输出图片路径"""try:# 步骤1: 转为黑白图片(临时文件)temp_bw_path = output_path.replace('.png', '_bw_temp.png')bw_path = convert_to_pure_bw(image_path, temp_bw_path, CONFIG['bw_threshold'])if not bw_path:return False# 步骤2: 背景透明化(临时文件)temp_transparent_path = output_path.replace('.png', '_trans_t
