python将一张图片切割成四份
时间: 2024-05-12 09:20:50 浏览: 112
以下是将一张图片切割成四份的Python代码示例:
```python
from PIL import Image
# 打开图片
img = Image.open('example.jpg')
# 获取图片尺寸
width, height = img.size
# 切割成四份
left = 0
top = 0
right = width/2
bottom = height/2
img1 = img.crop((left, top, right, bottom))
left = width/2
top = 0
right = width
bottom = height/2
img2 = img.crop((left, top, right, bottom))
left = 0
top = height/2
right = width/2
bottom = height
img3 = img.crop((left, top, right, bottom))
left = width/2
top = height/2
right = width
bottom = height
img4 = img.crop((left, top, right, bottom))
# 保存切割后的图片
img1.save('img1.jpg')
img2.save('img2.jpg')
img3.save('img3.jpg')
img4.save('img4.jpg')
```
以上代码将一张名为`example.jpg`的图片切割成四份,并将其保存成四个单独的文件,文件名分别为`img1.jpg`、`img2.jpg`、`img3.jpg`和`img4.jpg`。
阅读全文