Python拆分无atlas图集(瑕疵版)

老板给了张没有atlas文件的图集让我拆图,简单写了一版凑合用。

存在的问题:

  • 可能会拆出来一些小尺寸的透明像素图片;
  • 可能会拆出来一些偏大的小图的整体集合;
  • 可能会把应该是一块的小图批成两半,不过不多;
  • 其他未知的问题...

小图目测基本齐全,没用的图...手删一下趴

有改正的同学烦请告知,比个♥

from PIL import Image  
import numpy as np  
import time
  
def 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 = 0
      
    col_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_col
        
        def 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 = col
                    else:
                        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 = col
                    else:
                        break
            
            return 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 = True
                for col in range(min_col + 1, max_col):
                    if img_data[row, col, 3] != threshold:
                        row_all_0_flag = False
                        min_col, max_row, max_col = extern_bottom_left_right(row, min_col, max_col)
                        break
                
                # 当前行包围盒全透明,封闭包围盒
                if row_all_0_flag:
                    break
        
        return min_row, min_col, max_row, max_col
  
    # 遍历图像以找到非“透明”区域  
    for row in range(height):  
        col = 0
        while 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}秒')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值