Simple usage of python PIL module for image processing

Source: Internet
Author: User

Simple usage of python PIL module for image processing

This article describes how to use the PIL module of python to process images. It involves some techniques related to image sharpening, straight line drawing, and elliptical drawing using the PIL module of Python, for more information, see

This article describes how to use the PIL module of python to process images. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

#! /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 ()

Source image:

Processed drawing: (sharpened)

PS: In addition, there is another commonly used module, the image module.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.