# coding: utf-8
from PIL import Image
import os
import os.path
import numpy as np
import cv2
#指明被遍历的文件夹
rootdir=r'/home/mmediting/data/4K/valid/lr/000'
for parent, dirnames, filenames in os.walk(rootdir):#遍历每一张图片
filenames.sort()
for filename in filenames:
#print('parent is :' + parent)#parent is :/home/mmediting/data/4K/valid/gt/000
#print('filename is :' + filename)#filename is :00000084.png
currentPath = os.path.join(parent, filename)
print('the fulll name of the file is :' + currentPath)#the fulll name of the file is :/home/mmediting/data/4K/valid/gt/000/00000084.png
img = Image.open(currentPath)
#print (img.format, img.size, img.mode)#PNG (3840, 2160) RGB
#设置左、上、右、下的像素
#gt
#box1 = (0, 0, 1920, 1080)
# box2 = (0, 1080, 1920, 2160)
# box3 = (1920, 0, 3840, 1080)
# box4 = (1920, 1080, 3840, 2160)
#lr
box1=(0,0,480,270)
box2=(0,270,480,540)
box3=(480,0,960,270)
box4=(480,270,960,540)
image1 = img.crop(box4) # 图像裁剪
##存储裁剪得到的图像
image1.save(r"/home/mmediting/data/4K/valid_crop/lr/020"+'/'+filename)
将一个大图切成4份
仅需更改
image1 = img.crop(box4) # 图像裁剪
以及 径即可



