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)