02 图片批量剪切
安装:
pip3 install cv2
# 没有找到cv2应该安装opencv-python
pip3 install opencv-python
使用:
from PIL import Image
import os
import os.path
import numpy as np
import cv2
image_path = r'F:\whole-develop\offline\crawler\scrapy_frame\first_scrapy\first_scrapy\scripts\image'
ok_iamge_path = r'F:\whole-develop\offline\crawler\scrapy_frame\first_scrapy\first_scrapy\scripts\ok_image'
# 指明被遍历的文件夹
rootdir = image_path
for parent, dirnames, filenames in os.walk(rootdir): # 遍历每一张图片
for filename in filenames:
print('parent is :' + parent)
print('filename is :' + filename)
currentPath = os.path.join(parent, filename)
print('the fulll name of the file is :' + currentPath)
img = Image.open(currentPath)
print(img.format, img.size, img.mode)
# img.show()
# (left, upper, right, lower
box1 = (0, 19, 189, 272) # 设置左、上、右、下的像素
image1 = img.crop(box1) # 图像裁剪
image1.save(ok_iamge_path + '\\' + filename) # 存储裁剪得到的图像