Select to view content in your preferred language

Create Watermark on images post processing

904
1
01-13-2023 06:47 AM
kapalczynski
Frequent Contributor

 

 

I am trying to follow this example that seems to show how to post process images in a Feature Class with a watermark.

https://github.com/Esri/field-maps-scripts/blob/master/notebooks/Generate%20PDF%20Report/Generate%20...

As you can see from the code below I have commented out a bunch in the process_image function.

I am first off trying to get the  Lat Long and Time from the image and simply print those out ...

  1. I cannot figure out what the input_file parameter needs to be set to... As you can see I am trying to use the variable that was set earlier 
  2. I am not seeing where the script reads all the records and checks all the images that may exist on a single feature in the dataset.

Any help would be greatly appreciated.

 

import arcgis
from arcgis.gis import GIS
from PIL import Image, ImageOps, ImageDraw, ImageFont
import base64
import io
import json
import datetime
from PIL.ExifTags import GPSTAGS, TAGS

agol_username = 'username' 
agol_password = 'xxxxxx'  
gis = GIS('https://xxxx.maps.arcgis.com', agol_username, agol_password)
item = gis.content.get("eed50dc6xxxxxxxx876021a84eef")
processing_layer = item.layers[0]

def process_image():
    #image = Image.open(input_file)    
    image = Image.open(processing_layer )
    image = ImageOps.exif_transpose(image)
    lat, long, gps_time = get_location_data(image.getexif())
    print(lat)
    print(long)
    print(gps_time)
    
    #image_editable = ImageDraw.Draw(image)
    #font = ImageFont.truetype("/Library/Fonts/Arial Unicode.ttf", 24)
    #image_editable.text((1,1), f'{lat}, {long} at {gps_time.strftime("%Y-%m-%d %H:%M:%S")}', (255,255,0), font=font)
    #io_bytes = io.BytesIO()
    #image.save(io_bytes, format="JPEG")
    #io_bytes.seek(0)
    #return base64.b64encode(io_bytes.read()).decode()

def get_location_data(exif_data):
    exif = { TAGS[k]: v for k, v in exif_data.items() if k in TAGS and type(v) is not bytes }
    lat = None
    long = None
    gps_time = None
    if "GPSInfo" in exif:
        gps_info = {}
        for key, val in GPSTAGS.items():
            if key in exif["GPSInfo"]:
                gps_info[val] = exif["GPSInfo"][key]
        if "GPSLatitude" in gps_info:
            lat = get_decimal_from_dms(gps_info["GPSLatitude"], gps_info["GPSLatitudeRef"])
        if "GPSLongitude" in gps_info:
            long = get_decimal_from_dms(gps_info["GPSLongitude"], gps_info["GPSLongitudeRef"])
        if "GPSDateStamp" in gps_info and "GPSTimeStamp" in gps_info:
            gps_time = datetime.datetime.strptime(gps_info['GPSDateStamp'], '%Y:%m:%d').replace(
                hour=int(gps_info['GPSTimeStamp'][0]),
                minute=int(gps_info['GPSTimeStamp'][1]),
                second=int(gps_info['GPSTimeStamp'][2])
            )
    return (lat, long, gps_time)

def get_decimal_from_dms(dms, ref):
    degrees = float(dms[0])
    minutes = float(dms[1]) / 60.0
    seconds = float(dms[2]) / 3600.0
    decimal_degrees = round(degrees + minutes + seconds, 6)

    if ref in ['S', 'W']:
        decimal_degrees *= -1
    return decimal_degrees

if __name__ == '__main__':
    process_image()

 

 

 

 

0 Kudos
1 Reply
IanTodd
Occasional Contributor

On first glance, it looks like you're trying to reference a layer, which PIL wouldn't know how to open. In the block "In [6]", you can see how it loops through the attachments downloads the image to a local directory. Once you have the JPEG locally, Image.open can read the data.