|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import re |
| 4 | +import csv |
| 5 | + |
| 6 | +import JPEG |
| 7 | +import MapURLs |
| 8 | + |
| 9 | +def getCSVHeader() : |
| 10 | + return ["Filename", "Size (bytes)", "Make", "Model", "Software", "Timestamp", "Columns", "Rows", "Latitude", "Longitude", "Altitude (m)", "FromGPS", |
| 11 | + "OSMaps URL", "Google Maps URL", "Google Street View URL" ] |
| 12 | + |
| 13 | +def processJpegFile(dirName, jpegFileName) : |
| 14 | + fullPath = dirName + "\\" + jpegFileName |
| 15 | + |
| 16 | + try : |
| 17 | + p = JPEG.processFile(fullPath) |
| 18 | + except Exception as e : |
| 19 | + print("Exception processing JPEG file:", fullPath, " : ", e, file=sys.stderr) |
| 20 | + p = "" |
| 21 | + |
| 22 | + l = [] |
| 23 | + l.append(p['filename']) |
| 24 | + l.append(p['bytes']) |
| 25 | + l.append(p['make'] if 'make' in p else '') |
| 26 | + l.append(p['model'] if 'model' in p else '') |
| 27 | + l.append(p['software'] if 'software' in p else '') |
| 28 | + l.append(p['timestamp'] if 'timestamp' in p else '') # NB Excel will automatically convert / display this as its own date/time format |
| 29 | + l.append(p['columns'] if 'columns' in p else '') |
| 30 | + l.append(p['rows'] if 'rows' in p else '') |
| 31 | + l.append(p['latitude'] if 'latitude' in p else '') |
| 32 | + l.append(p['longitude'] if 'longitude' in p else '') |
| 33 | + l.append(p['altitude'] if 'altitude' in p else '') |
| 34 | + fromGPS = '' |
| 35 | + if 'latitude' in p : |
| 36 | + fromGPS = 'Y' if p['fromGPS'] else 'N' |
| 37 | + l.append(fromGPS) |
| 38 | + |
| 39 | + if 'latitude' in p and 'longitude' in p: |
| 40 | + zoomLevel = 16 |
| 41 | + l.append(MapURLs.urlForOrdnanceSurveyMaps(p['latitude'], p['longitude'], zoomLevel)) |
| 42 | + l.append(MapURLs.urlForGoogleMaps(p['latitude'], p['longitude'], zoomLevel)) |
| 43 | + l.append(MapURLs.urlForGoogleMapsStreetView(p['latitude'], p['longitude'])) |
| 44 | + |
| 45 | + # Convert to a CSV line for output |
| 46 | + return str(p), l |
| 47 | + |
| 48 | +# Only deal with files with a .jpeg or .jpeg file extension |
| 49 | +p = re.compile(r"^.*\.jpe?g$", re.IGNORECASE) |
| 50 | +def isJpegName(n) : |
| 51 | + return p.match(n) |
| 52 | + |
| 53 | +# Extract a list of (directory-path, filename) tuples for all the JPEG files under |
| 54 | +# this directory, recursing into sub-directories. |
| 55 | +def processDirectory(topdir) : |
| 56 | + outputList = [] |
| 57 | + |
| 58 | + # os.walk returns a generator object which produces a tuple for each directory |
| 59 | + # in the filesystem tree beneath (and including) our top-level directory: |
| 60 | + # - directory name |
| 61 | + # - list of sub-directories directly under this directory |
| 62 | + # - list of files in this directory |
| 63 | + |
| 64 | + for dirpath, dirnamesList, filenamesList in os.walk(topdir) : |
| 65 | + outputList.extend([(dirpath, name) for name in filenamesList if isJpegName(name)]) |
| 66 | + |
| 67 | + return outputList |
| 68 | + |
| 69 | + |
| 70 | +# |
| 71 | +#################################### |
| 72 | +# |
| 73 | + |
| 74 | +def main(location) : |
| 75 | + |
| 76 | + if os.path.isdir(location) : |
| 77 | + jpegFilesList = processDirectory(location) |
| 78 | + elif os.path.isfile(location) : |
| 79 | + dirname, filename = os.path.split(location) |
| 80 | + if isJpegName(filename) : |
| 81 | + jpegFilesList = [(dirname, filename)] |
| 82 | + else : |
| 83 | + print('*** ', location, " is not a JPEG file name") |
| 84 | + exit() |
| 85 | + else : |
| 86 | + print('*** ', location, " is not a file or directory name") |
| 87 | + exit() |
| 88 | + |
| 89 | + print("Found", len(jpegFilesList), "JPEG file(s) to process under", location) |
| 90 | + |
| 91 | + CSVFileName = "JPEGs.csv" |
| 92 | + with open(CSVFileName, "w", newline="") as csvfile: |
| 93 | + myCSVWriter = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) |
| 94 | + csvHeader = getCSVHeader() |
| 95 | + myCSVWriter.writerow(csvHeader) |
| 96 | + n = 0 |
| 97 | + for dirName, jpegFileName in jpegFilesList : |
| 98 | + n += 1 |
| 99 | + (dict, summaryList) = processJpegFile(dirName, jpegFileName) |
| 100 | + myCSVWriter.writerow(summaryList) |
| 101 | + if n % 10 == 0 : |
| 102 | + print(" .. ", n, "/", len(jpegFilesList), " .. ", dirName, jpegFileName) |
| 103 | + |
| 104 | + print("Produced CSV file:", CSVFileName) |
| 105 | +# |
| 106 | +#################################### |
| 107 | +# |
| 108 | + |
| 109 | +if __name__ == "__main__" : |
| 110 | + |
| 111 | + if len(sys.argv) == 1 : |
| 112 | + print("No directory/file name command line argument provided") |
| 113 | + exit() |
| 114 | + |
| 115 | + location = sys.argv[1] |
| 116 | + main(location) |
0 commit comments