批量修改图片----去掉预乘

用到的库 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")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值