Created
May 3, 2020 20:09
-
-
Save paul121/408dacd4f87a6967257d2bc303148d6e to your computer and use it in GitHub Desktop.
Create a farmOS Observation log with Timestamp and Geometry from a JPEG image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import base64 | |
| from datetime import datetime | |
| from farmOS import farmOS | |
| from PIL import Image | |
| from PIL.ExifTags import TAGS, GPSTAGS | |
| hostname = "http://localhost" | |
| username = "username" | |
| password = "password" | |
| image_filepath = "test_img.jpg" | |
| # GPSInfo code derived from: | |
| # https://www.sylvaindurand.org/gps-data-from-photos-with-python/ | |
| def get_exif(filename): | |
| exif = Image.open(filename).getexif() | |
| if exif is not None: | |
| for key, value in exif.items(): | |
| name = TAGS.get(key, key) | |
| data = exif.pop(key) | |
| exif[name] = data | |
| if 'GPSInfo' in exif: | |
| for key in exif['GPSInfo'].keys(): | |
| name = GPSTAGS.get(key,key) | |
| exif['GPSInfo'][name] = exif['GPSInfo'].pop(key) | |
| return exif | |
| def get_decimal_coordinates(info): | |
| for key in ['Latitude', 'Longitude']: | |
| if 'GPS'+key in info and 'GPS'+key+'Ref' in info: | |
| e = info['GPS'+key] | |
| ref = info['GPS'+key+'Ref'] | |
| info[key] = ( e[0][0]/e[0][1] + | |
| e[1][0]/e[1][1] / 60 + | |
| e[2][0]/e[2][1] / 3600 | |
| ) * (-1 if ref in ['S','W'] else 1) | |
| if 'Latitude' in info and 'Longitude' in info: | |
| return [info['Longitude'], info['Latitude']] | |
| exif = get_exif(image_filepath) | |
| log = { | |
| "name": "My Photo Observation", | |
| "type": "farm_observation", | |
| "done": 1, | |
| } | |
| # Read GPSInfo from image data. | |
| if exif is not None and 'GPSInfo' in exif: | |
| latlong = get_decimal_coordinates(exif['GPSInfo']) | |
| if latlong is not None: | |
| log['geofield'] = [ | |
| { | |
| "geom": f"POINT ({latlong[0]} {latlong[1]})", | |
| }, | |
| ] | |
| # Read timestamp from image. | |
| if exif is not None and 'DateTime' in exif: | |
| image_date = datetime.strptime(exif['DateTime'], '%Y:%m:%d %H:%M:%S') | |
| log['timestamp'] = image_date.timestamp() | |
| # Image binary. | |
| with open(image_filepath, "rb") as imageFile: | |
| image_binary = base64.b64encode(imageFile.read()) | |
| log['images'] = [ | |
| f"data:image/jpeg;base64,{image_binary.decode('utf-8')}" | |
| ] | |
| farm = farmOS(hostname=hostname, username=username, password=password) | |
| farm.authenticate() | |
| res = farm.log.send(payload=log) | |
| print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah yes. Sorry, I didn't really take any time to document this. Glad that worked!
In Python you can use
f"my string {my_variable} the rest of my string"to easily format variables inside. The alternative would be"my string " + my_variable + " the rest of my string."Hmm I'm not sure what you mean. The last line of my script is
print(res)- that should print the response from the POST to the farmOS server. Perhaps that is failing? That might explain "None".