使用python 读取 exif信息实现照片加水印
时间: 2023-05-15 18:07:42 浏览: 233
可以使用Python的Pillow库来读取照片的exif信息,并使用PIL库来实现照片加水印的功能。以下是一个简单的代码示例:
```python
from PIL import Image, ImageDraw, ImageFont
from PIL.ExifTags import TAGS
# 读取照片的exif信息
def get_exif_data(image):
exif_data = {}
try:
img = Image.open(image)
info = img._getexif()
if info:
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
exif_data[decoded] = value
except Exception as e:
print(e)
return exif_data
# 加水印
def add_watermark(image, text):
img = Image.open(image)
draw = ImageDraw.Draw(img)
width, height = img.size
font = ImageFont.truetype('arial.ttf', 36)
textwidth, textheight = draw.textsize(text, font)
x = width - textwidth - 10
y = height - textheight - 10
draw.text((x, y), text, font=font, fill=(255, 255, 255, 128))
img.save('watermarked.jpg')
# 使用示例
exif_data = get_exif_data('photo.jpg')
if exif_data:
add_watermark('photo.jpg', 'Copyright')
```
这个代码示例可以读取照片的exif信息,并在照片右下角加上一个白色半透明的水印。你可以根据自己的需要修改水印的位置、颜色、字体等参数。
阅读全文
相关推荐
















