wordpress 超时seo新方法
老板给了张没有atlas文件的图集让我拆图,简单写了一版凑合用。
存在的问题:
- 可能会拆出来一些小尺寸的透明像素图片;
- 可能会拆出来一些偏大的小图的整体集合;
- 可能会把应该是一块的小图批成两半,不过不多;
- 其他未知的问题...
小图目测基本齐全,没用的图...手删一下趴
有改正的同学烦请告知,比个♥
from PIL import Image
import numpy as np
import timedef split_image_into_blocks(image_path, threshold=0): img = Image.open(image_path) if img.mode != 'RGBA': img = img.convert('RGBA') # 转换到RGBA模式以获取alpha通道 img_data = np.array(img) width, height = img.size output_index = 0col_start_index = np.zeros(height, dtype=int) # 弃用,有问题,应该划分块regions = []def detect_bounding_box(row, col):# 初始化包围盒 min_row, max_row = row, row min_col, max_col = col, col if min_row == height:return min_row, min_col, max_row, max_coldef extern_bottom_left_right(row, min_col, max_col):# 最大行向下扩展max_row = row# 最小列向左扩展if min_col > 0:for col in range(min_col - 1, -1, -1):if img_data[row, col, 3] != threshold and col < min_col:min_col = colelse:break# 最大列向右扩展if max_col < width:for col in range(max_col + 1, width):if img_data[row, col, 3] != threshold and col > max_col:max_col = colelse:breakreturn min_col, max_row, max_col# 开始向下拓展for row in range(min_row + 1, height):# 非透明像素if img_data[row, min_col, 3] != threshold:min_col, max_row, max_col = extern_bottom_left_right(row, min_col, max_col)# 当前行最小列是透明像素else:# 向右判断当前行包围盒是否全为透明像素row_all_0_flag = Truefor col in range(min_col + 1, max_col):if img_data[row, col, 3] != threshold:row_all_0_flag = Falsemin_col, max_row, max_col = extern_bottom_left_right(row, min_col, max_col)break# 当前行包围盒全透明,封闭包围盒if row_all_0_flag:breakreturn min_row, min_col, max_row, max_col# 遍历图像以找到非“透明”区域 for row in range(height): col = 0while col < width:if img_data[row, col, 3] != threshold: # 假设threshold是“透明”的替代值 bounding_box = detect_bounding_box(row, col)print(f'第{output_index}张图片包围盒:{bounding_box}')min_row, min_col, max_row, max_col = bounding_box# 获取包围盒后置位透明img_data[min_row : max_row + 1, min_col : max_col + 1, 3] = 0# 裁剪并保存图像块 cropped_img = img.crop((min_col, min_row, max_col + 1, max_row + 1)) cropped_img.save(f"output3/output_{output_index}.png") print(f'第{output_index}张图片保存成功,路径:output2/output_{output_index}.png')output_index += 1 col = col + 1# 使用示例
start_time = time.time()
split_image_into_blocks("texture_00.png", threshold=0) # 假设0是“透明”的替代值
end_time = time.time()
usage_time = int(end_time - start_time)
m = usage_time // 60
s = usage_time % 60
print(f'任务执行完成,耗时:{m}分{s}秒')