python如何将两张图片大小变得一样
时间: 2024-03-21 19:44:12 浏览: 31
你可以使用Python中的Pillow库来实现这个功能。首先,你需要打开两张图片并获取它们的大小。然后,你可以使用Pillow库中的resize()方法来将它们的大小变得一样。下面是一个示例代码:
```python
from PIL import Image
# 打开两张图片
image1 = Image.open("image1.jpg")
image2 = Image.open("image2.jpg")
# 获取图片的大小
width1, height1 = image1.size
width2, height2 = image2.size
# 将图片大小变得一样
if width1 != width2 or height1 != height2:
if width1 > width2:
image1 = image1.resize((width2, height2))
else:
image2 = image2.resize((width1, height1))
```
这段代码将两张图片中较小的那张图片的大小调整为与另一张图片相同。你可以根据自己的需求进行修改。
相关问题
怎么用python将两张图片大小调节的一样
你可以使用Python中的Pillow库来实现这个功能。具体步骤如下:
1. 导入Pillow库
```python
from PIL import Image
```
2. 打开两张图片
```python
img1 = Image.open("path/to/image1.jpg")
img2 = Image.open("path/to/image2.jpg")
```
3. 获取两张图片的尺寸
```python
width1, height1 = img1.size
width2, height2 = img2.size
```
4. 调整图片大小
```python
if width1 != width2 or height1 != height2:
if width1/height1 > width2/height2:
img1 = img1.resize((int(height2/height1*width1), height2))
img2 = img2.resize((int(height2/height1*width2), height2))
else:
img1 = img1.resize((width2, int(width2/width1*height1)))
img2 = img2.resize((width2, int(width2/width1*height2)))
```
5. 保存调整后的图片
```python
img1.save("path/to/resized_image1.jpg")
img2.save("path/to/resized_image2.jpg")
```
这样,两张图片的大小就被调整为一样了。
将两张图片设为一样大小python
可以使用Python的Pillow库来调整图片大小。以下是一个简单的示例代码:
```python
from PIL import Image
# 打开图片
img1 = Image.open("image1.jpg")
img2 = Image.open("image2.jpg")
# 将图片大小设为一致
img1 = img1.resize((img2.size[0], img2.size[1]))
# 保存调整大小后的图片
img1.save("image1_resized.jpg")
```
这段代码可以将名为“image1.jpg”和“image2.jpg”的两张图片调整为相同的大小,然后将“image1.jpg”保存为“image1_resized.jpg”。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)