Skip to content

Instantly share code, notes, and snippets.

@glw
Last active October 5, 2021 21:39
Show Gist options
  • Save glw/dc0c5485cfcbf4870b5dd6d5a88ff950 to your computer and use it in GitHub Desktop.
Save glw/dc0c5485cfcbf4870b5dd6d5a88ff950 to your computer and use it in GitHub Desktop.
Python files to Export Image attachments from AGOL FGDB and, create a CSV file from a directory of those images (only works for JPG files for now)
# Create CSV from a list of files in a given directory. It is the companion file of export_photos.py. It doesnt take into account multiple photos associated with the same ID. Meant to be used with "export_photos.py"
import csv
import os
import argparse
import itertools
parser = argparse.ArgumentParser()
parser.add_argument('-i','--input folder', type=str, required=True, help='path location of images')
parser.add_argument('-o','--output folder', type=str, help='path location for csv output, leave blank if same as input.')
args = vars(parser.parse_args())
# input
image_path = args['input folder']
# output
output_folder = args['output folder']
csv_output_filename = 'image_list.csv'
final_output = os.path.join(output_folder,csv_output_filename)
# list of images
list_images = os.listdir(image_path)
def list_of_lists(filelst):
"""creat a nested list from list of files with oringal name an new cleaned name"""
# remove extention
rmsuffix = [f.removesuffix('.jpg') for f in filelst]
# remove brackets
rmfstlast = [s.lstrip('{').rstrip('}') for s in rmsuffix]
# put extention back
clean_filelst = [l + '.jpg' for l in rmfstlast]
# create list of local paths to images
full_path = [[os.path.join(image_path, p)] for p in filelst]
# create nested list for original names
oringal_list_of_imagenames = [[t] for t in filelst]
# create nested of list clean names
cleaned_list_of_imagenames = [[z] for z in clean_filelst]
# zip both lists together
combinelst = [list(itertools.chain(*i)) for i in zip(oringal_list_of_imagenames, cleaned_list_of_imagenames, full_path)]
return combinelst
# column headers
header = ['original_image_name', 'image_name_clean', 'image_path']
# create file
with open(final_output, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
for i in list_of_lists(list_images):
writer.writerow(i)
# This file is the same as create_csv_from_image_dir.py except that it also renames the files it finds in the directory
import csv
import os
import argparse
import itertools
parser = argparse.ArgumentParser()
parser.add_argument('-i','--input folder', type=str, required=True, help='path location of images')
parser.add_argument('-o','--output folder', type=str, help='path location for csv output, leave blank if same as input.')
args = vars(parser.parse_args())
# input
image_path = args['input folder']
os.chdir(image_path)
# output
output_folder = args['output folder']
csv_output_filename = 'image_list.csv'
final_output = os.path.join(output_folder,csv_output_filename)
# list of images
list_images = os.listdir(image_path)
def list_of_lists(filelst):
"""creat a nested list from list of files with oringal name an new cleaned name"""
newfilelst = []
# remove image ext and brackets, then re-add ext. Finally rename files.
for f in filelst:
newname = f.removesuffix('.jpg').lstrip('{').rstrip('}')+ '.jpg'
os.rename(f,newname)
newfilelst.append(newname)
# create list of local paths to images
full_path = [[os.path.join(image_path, p)] for p in newfilelst]
# create nested list for original names
oringal_list_of_imagenames = [[t] for t in filelst]
# create nested of list clean names
cleaned_list_of_imagenames = [[z] for z in newfilelst]
# zip both lists together
combinelst = [list(itertools.chain(*i)) for i in zip(oringal_list_of_imagenames, cleaned_list_of_imagenames, full_path)]
return combinelst
# column headers
header = ['original_image_name', 'image_name_clean', 'image_path']
# create file
with open(final_output, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(header)
for i in list_of_lists(list_images):
writer.writerow(i)
# Source: https://support.esri.com/en/Technical-Article/000017450
# ESRI license is required to run this. This is meant to be set up as a tool in a toolbox. Follow instructions in the source article.
import arcpy
from arcpy import da
import os
inTable = arcpy.GetParameterAsText(0)
fileLocation = arcpy.GetParameterAsText(1)
with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID', 'REL_GLOBALID']) as cursor:
for item in cursor:
attachment = item[0]
filename = str(item[3]) + '.jpg'
open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
del item
del filename
del attachment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment