python读取图像属性代码
时间: 2023-10-12 07:19:54 浏览: 46
你可以使用Python中的Pillow库来读取图像属性。以下代码演示了如何读取图像的宽度、高度和颜色模式:
```python
from PIL import Image
# 打开图像文件
image = Image.open("example.jpg")
# 获取图像属性
width, height = image.size
color_mode = image.mode
# 输出图像属性
print("图像宽度:", width)
print("图像高度:", height)
print("颜色模式:", color_mode)
```
其中,`Image.open()`函数用于打开图像文件,`image.size`返回一个元组,包含图像的宽度和高度,`image.mode`返回图像的颜色模式。
相关问题
python读取图像gps属性代码
下面是一个使用 `Pillow` 库(也称为 `PIL`)读取图像 GPS 属性的示例代码:
```python
from PIL import Image
from PIL.ExifTags import TAGS
def get_gps_info(img_path):
with Image.open(img_path) as img:
try:
exif_data = img._getexif()
gps_info = {}
if exif_data:
for tag, value in exif_data.items():
decoded = TAGS.get(tag, tag)
if decoded == "GPSInfo":
gps_info = {}
for t in value:
sub_decoded = TAGS.get(t, t)
gps_info[sub_decoded] = value[t]
except Exception as e:
print(e)
gps_info = {}
return gps_info
```
这个函数接受一个图像路径作为输入,并返回一个包含 GPS 信息的字典。如果图像没有 GPS 信息,则返回一个空字典。
python读取tif属性代码
你可以使用 Python 库 `rasterio` 来读取 tif 图像的属性,包括分辨率、投影等信息。以下是一个示例代码,可以读取一个名为 `input.tif` 的 tif 图像,并输出其属性信息:
```python
import rasterio
# 打开 tif 图像文件
with rasterio.open('input.tif') as src:
# 输出图像属性信息
print(f'Width: {src.width}')
print(f'Height: {src.height}')
print(f'Number of bands: {src.count}')
print(f'Coordinate Reference System (CRS): {src.crs}')
print(f'Transform matrix: {src.transform}')
print(f'Resolution: {src.res}')
```
这段代码使用 `rasterio` 库打开一个名为 `input.tif` 的 tif 图像文件,并输出其宽度、高度、波段数、投影、变换矩阵和分辨率等属性信息。你可以根据需要扩展这段代码,读取和处理更多的 tif 图像属性信息。