python 怎么修改图片的Exif数据
时间: 2024-02-22 21:57:46 浏览: 31
你可以使用 Python 的 PIL 库来修改图片的 Exif 数据。具体的操作步骤如下:
1. 导入 PIL 库和 ExifTags 模块:
```
from PIL import Image
from PIL.ExifTags import TAGS
```
2. 打开图片并获取 Exif 数据:
```
img = Image.open("example.jpg")
exif_data = img._getexif()
```
3. 将 Exif 数据转换为字典形式:
```
exif_dict = {}
if exif_data:
for tag_id, value in exif_data.items():
tag = TAGS.get(tag_id, tag_id)
exif_dict[tag] = value
```
4. 修改 Exif 数据:
```
exif_dict["DateTimeOriginal"] = "2022:01:01 00:00:00"
```
5. 将修改后的 Exif 数据写入图片:
```
img.save("example_with_new_exif.jpg", exif=img_dict)
```
注意:修改 Exif 数据可能会影响图片的元数据,建议备份原始图片文件。
相关问题
python 向没有exif数据的图片 添加Exif数据
向没有 Exif 数据的图片添加 Exif 数据可以使用 Python 的 Pillow 库。具体的操作步骤如下:
1. 导入 Pillow 库和 ExifTags 模块:
```
from PIL import Image
from PIL.ExifTags import TAGS
from PIL import JpegImagePlugin
```
2. 打开图片并创建 Exif 数据:
```
img = Image.open("example.jpg")
exif_dict = {"DateTimeOriginal": "2022:01:01 00:00:00", "Make": "Canon", "Model": "Canon EOS R5"}
exif_bytes = JpegImagePlugin.get_exif_bytes(exif_dict)
```
3. 将 Exif 数据写入图片:
```
img.save("example_with_exif.jpg", exif=exif_bytes)
```
注意:如果图片已经存在 Exif 数据,使用此方法会覆盖原有的 Exif 数据。
使用python 写一个读写图片的exif数据
可以使用Python的Pillow库来读写图片的exif数据。以下是一个示例代码:
```python
from PIL import Image
from PIL.ExifTags import TAGS
# 读取图片
image = Image.open('example.jpg')
# 获取exif数据
exifdata = image.getexif()
# 将exif数据转换成字典格式
exif = {}
for tag_id, value in exifdata.items():
tag = TAGS.get(tag_id, tag_id)
exif[tag] = value
# 打印exif数据
print(exif)
# 修改exif数据
exif['Artist'] = 'John Doe'
# 将修改后的exif数据写回图片
exif_bytes = bytes()
for tag, value in exif.items():
tag_id = TAGS.get(tag, tag)
exif_bytes += bytes(tag_id) + bytes([0]) + bytes([0]) + \
bytes([len(value)]) + bytes([0]) + bytes(value)
image.save('example.jpg', exif=exif_bytes)
```
在上面的代码中,首先使用`Image.open()`方法读取图片。然后,使用`image.getexif()`方法获取图片的exif数据,并将其转换成字典格式。接下来,可以对exif数据进行修改,最后使用`image.save()`方法将修改后的exif数据写回图片。注意,exif数据必须以字节形式传递给`image.save()`方法的`exif`参数。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.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)