foto hernoemen naar datum waarop de foto is gemaakt (exif)

749
11
08-24-2018 01:41 PM
G_A_Venhorst
New Contributor II

Op Arcgis online worden foto's gemaakt via de collector bij diverse objecten.

Men wil nu de mogelijkheid hebben om de foto's via ArcgisPro te hernoemen naar de datum(en tijd) waarop de foto's zijn gemaakt (info staat in Exif bij de foto)

Is dit via Python te doen?

Tags (1)
0 Kudos
11 Replies
RandyBurton
MVP Regular Contributor

Approximate translation via google:

rename the photo to the date the photo was taken (exif)

On Arcgis online photos are taken via the collector at various objects. They now want to be able to rename the photos via ArcgisPro to the date (and time) on which the photos were taken (info is in Exif on the photo) Is this possible via Python?

0 Kudos
RandyBurton
MVP Regular Contributor

I have read some comments about the removal of exif data by Collector, but that was a while ago. Can you check whether the exif data is intact when you work with the photo in Pro? If that is the case, there must be a way to rename the file using python and exif data.

Via google:

Ik heb wat opmerkingen gelezen over het verwijderen van exif-gegevens door Collector, maar dat was een tijdje geleden. Kun je controleren of de exif-gegevens intact zijn als je met de foto in Pro werkt? Als dat het geval is, moet er een manier zijn om het bestand te hernoemen met behulp van python- en exif-gegevens.

0 Kudos
G_A_Venhorst
New Contributor II

Ongeveer 80% van de foto's hebben hun exif data nog beschikbaar.

Kan je de Foto_ATTACH tabel ook bekijken als de data nog online staat?

Of kan je ook een batch edit doen op een selectie objecten waarvan je de foto's wil aanpassen.

Er zijn meer dan 300 objecten, de meesten hebben 6 of meer foto's. Het is een inspectie over meer dan 3 jaar.

Bij elke incpectieronde moeten er nieuwe foto's worden gemaakt.

Echter is er nu niet meer te zien wanneer deze foto's zijn gemaakt.

De database is te groot om eerst locaal te zetten, dan aanpassen en vervolgens weer online te zetten vermoed ik.

Approximately 80% of the photos still have their exif data available.

Can you also view the Foto_ATTACH table if the data is still online?

Or you can also do a batch edit on a selection of objects for which you want to adjust the photos.

There are more than 300 objects, most of them have 6 or more photos. It is an inspection over more than 3 years.

New photographs have to be taken at each round of incidence.

However, it is now no longer possible to see when these photos were taken.

The database is too large to first set up locally, then modify and then put online again I suspect.

RandyBurton
MVP Regular Contributor

You might try the exifread module at https://pypi.org/project/ExifRead/.  Sample code would be like:

import os, glob

import exifread
# info on exifread at https://pypi.org/project/ExifRead/

path = r'C:\Path\to\photo\folder'

for filename in glob.glob(os.path.join(path, '*.jpg')):
    print filename
    f = open(filename, 'rb') # open read only, binary
    tags = exifread.process_file(f)
    f.close()
    print tags['Image DateTime']
    newname = os.path.join(path, '{}.jpg'.format(str(tags['Image DateTime']).replace(':','-').replace(' ','_')))
    os.rename(filename, newname) # assumes that all date/times are different, otherwise an error will occur‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You would need to add some code to deal with errors such as missing exif tags and possible duplicate dates.  You may also want to adjust the date/time format in your file name, perhaps appending it to the original filename.

Another option to read exif is with the pillow module; information at https://github.com/python-pillow/Pillow.

You may also want to look at two arcpy tools at An overview of the Photos toolset. These tools can assist in matching photos to points or place them on a map using exif data.

I will try to answer your questions about working with photos that are still online.  Are you using your own server portal or are you using AGOL?

G_A_Venhorst
New Contributor II

Hi,

Yes the photo are online in AGOL.

We do not have a server.

0 Kudos
RandyBurton
MVP Regular Contributor

You can see most of (perhaps all of) the information in your attachment table Foto__ATTACH.  I have yet to find a way to edit the attachment table.

To view the information, log into your AGOL account.  At the top of the page select "Content".  On the next page, under "Layers", select the layer that has your attachments.

This will take you to the REST Services Directory page for your layer.  At the bottom of the page, click the link "Query Attachments".  (This link is only available if the layer has attachments.)

The form will look like the following image. In the box "Definition Expression" enter "OBJECTID > 0".  This will basically create a where clause: "where the OBJECTID of the parent table is greater than zero" and select all attachments.  You can limit the attachment types to "image/jpeg" if there are other types of attachments you

do not need.

Query Attachments

You should get links for the selected images which you can click and save the target.  It appears that all photos are renamed to "attachment##" when Collector attaches them to the parent table. I do not know if this field can be edited.  You should see the information in the attachment table for each photo, but it will not be in a table format.  ID is the number of each row in the table; and there is a global ID for the attachment.  You can also see the parent's ObjectID and GlobalID.


The above process can be scripted in Python, although I have not done it yet.  For additional information see this section in the REST API:  Query Attachments (Feature Service/Layer).

If you are wanting to export all photos and rename them,  it may be easier to click "Export Data" option on the feature's page that shows the layers.  You can save it as a file geodatabase (zipped) and work with it in

desktop.  This way you can work with the Foto_ATTACH table directly.  I have used the following script to export and rename jpeg's from the related attachment table.

import arcpy, os, exifread

masterFC = r'C:\path\to\file.gdb\parent'
masterFlds = ['GlobalID', 'OBJECTID', 'Note'] # can grab a short text field for use if needed
# Use list comprehension to build a dictionary from a da SearchCursor  
masterDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(masterFC, masterFlds)}

# print masterDict

relatedTbl = r'C:\path\to\file.gdb\photo__ATTACH' # note 2 underscores in table name
relatedFlds = ['REL_GLOBALID', 'ATTACHMENTID', 'DATA', 'CONTENT_TYPE']

fileLocation = r'C:\path\to\attachments\folder'

with arcpy.da.SearchCursor(relatedTbl, relatedFlds) as cursor:
    for item in cursor:
        if item[3] == 'image/jpeg': # process only images

            # item[0] is related GlobalID; take first item in masterDict tuple with that key
            f1 = masterDict[item[0]][0] # this is the ObjectID of parent record
            f2 = item[1] # this is the Attachment ID
            # make new filename 
            filename = "ATT_{}_{}".format(f1, f2)
            # print filename
            # we need to write to temporary file for exifread to work
            tempname = os.path.join(fileLocation, '{}.jpg'.format(filename))
            f = open(tempname, 'wb').write(item[2].tobytes())
            f = open(tempname, 'rb')
            tags = exifread.process_file(f) # has to be a file to process
            f.close()
            if tags['Image DateTime'] is not None: # may need to also check length in case of error
                theDate = str(tags['Image DateTime']).split(' ')[0].replace(':','')
                newname = os.path.join(fileLocation, "{}_{}.jpg".format(filename, theDate))
                print "ObjectID: {} \tNotes: {}  \tFile: {}".format(f1, masterDict[item[0]][1], newname)
                os.rename(tempname, newname) # should have unique name by using IDs from both parent and child table
            else: # keep the temporary name
                print "ObjectID: {} \tNotes: {}  \tFile: {}".format(f1, masterDict[item[0]][1], tempname)

del  cursor
del item‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

For my test, the results were:

ObjectID: 263   Notes: attachment test   File: C:\attachments\ATT_263_1_20170910.jpg
ObjectID: 263   Notes: attachment test   File: C:\attachments\ATT_263_2_20170910.jpg
ObjectID: 264   Notes: attach test        File: C:\attachments\ATT_264_3_20170627.jpg

Please note that I have not worked with missing exif information so I don't know how exifread will respond.  It should be possible to include a few lines of code to use the attachment's ID to change the filename in the attachment table.

G_A_Venhorst
New Contributor II

Hi Randy,

Thanks for all info.

The problem is that the data and the photos must remain online, the inspection continues.

The idea I have now is to edit the data via a replica and put it back online.

I thought about creating an extra field in the Foto_ATTACH table. (Date, txt field).

Then to be filled via field calculator from the exif info.

If that would work I would have been very helped, so this field could be used to rename the photo.

Then I could put the data back online.

0 Kudos
G_A_Venhorst
New Contributor II

Hoi Randy,

Is it possible to use the exif data in the popup info screen of the object?

If the name of the photo is shown in the popup, followed by the year on which it was made, I would have been very helped.

Renaming would then be less important.

Unfortunately, there is no create date in the attachment table.

0 Kudos
RandyBurton
MVP Regular Contributor

If there is date information in the parent record, it can be used to rename the photo.  Is this what you were thinking?  Perhaps you can attach a screen shot of a popup so I understand what you want to do. Also, can an individual feature have multiple photos over several years attached to it?

I have experimented with Collector and found that the current version allows you to rename attached photos: Work with attachments.  This seems to work by downloading the photo to Collector, renaming the photo, and then uploading it to the same spot in the attachment file.

I found the Update Attachment page in the REST API reference.  When used with the Query Attachments (Feature Service/Layer), it should be possible to develop a script to rename the photos.  The workflow would be something like:

  1. Log into your account and get a token
  2. Verify that the feature is set up for attachments
  3. Use query attachments to get a list of all the photos in the feature
  4. Working through the list, download each photo, examine the exif data and extract the date
  5. If a date is found, use it to rename the photo file and use update attachment to upload the renamed file to AGOL (the attachment name should pick up the new name)
  6. If no date is found in the exif data, use data from the parent table if possible to date and rename the photo.
0 Kudos