使用python opencv批量对贴图进行除透明边并压缩
生活随笔
收集整理的這篇文章主要介紹了
使用python opencv批量对贴图进行除透明边并压缩
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import glob
import cv2def 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 = 0bound_x_max = 0bound_y_min = 0bound_y_max = 0for row in range(height): # 遍歷高exist_color = Falsefor col in range(weight): # 遍歷寬if frame[row, col, 3] == 255:exist_color = Trueif bound_y_min == 0:bound_y_min = rowif not exist_color and bound_y_min != 0 and bound_y_max == 0:bound_y_max = row - 1for col in range(weight): # 遍歷高exist_color = Falsefor row in range(height): # 遍歷寬if frame[row, col, 3] == 255:exist_color = Trueif bound_x_min == 0:bound_x_min = colif not exist_color and bound_x_min != 0 and bound_x_max == 0:bound_x_max = col - 1clip_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)
?
總結
以上是生活随笔為你收集整理的使用python opencv批量对贴图进行除透明边并压缩的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PaddleOCR,图像检测识别
- 下一篇: 想知道手机配音软件哪个好用吗?一起来看看