Method of Increasing Image Contrast by python

  • 2021-07-16 02:44:34
  • OfStack

This code is to achieve, on the basis of 10 degrees of rotation, and then increase the contrast of the operation.

1 code:

The code in the code comment can run. But it is not very reliable, because the file name is edited one by one, which may not correspond to the original label. Better practice refer to code 2


# -*- coding: UTF-8 -*-
from PIL import Image
from PIL import ImageEnhance
import PIL.Image as img
from PIL import ImageEnhance
import os
 
def rotationImage(filepath,destpath):
 count = 0
 filelist=os.listdir(filepath) # File names of all files 
 total_num=len(filelist) # Number of all files 
 print(total_num) # Number of output files 
 for i in range(total_num): # Operate on each image 
  print(count)
  im=img.open(filepath+str(i+21)+str("_training")+".gif")
  for j in range(72):
   im_rotate=im.rotate(j*10) # Every image is 10 ° rotation 1 Times 
   # Then increase the brightness contrast and other operations 
 
   enh_con=ImageEnhance.Contrast(im_rotate) # Increase contrast   Get 1440 Zhang 
   image_contrasted=enh_con.enhance(1.5)
   image_contrasted.save(destpath + str("cont_") + str((j + 1) * 10) + str("_") + str(i + 21) + str("_") + str("manual1") + '.gif')
   count=count+1
   # enh_sha=ImageEnhance.Sharpness(im_rotate) # Increase sharpness 
   # image_sharped=enh_sha.enhance(3.0)
   # image_sharped.save(destpath + str("sharp_") + str((j + 1) * 10) + str("_") + str(i + 21) + str("_") + str("training") + '.tif')
 
   # enh_bri=ImageEnhance.Brightness(im_rotate) # Increase brightness   But there are problems 
   # image_bright=enh_bri.enhance(1.5)
   # image_bright.save(destpath + str("bri_") + str((j + 1) * 10) + str("_") + str(i + 21) + str("_") + str("training") + '.tif')
 
   # enh_col=ImageEnhance.Color(im_rotate) # Increase chromaticity   But there are problems ,
   # image_colored=enh_col.enhance(1.5)
   # image_colored.save(destpath + str("col_") + str((j + 1) * 10) + str("_") + str(i + 21) + str("_") + str("training") + '.tif')
 
  j=0
 
if __name__== '__main__':
 filepath='/home/qxq/Desktop/eyedata_final/train/label/gif/orginal/'
 destpath='/home/qxq/Desktop/eyedata_final/train/label/gif/brighten/'
 rotationImage(filepath,destpath)

2 code:

A more reliable approach is as follows:


# -*- coding: UTF-8 -*-
from PIL import Image
from PIL import ImageEnhance
import os
 
rootdir = r'/home/qxq/Desktop/eyedata_final/mask/original/' #  Indicates the folder to be traversed 
for parent, dirnames, filenames in os.walk(rootdir):
 for filename in filenames:
  currentPath = os.path.join(parent, filename)
  im = Image.open(currentPath)
  for j in range(72):
   im_rotate = im.rotate(j * 10) #  Every image is 10 ° rotation 1 Times 
 
   enh_con = ImageEnhance.Contrast(im_rotate) #  Increase contrast   Get 1440 Zhang (20*72=1440)
   image_contrasted = enh_con.enhance(1.5)
   newname1 = r"/home/qxq/Desktop/eyedata_final/mask/brighten/" + 'Cont_' + filename
   image_contrasted.save(newname1)
 
   enh_sha = ImageEnhance.Sharpness(im_rotate) #  Increase sharpness 
   image_sharped = enh_sha.enhance(3.0)
   newname2 = r"/home/qxq/Desktop/eyedata_final/mask/brighten/" + 'sharp_' + filename
   image_contrasted.save(newname2)
 
   #
   enh_bri = ImageEnhance.Brightness(im_rotate) #  Increase brightness   But there are problems 
   image_bright = enh_bri.enhance(1.5)
   newname3 = r"/home/qxq/Desktop/eyedata_final/mask/brighten/" + 'Bri_' + filename
   image_contrasted.save(newname3)
 
   #
   enh_col = ImageEnhance.Color(im_rotate) #  Increase chromaticity   But there are problems ,
   image_colored = enh_col.enhance(1.5)
   newname4 = r"/home/qxq/Desktop/eyedata_final/mask/brighten/" + 'Col_' + filename
   image_contrasted.save(newname4)
 
 
  j = 0

Related articles: