import sys
import os
from PIL import Image
file_root = "/root/img" #要切的图片放的文件
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,3):
for i in range(0,3):
#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("/root/img/{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 裁剪九宫格图片 —— 筑梦之路
猜你喜欢
转载自blog.csdn.net/qq_34777982/article/details/125019068
今日推荐
周排行