|
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
|
1513
|
|
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
|
1612
|
|
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
|
5996
|
|
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
|
5996
|
|
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
|
5996
|
|
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
|
6340
|
|
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
|
4048
|
|
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
|
4201
|
|
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
|
1424
|
|
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
|
3660
|
|
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
|
3341
|
|
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
|
3660
|
|
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
|
3660
|
|
POST
|
How do I update a Raster Field using a cursor? I'm attempting to write a script that uses a cursor to update this field with rasters from a directory. It's a stand-alone script using arcpy for ArcGIS Pro 2.3.1. arcpy.da.InsertCursor, arcpy.da.SearchCursor and arcpy.da.UpdateCursor state that as a field_name, "Raster fields are not supported."
... View more
03-25-2019
01:17 PM
|
0
|
7
|
4022
|
|
POST
|
I couldn't get the os.walk portion of the code you provided to get to the jpgs. If I ran that portion of the script to test it (left out the cursor portion) it would iterate a folder in that same directory that had a jpg, but then stop there. It didn't find any other jpgs. So I changed those lines to this: ws = r'C:\path\to\jpegs\root'
jpgs = {} # empty dictionary
for dirName, subdirList, fileList in os.walk(ws):
for fname in fileList:
jpgs[fname[:-4]] = os.path.join(dirName,fname) Anyway, thanks to all of your help this does what I want: import arcpy, os
ws = r'C:\path\to\jpegs\root'
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'pathto\Projects\DOT_SignInventory\Data.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
03-21-2019
01:52 PM
|
1
|
0
|
690
|
| 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 |
Friday
|