You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defmoduleEXIFUpdatedoaliasEXIFUpdate.Utilsdefupdate_file(filename,dir)dofilepath=Path.join(dir,filename)# IO.puts("Working on: #{filepath}")split_filename_on_parantheses=String.split(filename,"(")json_filename=ifKernel.length(split_filename_on_parantheses)==1doPath.join(dir,[split_filename_on_parantheses,".json"])elsename=Enum.at(split_filename_on_parantheses,0)multiple_with_ext=Enum.at(split_filename_on_parantheses,1)|>String.split(")")multiple=multiple_with_ext|>Enum.at(0)extension=multiple_with_ext|>Enum.at(1)Path.join(dir,[name,extension,"(",multiple,")",".json"])end{:ok,data}=Exiftool.execute([json_filename])%{"photo_taken_time_timestamp"=>json_photo_taken_time_timestamp,"creation_time_timestamp"=>json_creation_time_timestamp,"geo_data_exif_altitude"=>json_geo_data_exif_altitude,"geo_data_exif_latitude"=>json_geo_data_exif_latitude,"geo_data_exif_longitude"=>json_geo_data_exif_longitude}=datacreate_date=Utils.json_timestamp_to_exif_datetime(json_creation_time_timestamp)date_time_original=Utils.json_timestamp_to_exif_datetime(json_photo_taken_time_timestamp)gps_altitude=json_geo_data_exif_altitude|>String.to_float()gps_latitude=json_geo_data_exif_latitude|>String.to_float()gps_longitude=json_geo_data_exif_longitude|>String.to_float()exif_fields=Utils.build_exif_fields(filepath,create_date,date_time_original,gps_altitude,gps_latitude,gps_longitude)# |> IO.inspect(label: "EXIF Field Changes")# IO.puts("Writing: #{filepath}")Utils.write_exif_data(filepath,exif_fields)# IO.puts("")endend
Utils Module
defmoduleEXIFUpdate.Utilsdodefbuild_exif_fields(filepath,create_date,date_time_original,gps_altitude,gps_latitude,gps_longitude)do{:ok,current_exif_fields}=Exiftool.execute([filepath])# IO.inspect(current_exif_fields)is_png?=String.slice(filepath,-4..-1)==".png"%{}|>__MODULE__.maybe_add_date_time_fields(current_exif_fields,is_png?,create_date,date_time_original)|>__MODULE__.maybe_add_gps_fields(current_exif_fields,gps_latitude,gps_longitude,gps_altitude)end@doc""" Add new CreateDate or DateTimeOriginal only if they don't already exist. """defmaybe_add_date_time_fields(new_exif_fields,current_exif_fields,is_png?,create_date,date_time_original)doEnum.reduce([{"create_date","CreateDate",create_date},{"date_time_original","DateTimeOriginal",date_time_original}],new_exif_fields,fn{exif_key,write_key,val},acc->ifMap.has_key?(current_exif_fields,exif_key)doaccelseifis_png?==trueandwrite_key=="DateTimeOriginal"do# DateCreated needs to be written to EXIF ModifyDate # as "YYYY-MM-DD hh:mm:ss" for Google Photos to properly store# PNG files at the correct date/time.modify_date=val|>Timex.parse!("{ISO:Extended}")|>Timex.format!("{YYYY}-{0M}-{0D} {h24}:{m}:{s}")acc|>Map.put(write_key,val)|>Map.put("DateCreated",modify_date)elseMap.put(acc,write_key,val)endendend)end@doc""" Add new GPSLatitude and GPSLongitude if: - none of these fields currently exist: GPSPosition, GPSLatitude, and GPSLongitude and - either the new GPSLatitude or the GPSLongitude does not equal 0.0. ExifTool will automatically figure out GPSLatitudeRef/GPSLongitudeRef from the same GPSLatitude/GPSLongitude value. Add new GPSAltitude: - only if new GPSLatitude/GPSLongitude are being added and - if new GPSAltitude input does not equal 0.0. """defmaybe_add_gps_fields(new_exif_fields,current_exif_fields,gps_latitude,gps_longitude,gps_altitude)doifshould_add_gps_fields?(current_exif_fields)==trueand(gps_latitude!=0.0orgps_longitude!=0.0)donew_exif_fields|>(&Enum.reduce([{"GPSLatitude",gps_latitude},{"GPSLatitudeRef",gps_latitude},{"GPSLongitude",gps_longitude},{"GPSLongitudeRef",gps_longitude}],&1,fn{key,val},acc->Map.put(acc,key,val)end)).()|>maybe_add_gps_altitude(gps_altitude)elsenew_exif_fieldsendenddefpshould_add_gps_fields?(current_exif_fields)doMap.has_key?(current_exif_fields,"gps_position")==falseandMap.has_key?(current_exif_fields,"gps_latitude")==falseandMap.has_key?(current_exif_fields,"gps_longitude")==falseenddefpmaybe_add_gps_altitude(new_exif_fields,gps_altitude)doifgps_altitude!=0.0donew_exif_fields|>Map.put("GPSAltitude",gps_altitude)|>Map.put("GPSAltitudeRef",0)elsenew_exif_fieldsendenddefjson_timestamp_to_exif_datetime(timestamp)dotimestamp|>String.to_integer()|>Timex.from_unix()|>Timex.format!("{ISO:Extended}")enddefwrite_exif_data(filepath,exif_fields)doexif_fields_params=Enum.reduce(exif_fields,[],fn{key,val},acc->["-xmp:#{key}=\"#{val}\""|acc]end)# IO.inspect(exif_fields_params)Exiftool.execute(exif_fields_params++["-overwrite_original_in_place",filepath])endend