Skip to content

Instantly share code, notes, and snippets.

@troutcolor
Last active March 24, 2024 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troutcolor/409232f0772992c3acbef5afd07add76 to your computer and use it in GitHub Desktop.
Save troutcolor/409232f0772992c3acbef5afd07add76 to your computer and use it in GitHub Desktop.
makes 9x9 montages of photos the middle image is a map where the photos are taken. photos need ot be geotagged.
-- I use a hard coded folder to hold the photo. I export from photos app. The folder needs 8 photos with geotags
--needs imagemagick/convert
--added this: exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d-%H-%M-%S%%-c.%%e" *.jpeg
--so need exiftool
--changed from mapquest to geoapify NEED AN API key https://www.geoapify.com
set APIKEY to "ThisIsWhereTheAPIkeyGoes"
set eightcolours to {"ff0000", "0000ff", "00ff00", "ffff00", "ffa500", "800080", "ffffff", "000000"}
set thefolder to "/Users/john/Desktop/flickr-temp/grid/"
set mytempFolder to do shell script "mktemp -d "
do shell script "cd " & thefolder & "; /usr/local/bin/exiftool '-FileName<DateTimeOriginal' -d \"%Y-%m-%d-%H-%M-%S%%-c.%%e\" *.jpeg"
--open thefolder to see what is going on
--do shell script "open " & mytempFolder
--get list of files in folder
tell application "System Events"
set FileList to POSIX path of disk items of folder thefolder whose name extension is "jpeg" or name extension is "jpg"
end tell
--just sorting by name as export from photos with filename give chronlogical
--eventually should sort by date taken... The exiftool line above is a kludge
set FileList to simple_sort(the FileList)
set FileList to items 1 through 8 of FileList
--use the original files to get location
--cause if you copy with cp they seem to lose geo
set loclist to ""
set c to 1
repeat with file_ in FileList
repeat 1 times
set imgkind to kind of (info for file_)
if (imgkind does not contain "Image") then
exit repeat -- 1 time
end if
set file_ to quoted form of POSIX path of (file_ as string)
set latTotal to 0
set longTotal to 0
--set thelat to do shell script "mdls -name kMDItemLatitude " & file_
--set thelon to do shell script "mdls -name kMDItemLongitude " & file_
--exiftool -GPSLatitude -n
set thelat to do shell script "/usr/local/bin/exiftool -GPSLatitude -n " & file_ & "| cut -d ':' -f 2 | tr -d ' '"
set thelon to do shell script "/usr/local/bin/exiftool -GPSLongitude -n " & file_ & "| cut -d ':' -f 2 | tr -d ' '"
set latTotal to thelat + latTotal
set longTotal to thelon + longTotal
set olddel to AppleScript's text item delimiters
set AppleScript's text item delimiters to "= "
--set loclist to loclist & text item 2 of thelat & "," & text item 2 of thelon & "|circle-md-" & item c of eightcolours & "||"
--set loclist to loclist & thelat & "," & thelon & "|circle-md-" & item c of eightcolours & "||"
set loclist to loclist & "lonlat:" & thelon & "," & thelat & ";type:circle;color:%23" & item c of eightcolours & ";size:small;icontype:material;whitecircle:no|"
--lonlat:-4.687856955889288,56.074354156448436;type:circle;color:%2346a7b5;size:small;icontype:material;whitecircle:no|
set AppleScript's text item delimiters to olddel
set c to c + 1
end repeat
--loclist now got list for mapquest api location & icon
end repeat
set loclist to text 1 thru -2 of loclist
--display dialog loclist
set latAverage to latTotal / 8
set longAverage to longTotal / 8
--&center=lonlat:-122.304378,47.526022
--get the map for geoapify
set theurl to "https://maps.geoapify.com/v1/staticmap?style=osm-bright&width=600&height=600&zoom=11.8923&marker=" & loclist & "&apiKey=" & APIKEY
--&center=lonlat:" & latAverage & "," & longAverage
do shell script "curl '" & theurl & "' > " & mytempFolder & "/map.jpg"
--copy all the images into the temp folder
--this seems to lose geo exif tags (probably other stuff too)
do shell script "cp -a " & thefolder & "*.jpeg " & mytempFolder
--Change file list to new files
--and sort
tell application "System Events"
set FileList to POSIX path of disk items of folder mytempFolder whose name extension is "jpeg"
end tell
set FileList to simple_sort(the FileList)
set FileList to items 1 through 8 of FileList
--this loop with make the images square, resize them and add circles
--I expect this could be done in one fell swoop with imagemagick/convert
repeat with n from 1 to 8
set file_ to item n of FileList
squareImage(file_)
do shell script "sips --resampleWidth 600 " & file_
do shell script "/opt/homebrew/bin/convert " & file_ & " -draw 'fill #" & item n of eightcolours & " circle 580,580,596,596' " & file_
end repeat
--make a list of files for the montage, in chronological order with map in the middle
set montageList to ""
repeat with n from 1 to 4
set montageList to montageList & "'" & item n of FileList & "' "
end repeat
set montageList to montageList & "'" & mytempFolder & "/map.jpg' "
repeat with n from 5 to 8
set montageList to montageList & "'" & item n of FileList & "' "
end repeat
--make a montage
--use try cause imagemagic is complainging about fonts
try
set mon to do shell script "set -e;cd " & mytempFolder & "; /opt/homebrew/bin/montage -mode concatenate -tile 3x " & montageList & " out.jpg > /dev/null 2>&1 "
end try
--need to wait until imagemagick does its stuff
tell application "Finder"
repeat
if exists mytempFolder & "/out.jpg" as POSIX file then
exit repeat
else
delay 5
end if
end repeat
end tell
set resultFile to POSIX path of (choose file name with prompt "Save As File" default name "map-square.jpg" default location path to desktop) as text
do shell script "mv " & mytempFolder & "/out.jpg " & quoted form of resultFile
---display dialog montageList
on simple_sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end simple_sort
on squareImage(this_file)
try
tell application "Image Events"
-- start the Image Events application
launch
-- open the image file
set this_image to open this_file
-- get dimensions of the image
copy dimensions of this_image to {W, H}
-- determine scale length
if W is less than H then
set the side_length to W
else
set the side_length to H
end if
-- perform action
crop this_image to dimensions {side_length, side_length}
-- save the changes
save this_image with icon
-- purge the open image data
close this_image
end tell
on error error_message
display dialog error_message
end try
return {W, H}
end squareImage
on change_case_of(this_text, this_case)
if this_case is "lower" then
set the comparison_string to " "
set the source_string to "_"
else
set the comparison_string to "abcdefghijklmnopqrstuvwxyz"
set the source_string to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
end if
set the new_text to ""
repeat with thisChar in this_text
set x to the offset of thisChar in the comparison_string
if x is not 0 then
set the new_text to (the new_text & character x of the source_string) as string
else
set the new_text to (the new_text & thisChar) as string
end if
end repeat
return the new_text
end change_case_of
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment