|
#! /Usr/bin/env python
# Encoding: UTF-8
Import Image
Class myimg:
Def _ init _ (self, open_file, save_file ):
Self. img = Image. open (open_file)
Self. save_file = save_file
Def Change_Size (self, percent = 100, height = None, width = None ):
'''''
Percent: the percentage is used to determine the size.
Height, width: determines the size based on the specified height and width.
'''
If not (height and width ):
Width, height = self. img. size
New_img = self. img. resize (width * percent/100, height * percent/100), Image. BILINEAR)
New_img.save (self. save_file)
Def Rotation (self, angle ):
'''''
Angle: degrees of Rotation
'''
Rot_img = self. img. rotate (angle) # rotate
Rot_img.save (self. save_file)
Def Save_as (self, filename ):
'''''
Filename: Save As image format, directly according to the suffix
'''
Self. img. save (filename)
Def Draw_Something (self ):
'''''
Use ImageDraw to draw images
'''
Import ImageDraw
Draw = ImageDraw. Draw (self. img)
Width, height = self. img. size
Draw. line (255), (width-1, height-1), fill =) # draw a straight line
Draw. line (0, height-1), (width-1, 0), fill = 255)
Draw. arc (0,360, width-1, height-1), 255, fill =) # draw an elliptic
Self. img. save (self. save_file)
Def Enhance_Something (self ):
'''''
Use ImageEnhance to enhance the image effect
'''
Import ImageEnhance
Brightness = ImageEnhance. Brightness (self. img)
Bright_img = brightness. enhance (2.0) # brightness Enhancement
Bright_img.save (self. save_file)
Sharpness = ImageEnhance. Sharpness (self. img)
Sharp_img = sharpness. enhance (7.0) # sharpness Enhancement
Sharp_img.save (self. save_file)
Contrast = ImageEnhance. Contrast (self. img) # contrast enhancement
Contrast_img = contrast. enhance (2.0)
Contrast_img.save (self. save_file)
If _ name _ = "_ main __":
File_name = r "D: \ test.png"
Save_file = r "D: \ save.png"
Saveas_file = r "D: \ save_as.bmp"
Oimg = myimg (file_name, save_file)
Oimg. Change_Size (30)
Oimg. Rotation (45)
Oimg. Save_as (saveas_file)
Oimg. Draw_Something ()
Oimg. Enhance_Something ()
|