0

I have the following function that stamps images using the date field in the exif file. This code works fine for images that have an orientation of 1. See attachmentPhoto 4 these images are stamped based on the 0,0 x,y. If the image has an orientation of 3 the date is stamped upside down and at the top of the image like this image.Photo 2 Has anyone else run into this issue?

Here is the function I'm calling to stamp the image.

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()
Larry
  • 1
  • 2
  • Can you inspect the orientation and then write the text on a layer then rotate the layer based on the orientation? – JonSG Aug 23 '23 at 13:02
  • This might be of value: [How do I draw text at an angle using python's PIL?](https://stackoverflow.com/questions/245447/how-do-i-draw-text-at-an-angle-using-pythons-pil) – JonSG Aug 23 '23 at 13:03
  • I can return the date object that has the various orientations. In my small mind its seems as though I just need to rotate the stamp based on the orientation. I'm guessing I would perform that in my stamp image function. – Larry Aug 23 '23 at 19:45

0 Answers0