使用python切割图片

import sys
import os 
from PIL import Image
file_root = "E:\\work\\yin\\yinn" #要切的图片放的文件
fileList = []
n = 0
#读取文件列表
def read_bmp_file_list(path):
     for root, dirs, files in os.walk(path, topdown=False):
            for file in files:
                fileList.append(os.path.join(root, file))
#开始切图
def qie_tu(file_path):
    global n
    im = Image.open(file_path)
    # 图片的宽度和高度
    img_size = im.size
    print("图片宽度和高度分别是{}".format(img_size))
    #切成512X512
    x = 0
    y = 0
    w = 512
    h = 512
    for j in range(0,4):
        for i in range(0,6):
            #region = im.crop((x, y, x+w*i, y+h*j))
            print(x+w*i, y+h*j,x+w*i+w, y+h*j+h)
            region = im.crop((x+w*i, y+h*j,x+w*i+w, y+h*j+h))
            #文件输出位置
            region.save("E:\\work\\yin\\yinn_o\\{name}.jpg".format(name=n))
            n=n+1

if __name__ == '__main__':
    read_bmp_file_list(file_root)
    for f in fileList:
        qie_tu(f)

你可能感兴趣的:(图像处理,python)