Python基于EXIF方向打图片日期戳:orientation3异常问题求助
图片EXIF方向为3时戳记显示异常问题
我编写了一个利用EXIF文件中的日期字段为图片添加戳记的函数,该代码在图片orientation为1时运行正常(基于0,0坐标戳记),但当图片orientation为3时,日期戳会颠倒且显示在图片顶部。请问是否有人遇到过该问题?
以下是打戳函数代码:
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()
内容的提问来源于stack exchange,提问作者Larry
相关产品推荐
相关产品推荐

