1. 程式人生 > >python學習筆記之基本的影象處理

python學習筆記之基本的影象處理

對比度、亮度、高斯模糊濾波操作

# -*- coding: utf-8 -*-

import os
from PIL import Image, ImageEnhance
from scipy.ndimage import filters
from scipy import misc
#from matplotlib import pyplot as plt

class preprocess_img(object):
    def __init__(self,imgs_path,save_dir = "./"):
        self.imgs_path = imgs_path
        self.
save_dir = save_dir if not os.path.exists(self.save_dir): os.makedirs(self.save_dir) print("%s had been made successfully!"%(self.save_dir)) def change_contrast(self,factor): ''' Adjust image contrast. This class can be used to control the contrast of an image, similar to the contrast control on a TV set. An enhancement factor of 0.0 gives a solid grey image. A factor of 1.0 gives the original image. '''
if os.path.isdir(self.imgs_path): img_lists = os.listdir(self.imgs_path) for item in img_lists: img_path = os.path.join(self.imgs_path,item) img = Image.open(img_path) img_change = ImageEnhance.Contrast(img).enhance(factor)
save_path = os.path.join(self.save_dir,item) img_change.save(save_path) else: img = Image.open(self.imgs_path) img_change = ImageEnhance.Contrast(img).enhance(factor) temp_tuple = os.path.split(self.imgs_path) save_path = os.path.join(temp_tuple[0],"Contr_"+str(factor)+"_"+temp_tuple[1]) img_change.save(save_path) def change_bright(self, factor): ''' Adjust image brightness, used to control the brightness of an image. An enhancement factor of 0.0 gives a black image. A factor of 1.0 gives the original image. param factor: A floating point value controlling the enhancement. ''' if os.path.isdir(self.imgs_path): img_lists = os.listdir(self.imgs_path) for item in img_lists: img_path = os.path.join(self.imgs_path,item) img = Image.open(img_path) img_change = ImageEnhance.Brightness(img).enhance(factor) save_path = os.path.join(self.save_dir,item) img_change.save(save_path) else: img = Image.open(self.imgs_path) img_change = ImageEnhance.Brightness(img).enhance(factor) temp_tuple = os.path.split(self.imgs_path) save_path = os.path.join(temp_tuple[0],"Bright_"+str(factor)+"_"+temp_tuple[1]) img_change.save(save_path) def change_Blur(self,factor): ''' Parameters ---------- input : array_like Input array to filter. sigma : scalar or sequence of scalars Standard deviation for Gaussian kernel. The standard deviations of the Gaussian filter are given for each axis as a sequence, or as a single number, in which case it is equal for ''' if os.path.isdir(self.imgs_path): img_lists = os.listdir(self.imgs_path) for item in img_lists: img_path = os.path.join(self.imgs_path,item) imag_arr = misc.imread(img_path,mode='RGB') blur_image = filters.gaussian_filter(imag_arr,[factor, factor, 0]) #blur_image = filters.uniform_filter(imag_arr,[factor, factor, 0]) save_path = os.path.join(self.save_dir,item) misc.imsave(save_path,blur_image) else: imag_arr = misc.imread(self.imgs_path,mode='RGB') blur_image = filters.gaussian_filter(imag_arr,[factor, factor, 0]) temp_tuple = os.path.split(self.imgs_path) save_path = os.path.join(temp_tuple[0],"Blur_"+str(factor)+"_"+temp_tuple[1]) misc.imsave(save_path,blur_image) if __name__ == '__main__': temp = preprocess_img(imgs_path=r"65412119891005287X_20170114044649-idcardimage.jpg") temp.change_contrast(factor=2.0)