读取文件夹下图片 进行剪裁若干张并全部保存
from PIL import Image
import sys
import cv2
import numpy as np
import os
def cut_image(image):
width, height = image.size
item_width = int(width / 2)
item_height = int(height / 4)
box_list = []
count = 0
for j in range(0,4):
for i in range(0, 2):
count += 1
box = (i * item_width, j * item_height, (i + 1) * item_width, (j + 1) * item_height)
box_list.append(box)
image_list = [image.crop(box) for box in box_list]
return image_list
def process():
file= 'E:/databases/wood/hongxiang_process'
for filename in os.listdir(file):
img_path = file + ('/%s' % filename)
image = Image.open(img_path)
image_list = cut_image(image)
index = 1
for image in image_list:
image.save(r'E:\databases\wood\crophongxiang' +('/%s' % filename.split('.')[0])+ str(index) + '.jpg')
index += 1
process()