Image processing _Image

1. Install

   Enter pip install PIL error:

  ERROR: Could not find a version that satisfies the requirement PIL (from versions: none)
  ERROR: No matching distribution found for PIL

solution:

Pillow in Python3 from the PIL (2 in)

(1)       python -m pip install Pillow

(2) pip install path \ file name is the file name in the URL:

https://www.lfd.uci.edu/~gohlke/pythonlibs/

Download the corresponding module.

Error when using (1):

ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

Timeout issues, extend the time:

python -m pip --default-timeout=100 install -U Pillow

 

Note: pillow is a branch PIL (Python imaging library), it is no longer maintained. Therefore, in order to maintain backward compatibility, often using the old module name --PIL, that is a reference module, directly import PIL

 

2. Functions

The image data for quick access to several basic types of the pixel represented by the core, can do processing on the image filing, a display image, the common image processing (conversion, point operation, filtering, color, etc.).

1.1 Image Module

from PIL import Image

 

Open an image #

Picture = Image.open("C:\\Users\\sue\\Pictures\\test.png")

print(Picture)

 

# Returns the attributes of the instance

print ( "image format: {}; image mode: {}; image size:. {}". format (Picture.format, Picture.mode, Picture.size))

 

 

# View instance, show temporarily store a temporary file, there is efficiency

Picture.show()

 

# Instance methods:

# 1. Save pictures and convert picture format to be converted to report conversion errors: svae (storage file name [, storage file format: You can omit the name is determined by the extension])

Picture.save("C:\\Users\\sue\\Pictures\\test2.png","PNG")

 

Picture.save("C:\\Users\\sue\\Pictures\\test3.jpg")

try:

    Picture.save ( "C: \\ Users \\ sue \\ Pictures \\ test4.jpg", "JPG") # clear after format, format conversion but add error KeyError

except:

    print("cannot convert")

 

Thumbnail # 2 produced p.thumbnail ((x, y)), a parameter tuple

width,heighth = Picture.size

Picture.thumbnail((width/2,heighth/2))

Picture.save("C:\\Users\\sue\\Pictures\\test2.png","PNG")

 

 

Image # 3 Crop: p.crop ((x, y, x + m, y + n)), x, y for the image to the upper left corner as the origin, the downward y-axis, the x-axis to the right;

# M, n who want to cut the length and width

# Original position (20,10) start cutting a length 200, width 100 of FIG.

PCrop = Picture.crop((20,40,20+200,10+100))

PCrop.show()

 

4. Modification and paste #

# P.transpose (Image.XX): wherein XX = FLIP_LEFT_RIGHT (approximately mirror); FLIP_TOP_BOTTOM (mirror down)

# ROTATE_90 (rotated counterclockwise by 90 degrees); RATATE_180 (counterclockwise 180 degree rotation); ROTATAE_270;

# TRANSPOSE (transpose matrix of pixels, the spatial transform); TRANVERSE (space transformation)

# P.paste (p1, (x, y, x + m, y + n)), to paste the picture p, p1 (x, y), the length representing the size of n m wide. The latter two do not write is complete paste p1

 

from PIL import Image

 

# The figure on the left is a mirror image upside down, copied to the right, the right to the left copied

def P_transpose(P):

    x,y = P.size

 

    pleft = P.crop((0,0,x//2,y))

    pright = P.crop((x//2,0,x,y))

 

    pleft = pleft.transpose(Image.FLIP_TOP_BOTTOM)

 

    P.paste (pright, (0,0, x // 2, y))

    P.paste(pleft,(x//2,0,x,y))

    P.show()

 

Picture = Image.open("C:\\Users\\sue\\Pictures\\人物.png")

P_transpose(P)

 

# 5. resizing

# resize((m,n))

# Rotate (sigma), counterclockwise to adjust the angle

Guess you like

Origin www.cnblogs.com/wljlxx/p/11695390.html