在Python中,你可以使用Pillow库来获取图片的尺寸。以下是获取图片尺寸的步骤和代码示例:
1. 安装Pillow库:
```
pip install pillow
2. 使用Pillow库获取图片尺寸的代码示例:
```python
from PIL import Image
本地图片路径
file_path = 'path/to/your/image.jpg'
打开图片
img = Image.open(file_path)
获取图片尺寸
width, height = img.size
打印图片尺寸
print(f"图片尺寸:{width} x {height}")
如果你需要获取远程图片的尺寸,可以使用`requests`库下载图片,然后用Pillow打开它:
```python
import requests
from PIL import Image
from io import BytesIO
远程图片URL
url = 'http://example.com/path/to/image.jpg'
下载图片
response = requests.get(url)
image_content = response.content
使用BytesIO将内容转换为文件对象
image_file = BytesIO(image_content)
打开图片
img = Image.open(image_file)
获取图片尺寸
width, height = img.size
打印图片尺寸
print(f"图片尺寸:{width} x {height}")
以上代码将帮助你获取本地或远程图片的尺寸信息。