python中获取图片的长度和宽度
时间: 2023-11-27 07:55:35 浏览: 42
你可以使用Python的Pillow库来获取图片的长度和宽度。以下是示例代码:
```python
from PIL import Image
# 打开图片文件
img = Image.open('image.jpg')
# 获取图片的长度和宽度
width, height = img.size
print('图片宽度为:', width)
print('图片高度为:', height)
```
在上面的代码中,我们首先使用Pillow库的Image模块打开了一张图片文件,并将其赋值给变量img。然后,我们使用img对象的size属性来获取图片的长度和宽度,并分别将其赋值给变量width和height。最后,我们打印出这两个变量的值,以展示获取图片长度和宽度的结果。
相关问题
python中图片宽度高度的获取
在Python中,可以使用PIL库(Python Imaging Library)来获取图片的宽度和高度。具体代码如下:
```python
from PIL import Image
# 打开图片
img = Image.open('image.jpg')
# 获取图片的宽度和高度
width, height = img.size
print('图片宽度为:', width)
print('图片高度为:', height)
```
其中,`Image.open()`函数用于打开图片,`img.size`返回一个元组,包含图片的宽度和高度。
python读取一张图片文件的宽度和高度
你可以使用Python的Pillow库来读取图片文件的宽度和高度。下面是示例代码:
```python
from PIL import Image
# 打开图片文件
image = Image.open("example.jpg")
# 获取宽度和高度
width, height = image.size
print("宽度:", width)
print("高度:", height)
```
在这个示例中,我们打开了名为“example.jpg”的图片文件,并使用`.size`属性获取了它的宽度和高度。