python将一张图片复制成多张图片
时间: 2023-10-06 16:12:35 浏览: 37
要将一张图片复制成多张图片,可以使用Python中的PIL库(Python Imaging Library)。
首先,使用PIL库中的Image模块打开图片:
```python
from PIL import Image
img = Image.open("example.jpg")
```
接下来,可以使用PIL库中的Image模块中的resize()方法调整图片大小。假设需要将图片复制成4张图片,可以将原图片的大小调整为4份:
```python
img_resized = img.resize((img.width//2, img.height//2))
```
现在,img_resized是原图片的一半大小。接下来,可以使用PIL库中的Image模块的save()方法将图片保存为不同的文件名。例如,可以将原图片保存为example_1.jpg,img_resized保存为example_2.jpg:
```python
img.save("example_1.jpg")
img_resized.save("example_2.jpg")
```
最后,可以将img_resized再次缩小为一半大小,保存为example_3.jpg和example_4.jpg:
```python
img_resized2 = img_resized.resize((img_resized.width//2, img_resized.height//2))
img_resized2.save("example_3.jpg")
img_resized2.save("example_4.jpg")
```
完整代码如下:
```python
from PIL import Image
img = Image.open("example.jpg")
img_resized = img.resize((img.width//2, img.height//2))
img.save("example_1.jpg")
img_resized.save("example_2.jpg")
img_resized2 = img_resized.resize((img_resized.width//2, img_resized.height//2))
img_resized2.save("example_3.jpg")
img_resized2.save("example_4.jpg")
```