|
POST
|
Thanks to GeoNet, here's the work around I eventually came up with seeing that using Python to update a raster field isn't supported. In the end I didn't use a raster field at all. I had a text field with a unique ID that matched the file name of JPGs in a directory. I used a script that updated another text field named 'Photo' with the full name of the JPG. Then I used arcpy.EnableAttachments_management(), which created a related table that can store rasters. Finally, I used arcpy.AddAttachments_management(), which populated the related table. Working with attachments was also helpful. '''the purpose of this script is to update the 'Photo' field in the fc
with the file path of the same name.
'''
import arcpy, os
ws = r'path\to\Projects\DOT_SignInventory\pics'
jpgs = {} # empty dictionary
for dirName, subdirList, fileList in os.walk(ws):
for fname in fileList:
jpgs[fname[:-4]] = os.path.join(dirName,fname)
for jpg in jpgs:
print(jpg)
fc = r'path\to\Projects\DOT_SignInventory\Data2.gdb\WCDOT_Signs'
fields = ['OID@', 'Post_Number', 'Photo']
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
try:
row[2] = jpgs[row[1]]
cursor.updateRow(row)
except KeyError:
print("No photo for: {}".format(row[1]))
del cursor
... View more
04-24-2019
08:06 AM
|
0
|
0
|
3725
|
|
POST
|
Hello, The intention of this script is to update the 'Photo' field of a feature class table with the full path of a JPG based on the 'Post_Number' field. The problem is only the first seven digits of the JPG matches the unique ID in the 'Post_Number' field. The rest of the file name is a date. I've placed a string index number variable in a for loop that gets the JPG by the first seven digits. A dictionary is then built off the existing paths without the date. The cursor is then supposed to update the 'Photo' field. The script runs without error but the Photo field isn't updated?? I'm thinking the 'var' variable is in the wrong place. '''Python 3.6.6. The purpose of this script is to update the 'Photo' field in the fc
with the full path of a JPG.
'''
import arcpy, os
ws = r'path\to\Projects\DOT_SignInventory\pics'
jpgs = {} # empty dictionary
for dirName, subdirList, fileList in os.walk(ws):
for fname in fileList:
var = fname[:7] # get first seven digits of file name
jpgs[var[:-4]] = os.path.join(dirName,var)
fc = r'path\to\Projects\DOT_SignInventory\Data2.gdb\WCDOT_Signs'
fields = ['OID@', 'Post_Number', 'Photo'] # fields of FC
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
try:
row[2] = jpgs[row[1]]
cursor.updateRow(row)
except KeyError:
print("No photo for: {}".format(row[1]))
del cursor
Here's how the JPG looks in the directory: Here's how I want the Photo field to be updated. Right now it's empty. Both fields are text fields.
... View more
04-23-2019
01:32 PM
|
0
|
1
|
1383
|
|
POST
|
Thanks, that's it. Here's how I put it in: for dirName, subdirList, fileList in os.walk(ws):
for fname in fileList:
var = fname[:7]
jpgs[var[:-4]] = os.path.join(dirName,var) The script runs without error, but now I have to figure out why the Photo field isn't being populated.
... View more
04-23-2019
09:33 AM
|
0
|
0
|
1551
|
|
POST
|
I have a working script that updates the 'photo' field with a file path to a JPG based on the 'post_number' field. In other words, the script searches the directory for JPGs which it puts into a dictionary with it's file path. It then uses a cursor to update the photo field with a path to the JPG. import arcpy, os, re
ws = r'path\to\Projects\DOT_SignInventory'
jpgs = {} # empty dictionary
for dirName, subdirList, fileList in os.walk(ws):
for fname in fileList:
jpgs[fname[:-4]] = os.path.join(dirName,fname)
fc = r'path\to\Projects\DOT_SignInventory\Data.gdb\WCDOT_Signs'
editws = os.path.dirname(fc)
fields = ['OID@', 'Post_Number', 'Photo']
# Start an edit session. Must provide the workspace.
edit = arcpy.da.Editor(editws)
# Edit session is started without an undo/redo stack for versioned data
# (for second argument, use False for unversioned data)
edit.startEditing(False, True)
# Start an edit operation
edit.startOperation()
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
try:
row[2] = jpgs[row[1]]
cursor.updateRow(row)
except KeyError:
print("No photo for: {}".format(row[1]))
del cursor
# Stop the edit operation
edit.stopOperation()
# Stop the edit session and save the changes
edit.stopEditing(True)
It works fine now because the 'Post_Number' field is the same as the JPG: My problem is that the JPG will soon have a date concatenated to look like this, for example: 0110100_20180607. So, my question is how can I change the code so it uses only those first seven digits to populate the Photo field? They are both text fields.
... View more
04-22-2019
02:12 PM
|
0
|
3
|
1650
|
|
POST
|
Thanks a lot! I did put the image.close() line in at one point. But, I didn't dedent it as you have. I also never dedented os.rename().
... View more
04-16-2019
02:25 PM
|
0
|
0
|
6074
|
|
POST
|
Tried both options, and also tried '%Y%m%d'. Still throwing the PermissionError. Here's what I have: import arcpy, sys
import exifread
from exifread import exif
import os
import time
from datetime import datetime
im = "path\to\DOT_SignInventory"
for root, dirnames, filenames in os.walk(im): #iterate directory
for fname in filenames:
if fname.endswith('.JPG'):
with open(os.path.join(root, fname), 'rb') as image: #file path and name
exif = exifread.process_file(image)
dt = str(exif['EXIF DateTimeOriginal']) #get 'Date Taken' from JPG
ds = time.strptime(dt, '%Y:%m:%d %H:%M:%S')
nt = time.strftime("%Y-%m-%d",ds)
newname = fname[0:7] + "_" + nt + ".jpg"
os.rename(os.path.join(root,fname), newname)
... View more
04-16-2019
01:17 PM
|
0
|
2
|
6074
|
|
POST
|
Randy, I tried: newname = fname[0:7] + "_" + nt + ".jpg"
os.rename(os.path.join(root,fname), os.path.join(root,newname)) Even within the with loop it's throwing a Permission Error: PermissionError: [WinError 32] The process cannot access the file
because it is being used by another process:
'path\\to\\DOT_SignInventory\\CH01\\0100010.JPG' ->
'path\\to\\DOT_SignInventory\\CH01\\0100010_04/27/2018.jpg'
... View more
04-16-2019
12:32 PM
|
0
|
4
|
6074
|
|
POST
|
I need some help putting the date in the filenames. Here's the working code I have so far. import arcpy, sys
import exifread
from exifread import exif
import os
import time
from datetime import datetime
im = r"path\to\DOT_SignInventory"
for root, dirnames, filenames in os.walk(im): #iterate directory
for fname in filenames:
if fname.endswith('.JPG'):
with open(os.path.join(root, fname), 'rb') as image: #file path and name
exif = exifread.process_file(image)
dt = str(exif['EXIF DateTimeOriginal']) #get 'Date Taken' from JPG
ds = time.strptime(dt, '%Y:%m:%d %H:%M:%S')
nt = time.strftime("%m/%d/%Y",ds)#variable with 'Date Taken'
print("Photo:{} Date Taken:{}".format(fname, "_" + nt)) I've tried these couple things after the print statement, but no cigar: new_file = fname + nt + ".jpg" f = open(fname.format(nt), "w")
f.write(fname + nt)
f.close
... View more
04-16-2019
09:05 AM
|
0
|
7
|
6418
|
|
POST
|
Randy, Thanks, that helped. As it stands, it only reads the first JPG, which has the date 04/27/2018. It isn't printing the dates of the numerous other files? im = r"path\to\DOT_SignInventory"
for root, dirnames, filenames in os.walk(im):
for fname in filenames:
if fname.endswith('.JPG'):
with open(os.path.join(root, fname), 'rb') as image: #file path and name
exif = exifread.process_file(image)
dt = str(exif['EXIF DateTimeOriginal'])#get JPG 'Date taken'
st = '2018:04:27 09:30:59'
date = time.strptime(st, '%Y:%m:%d %H:%M:%S')
print(time.strftime("%m/%d/%Y",date)) 04/27/2018
04/27/2018
04/27/2018
04/27/2018
04/27/2018
04/27/2018
etc... It seems the first part of the code involving exif (up to line 😎 works fine. If I put a print statement after line 8: print(dt) it gives me: 2018:04:27 09:30:59
2018:04:27 09:36:30
2018:04:27 09:40:00
2018:04:27 09:41:03
2018:04:27 09:42:03
2018:04:27 09:47:00
etc...
... View more
04-12-2019
09:07 AM
|
0
|
1
|
4123
|
|
POST
|
I'm attempting to extract the Date Taken from jpgs within a directory. And then I want to have the date printed as such: mo/day/year Here's an example of a JPG's Properties menu: This code runs with no error but the output isn't what I want. import exifread
from exifread import exif
import os
import time
im = r"path\to\DOT_SignInventory"
for root, dirnames, filenames in os.walk(im):
for fname in filenames:
if fname.endswith('.JPG'):
with open(os.path.join(root, fname), 'rb') as image: #file path and name
exif = exifread.process_file(image)
dt = str(exif['EXIF DateTimeOriginal']) #get Date Taken from JPG
date = time.strptime(dt, '%Y:%m:%d %H:%M:%S')
print("Photo:{} Date Taken:{}".format(fname, date)) Photo:0710100.JPG Date Taken:time.struct_time(tm_year=2018, tm_mon=6, tm_mday=5, tm_hour=13, tm_min=49, tm_sec=40, tm_wday=4, tm_yday=166, tm_isdst=-1) Ideally, I want the output to look as such: Photo:0710100.JPG Date Taken:06/05/2018 For line 14, I can't find the right format. I've also tried the datetime module but couldn't get that to work right either.
... View more
04-11-2019
02:08 PM
|
0
|
3
|
4276
|
|
IDEA
|
I have a directory full of JPGs which need to go in a raster field of a feature class table. You can do this manually by adding raster datasets as attributes in a feature class. But I guess there is no automated way to do it, and automation speaks for itself. Using a cursor to do this is not supported.
... View more
03-27-2019
01:41 PM
|
1
|
3
|
1443
|
|
POST
|
For what it's worth, it looks like I can convert the rasters to a FGDB. arcpy.env.workspace = r'\pathTo\jpgs'
#Convert Multiple Raster Dataset to FGDB
arcpy.RasterToGeodatabase_conversion("0100030.jpg;0100040.jpg", r"\pathTo\Data.gdb") Of course, this is just a sample. I'll have to set the script up to iterate through the directory and convert thousands of jpgs to the FGDB. But, after I do that I'm still stuck wondering how to get the raster into the Raster Field via Python.
... View more
03-26-2019
12:09 PM
|
0
|
0
|
3725
|
|
POST
|
Any intent I did with code to load the raster resulted in an error. Since I could not find any documentation on doing this in Python I assume it is not supported. Xander, Is this supported in arcpy for ArcGIS Pro? I'm currently looking for a way to update raster fields in FC table with Python. I have a script that uses the updatecursor to populate a text field with a path to the directory where the rasters are stored. But, what I really need is to update the raster field with the image itself. And apparently you can't use cursors for this. I started a thread the other day: https://community.esri.com/thread/231019-raster-fields-not-supported-using-cursors
... View more
03-26-2019
08:46 AM
|
0
|
0
|
3370
|
|
POST
|
Dan, Adding raster datasets as attributes is exactly what I need. But is there a way to automate this? I'm dealing with 1000s of rasters. I started out on this page and because there was no mention of Arcpy I moved on to looking into cursors.
... View more
03-26-2019
06:41 AM
|
0
|
0
|
3725
|
|
POST
|
So creating a separate table (using an insertcursor) for joining isn't a possibility? How to use an insertcursor on a Raster Field is pretty much my question. There's a Raster Field in a FC that I want populated with rasters. But, as I was reading how to access that type of field with a cursor it says it's not possible:
... View more
03-25-2019
01:44 PM
|
1
|
2
|
3725
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 06-25-2026 12:25 PM | |
| 1 | 05-04-2026 08:45 AM | |
| 1 | 04-20-2026 01:20 PM | |
| 1 | 07-24-2025 01:27 PM | |
| 1 | 11-13-2025 08:22 AM |
| Online Status |
Offline
|
| Date Last Visited |
Thursday
|