修改图片的明亮度和对比度

def enhance_brightness(imgf, newname):
  img = Image.open(imgf)
  enhance = ImageEnhance.Brightness(img)
  bright_img = enhance.enhance(5)
  bright_img.show()
  bright_img.save(newname)


def enhance_contrast(imgf, newname):
  img = cv2.imread(imgf)
  hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  v_channel = hsv[:,:,2]
  contrast_img = cv2.addWeighted(v_channel, 2, np.zeros(v_channel.shape, v_channel.dtype), 0,0)
  hsv[:,:,2] = contrast_img
  contrast_img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
  cv2.imshow('contrast_img', cv2.resize(contrast_img,(1000,600)))
  cv2.waitKey(0)
  cv2.imwrite(newname, contrast_img)



imgl = r'D:\mydocs\ftp\temp_scan\left_155.png'
imgr = r'D:\mydocs\ftp\temp_scan\right_155.png'
enhance_brightness(imgl, r'D:\mydocs\ftp\temp_scan\bright_left_155.png')
enhance_brightness(imgr, r'D:\mydocs\ftp\temp_scan\bright_right_155.png')
enhance_contrast(imgl, r'D:\mydocs\ftp\temp_scan\contrast_left_155.png')
enhance_contrast(imgr, r'D:\mydocs\ftp\temp_scan\contrast_right_155.png')

原图修改图片的明亮度和对比度_第1张图片

亮度提升后

修改图片的明亮度和对比度_第2张图片

对比度提升

你可能感兴趣的:(python,opencv,开发语言)