用到的库 python- OpenCV
安装
pip install opencv-python
代码:
#!/usr/bin/python
#coding=utf-8
import os
import shutil
import re
import sys
from os import path
import json
import hashlib
import math
import imp
from imp import reload
import cv2
import numpy
reload(sys)
# 去掉图片的预乘
def buildImgToRemoveAlphaAdd(pathInit, pathTarget):
if not os.path.exists(pathInit):
print("pathInit not exists : ", pathInit)
return
makeDirForPath(pathTarget)
bgPath = pathInit
imgBgInit = cv2.imread(bgPath,-1)
tempWidth = len(imgBgInit[0])
tempHeight = len(imgBgInit)
imgBgRule = imgBgInit.copy()
for tempY in range(0, tempHeight):
for tempX in range(0,tempWidth):
if imgBgInit[tempY][tempX][3] != 0 and imgBgInit[tempY][tempX][3] != 255 :
data = imgBgInit[tempY][tempX]
imgBgInit[tempY][tempX][0] = math.ceil(data[0] / (data[3]/255))
imgBgInit[tempY][tempX][1] = math.ceil(data[1] / (data[3]/255))
imgBgInit[tempY][tempX][2] = math.ceil(data[2] / (data[3]/255))
if imgBgInit[tempY][tempX][0] > 255:
imgBgInit[tempY][tempX][0] = 255
if imgBgInit[tempY][tempX][1] > 255:
imgBgInit[tempY][tempX][1] = 255
if imgBgInit[tempY][tempX][2] > 255:
imgBgInit[tempY][tempX][2] = 255
print(tempX, tempY, imgBgInit[tempY][tempX][0], imgBgInit[tempY][tempX][1], imgBgInit[tempY][tempX][2], imgBgInit[tempY][tempX][3])
# cv2.imshow('small', imgBgInit)
# cv2.waitKey()
smallImgPath = pathTarget
cv2.imwrite(smallImgPath, imgBgInit, [cv2.IMWRITE_PNG_COMPRESSION, 9])
if __name__=="__main__":
buildImgToRemoveAlphaAdd("pathInit", "pathTarget")