用python切分图片尺寸为942.1140,每一行包含人脸20个,每一列包含人脸20个,那么每一个人脸区域大小为47.57;文件名叫qwe
时间: 2024-03-12 22:43:30 浏览: 52
好的,您可以使用PIL库来进行图片的切割和保存操作。以下是一个示例代码:
```python
from PIL import Image
# 打开图片
img = Image.open("your_image_path.jpg")
# 定义人脸区域大小和每行每列的人脸数
face_size = (47, 57)
faces_per_row = 20
faces_per_column = 20
# 计算每个人脸的起始坐标和结束坐标
face_start_x = 0
face_start_y = 0
face_end_x = face_start_x + face_size[0]
face_end_y = face_start_y + face_size[1]
# 遍历图片并切分
for i in range(faces_per_column):
for j in range(faces_per_row):
# 切割人脸区域
face = img.crop((face_start_x, face_start_y, face_end_x, face_end_y))
# 保存人脸区域
face.save(f"qwe_{i}_{j}.jpg")
# 更新下一个人脸的起始和结束坐标
face_start_x = face_end_x
face_end_x = face_start_x + face_size[0]
face_start_x = 0
face_end_x = face_start_x + face_size[0]
face_start_y = face_end_y
face_end_y = face_start_y + face_size[1]
```
在上面的代码中,您需要将`your_image_path.jpg`替换为您的图片路径。代码将会遍历整个图片,将每个人脸区域切割出来,并保存到以`qwe_行号_列号.jpg`为格式的文件名中。
阅读全文