pillow block

pillow block

Image processing

Chinese documents

installation

pip install Pillow

Picture rotated 90 degrees to display

from PIL import Image
im=Image.open("t.jpg")
im.rotate(90).show()

The picture and crop and save

from PIL import Image
import glob, os

size = 128, 128

for infile in glob.glob("*.jpg"):
    file, ext = os.path.splitext(infile)
    im = Image.open(infile)
    im.thumbnail(size, Image.ANTIALIAS)
    im.save(file + ".thumbnail.jpg", "JPEG")

Cutting pictures

from the PIL Import Image 

DEF fill_image (Image): 

    width, height = image.size 

    # selected length and width of the larger value as a new image side length 
    new_image_length width = IF width> height the else height 

    # generates a new image [white], other colors may be arranged background 
    new_image = Image.new (image.mode, (new_image_length, new_image_length), color = ' White ' ) 

    # picture before a new input image in FIG attached, centered 
    IF width> height: # picture large high, the vertical dimension of the picture is populated # (x, y) represents a tuple FIG paste on the relative location of the start of the FIG., a coordinate point 
        new_image.paste (image, (0, int ((new_image_length - height ) / 2 )))
    else:
        new_image.paste(image, (int((new_image_length - width) / 2), 0))

    return new_image

def cut_image(image):

    width, height = image.size

    item_width =int(width /3) # 因为朋友圈一行放3张图

    box_list = []

    for i in range(0, 3):

        for j in range(0, 3):

            box = (j*item_width, i*item_width, (j+1)*item_width, (i+1)*item_width) # (left, top, right, bottom)

            box_list.append(box)

    image_list = [image.crop (Box) for Box in box_list] 

    return image_list 

DEF save_images (image_list): 

    index =. 1 for Image in   image_list: 
        Image.Save (STR (index) + ' .png ' , ' PNG ' ) 
        index + 1 = IF __name__ == ' __main__ ' : 
    file_path = " t.jpg " # the target picture input image file into the folder in which the code 
    image

    


        
   
= Image.open(file_path)
    image = fill_image(image)

    image_list = cut_image(image)

    save_images(image_list)

 

Guess you like

Origin www.cnblogs.com/huay/p/11882186.html