使用python opencv批量对贴图进行除透明边并压缩

import glob
import cv2


def access_pixels(file, frame):
    print(frame.shape)  # shape内包含三个元素:按顺序为高、宽、通道数
    height = frame.shape[0]
    weight = frame.shape[1]
    channels = frame.shape[2]

    print("weight : %s, height : %s, channel : %s" % (weight, height, channels))
    if channels != 4:
        return

    # cv2.imwrite('crop.jpg', crop)
    bound_x_min = 0
    bound_x_max = 0
    bound_y_min = 0
    bound_y_max = 0

    for row in range(height):  # 遍历高
        exist_color = False
        for col in range(weight):  # 遍历宽
            if frame[row, col, 3] == 255:
                exist_color = True
                if bound_y_min == 0:
                    bound_y_min = row
        if not exist_color and bound_y_min != 0 and bound_y_max == 0:
            bound_y_max = row - 1

    for col in range(weight):  # 遍历高
        exist_color = False
        for row in range(height):  # 遍历宽
            if frame[row, col, 3] == 255:
                exist_color = True
                if bound_x_min == 0:
                    bound_x_min = col
        if not exist_color and bound_x_min != 0 and bound_x_max == 0:
            bound_x_max = col - 1

    clip_image = frame[max(bound_y_min - 5, 0):min(bound_y_max + 5, height), max(bound_x_min - 5, 0):min(bound_x_max + 5, weight)]
    resized_img = cv2.resize(clip_image, (clip_image.shape[1] // 2, clip_image.shape[0] // 2))
    #cv2.imshow('clip_image', clip_image)
    cv2.imwrite(file, resized_img)


def main():
    """入口"""
    # file = "D:/alt/unlimited/project/Assets/UI/Atlas/Icon/Icon_M4A1S_Normal.png"
    for file in glob.glob("D:/alt/unlimited/project/Assets/UI/Atlas/Icon/*.png"):
        src = cv2.imread(file, cv2.IMREAD_UNCHANGED)
        # cv2.imshow(file, src)
        access_pixels(file, src)



if __name__ == "__main__":
    main()
    cv2.waitKey(0)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值