提问人:Larry 提问时间:8/23/2023 更新时间:8/23/2023 访问量:36
有没有办法使用 Python 根据 EXIF 方向对图像进行日期标记?
Is there a way to date stamp images with based on EXIF orientation using Python?
问:
我有以下函数,可以使用exif文件中的日期字段标记图像。此代码适用于方向为 1 的图像。见附件照片 4,这些图像是根据 0,0 x,y 加盖的。如果图像的方向为 3,则日期将倒置并标记在图像的顶部,如下所示。照片 2还有其他人遇到过这个问题吗?
这是我调用的用于标记图像的函数。
def stampImage(image, date, ALIGN):
from PIL import ImageFont
from PIL import ImageDraw
from PIL import Image
import os
import arcpy
fontsPath = r'\mdtdata\arcgis\fonts'
outputPath = r'\mdtdata\arcgis\home'
PIXELS_BETWEEN_LINES = 3
astr = date
FONT_SIZE = 80 #int(arcpy.GetParameterAsText(1))
TEXT_COLOR = "White" #arcpy.GetParameterAsText(2)
foreground = Image.open(os.path.join(outputPath, image))
EXIF = foreground.getexif()
fits = False #assume the text doesn't fit to start
finalSize = FONT_SIZE #start with user specified fontsize and test for fit. Shrink as necessary
foreground.size
edgePadding = 10
ULX = foreground.size[0] - foreground.size[0] + edgePadding
ULY = foreground.size[1] - edgePadding - 60
LRX = foreground.size[0] - edgePadding
LRY = foreground.size[1] - edgePadding
while not fits:
words = astr.split()
img = Image.open(os.path.join(outputPath, image))
MAX_W = LRX - ULX
MAX_H = LRY - ULY
draw1 = ImageDraw.Draw(img)
font1 = ImageFont.truetype(os.path.join(fontsPath, "LiberationSans-Bold-webfont.ttf"), finalSize)
w, h = draw1.textsize(astr, font=font1)
MAX_LINES = MAX_H // (h + PIXELS_BETWEEN_LINES)
sentence = []
lines = []
for idx, word in enumerate(words):
sentenceTest = " ".join(sentence)
sentence.append(word)
sentenceStr = " ".join(sentence)
w, h = draw1.textsize(sentenceStr, font=font1)
if (w > MAX_W):
lines.append(sentenceTest)
sentence = []
sentence.append(word)
arcpy.AddMessage("LINE CALCULATED: '" + sentenceTest + "' and new line started")
arcpy.AddMessage("idx: '" + str(idx) + ", " + str(len(words) - 1))
if (idx == (len(words) - 1)):
sentenceStr = " ".join(sentence)
lines.append(sentenceStr)
elif (idx == (len(words) - 1)):
lines.append(sentenceStr)
if (MAX_LINES >= len(lines)):
fits = True
else:
fits = False
finalSize -= 1
FONT_SIZE = finalSize
totalHeight = (len(lines) * (h + PIXELS_BETWEEN_LINES)) - PIXELS_BETWEEN_LINES
MAX_W = LRX - ULX
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(os.path.join(fontsPath, "LiberationSans-Bold-webfont.ttf"), FONT_SIZE)
current_h = ((LRY - ULY) - totalHeight) / 2 + ULY
if (ALIGN == "Right"):
for line in lines:
w, h = draw.textsize(line, font=font)
draw.text((ULX + (MAX_W - w), current_h), line, font=font, fill=TEXT_COLOR)
current_h += h + PIXELS_BETWEEN_LINES
elif (ALIGN == "Center"):
for line in lines:
w, h = draw.textsize(line, font=font)
draw.text((((LRX - ULX - w) / 2), current_h), line, font=font, fill=TEXT_COLOR)
current_h += h + PIXELS_BETWEEN_LINES
else:
for line in lines:
w, h = draw.textsize(line, font=font)
draw.text((ULX, current_h), line, font=font, fill=TEXT_COLOR)
current_h += h + PIXELS_BETWEEN_LINES
img.save(image, exif=EXIF)
img.close()
答: 暂无答案
评论