py-opencv对图片批量处理后的转存且保留原文件名

import skimage.io as io
import  numpy as np
import os
import  cv2



DATADIR="./123" #这个位子放读取的文件夹路径
path=os.path.join(DATADIR)
img_list=os.listdir(path)
ind=0

*************************这一段是我对图片的处理没有参考价值************************
for image_name in img_list:
    img = io.imread(os.path.join(path, i),0)
    new_img =img
    height = new_img.shape[0]
    width = new_img.shape[1]
    arr = np.zeros((256))
    outimg = np.zeros((height, width)).astype(np.float32)
    for i in new_img:
        for j in i:
            arr[j] += 1
    for i in range(256):
        arr[i] /= width * height
    crr = np.zeros(256)
    drr = np.zeros(256)
    for i in range(256):
        for j in range(i):
            crr[i] += arr[j]
    for i in range(256):
        drr[i] = (int)(crr[i] * 255 + 0.5)
    for i in range(height):
        for j in range(width):
            new_img[i][j] = drr[new_img[i][j]]
************************************************************************
    ind = ind + 1


    cv2.imwrite('.\\1111\\'  + image_name, new_img)
#1111是存放的目标文件夹地址切,路径最后还要加上\\
#如果需要改变文件格式,如bmp转jpg,在cv2.imwrite()的filename后面加      + '.jpg'

文章的代码部分也是从网上整合的,记录一下学习过程。

你可能感兴趣的:(python,opencv)