python读取图像详细信息代码
时间: 2023-10-14 14:28:16 浏览: 54
可以使用Python的Pillow库来读取图像的详细信息。以下是一个示例代码:
```python
from PIL import Image
# 打开图像文件
img = Image.open('image.jpg')
# 获取图像基本信息
print("文件格式:", img.format)
print("图像大小:", img.size)
print("颜色模式:", img.mode)
# 获取图像的像素值
pixels = img.load()
print("图像像素值:", pixels[0,0])
# 关闭图像文件
img.close()
```
其中,`Image.open()`函数用于打开图像文件,`img.format`、`img.size`、`img.mode`分别获取图像的文件格式、大小和颜色模式,`img.load()`获取图像的像素值,`pixels[0,0]`获取图像的左上角像素点的值。最后,使用`img.close()`函数关闭图像文件。
相关问题
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 opencv图像增强详细代码
下面是一个简单的 Python OpenCV 图像增强代码示例,包括图像亮度调整、对比度调整、直方图均衡化和自适应直方图均衡化:
```python
import cv2
# 图像读取
img = cv2.imread('test.jpg')
# 图像亮度调整
brightness = 50
img_bright = cv2.add(img, brightness)
# 图像对比度调整
contrast = 1.5
img_contrast = cv2.convertScaleAbs(img, alpha=contrast, beta=0)
# 直方图均衡化
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_eq = cv2.equalizeHist(img_gray)
# 自适应直方图均衡化
img_adapt = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)).apply(img_gray)
# 显示图像
cv2.imshow('Original', img)
cv2.imshow('Brightness', img_bright)
cv2.imshow('Contrast', img_contrast)
cv2.imshow('Equalization', img_eq)
cv2.imshow('Adaptive Equalization', img_adapt)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
你可以根据需要调整亮度、对比度等参数,以实现更好的图像增强效果。