Select to view content in your preferred language

How to rename attachment based on field name?

189
1
07-29-2024 01:59 AM
Labels (1)
RikiChan
Emerging Contributor

Hi, everyone. I am new to using ArcGIS Notebook and I would like to automate the workflow for my fieldwork operations. Specifically, I would like to have the workers take photos using ArcGIS Fieldmaps, and then use ArcGIS Notebook to insert a date watermark into those photos. Finally, I would like to zip the photos into a file.

The part I'm stuck on is renaming the photo attachments. I would like to rename them based on a field name, such as "TreeID". Could you please provide some guidance on how I can accomplish this workflow in an efficient manner? I'm happy to provide any additional details that might be helpful. Thank you in advance for your assistance.

import datetime
import os
import zipfile
from arcgis.gis import GIS
from PIL import Image
from PIL import ExifTags
from PIL import ImageDraw, ImageFont
from PIL.ExifTags import GPSTAGS, TAGS

'''
https://github.com/Esri/field-maps-scripts/blob/main/notebooks/Generate%20PDF%20Report/Generate%20PD...
'''
def format_datetime(original_datetime):
# Split the original datetime into date and time components
date, time = original_datetime.split()

# Change the date format from yyyy:mm:dd to yyyy-mm-dd
formatted_date = date.replace(':', '-')

return f"{formatted_date}\n{time}"

def add_date_watermark(image_path):
image = Image.open(image_path)
exif = image.getexif()
watermark_text = None

gps_info = {}
draw = ImageDraw.Draw(image)
watermark_text = exif[306]
print ("success")

if hasattr(image, '_getexif'):
exif = image._getexif()
if exif is not None:
orientation = exif.get(274, 1) # Get the orientation tag, default to 1 if missing
rotations = {
3: Image.ROTATE_180,
6: Image.ROTATE_270,
8: Image.ROTATE_90
}
if orientation in rotations:
image = image.transpose(rotations[orientation])

print ("watermark")
print (watermark_text)


# Get the original datetime from EXIF
original_datetime = exif.get(306, "No date found")

# Format the datetime as per the new requirement
formatted_datetime = format_datetime(original_datetime)


draw = ImageDraw.Draw(image)

# Specify the font size and use a system font directly
font = ImageFont.load_default()

# Increase the font size
font = font.font_variant(size=36)

# Create the updated watermark text with formatted datetime
updated_watermark_text = f"{formatted_datetime}"

draw.text((720, 1150), updated_watermark_text, fill=(255, 104, 32), font=font)

# Save the processed image to a temporary directory
save_path = r'/arcgis/home/watermark2/' + os.path.basename(image_path)
image.save(save_path)

return save_path

# Process images and save them to a zip file
processed_images = []
gis = GIS('home')


layer_item_id = "86fxxxxxxxxxxxxxxxxx469bbb08"
layer_item = gis.content.get(layer_item_id)
layer = layer_item.layers[0]


attachments_list = layer.attachments.search(attachment_types=['image/jpeg'])
oid_list = layer.query(attachment_types=['image/jpeg'], return_ids_only=True)['objectIds']
attachments = layer.attachments.download(oid=oid_list, save_path=r'/arcgis/home/watermark2/')

for attach in attachments:
processed_image_path = add_date_watermark(attach)
processed_images.append(processed_image_path)

# Create a zip file containing all processed images
zip_filename = r'/arcgis/home/watermark2/processed_images.zip'
with zipfile.ZipFile(zip_filename, 'w') as zipf:
for img in processed_images:
zipf.write(img, os.path.basename(img))

print("Images processed and saved as a zip file.")

0 Kudos
1 Reply
NicholasGiner1
Esri Contributor
0 Kudos