Interesting Links Of The Week 1

2024-01-05 Posted in Design InspirationMapsVideosOpen DataTypescriptJavascriptDevelopment in other languagesGeospatial DataTools3DBlogsRobotsMiscellaneous

Every week I scour the internet to find the most fascinating, thought provoking and entertaining links I come across. From inspiring articles and informative blog posts to exciting videos and interesting resources.

Design Inspiration

Maps

Videos

Open Data

Typescript

Javascript

Development in other languages

Geospatial Data

Tools

3D

Blogs

Robots

Miscellaneous

Read EXIF Data From Images C#

2023-09-12 Posted in EXIFGPS

In this post we will discussed how to read EXIF data from images. You can organize your photos in folders according to tags or dates. In this article we will go through how to read EXIF data and use the metadata to organize the images and in different ways show what the images were taken if they have GPS tags.

Read EXIF data

We will use MetadataExtractor to read the metadata from the image.

using MetadataExtractor;

string path = @"c:\images\image1.jpg";

foreach (MetadataExtractor.Directory dir in ImageMetadataReader.ReadMetadata(path))
{
   foreach (Tag tag in dir.Tags){
       console.WriteLine($"{tag.Name}: {tag.Description}");
   }
}

GPS coordinates

If you want to read GPS coordinates from a picture, you have to recalculate the numbers so that you get them in the right format.

if (tag.Name == "GPS Longitude")
{
     
   MetadataExtractor.Rational[] d = dir.GetRationalArray(tag.Type);

   string Longitude = (d[0].ToDouble() + (d[1].ToDouble() / 60) + (d[2].ToDouble() / 3600)).ToString();
   console.WriteLine($"Longitude: {Longitude}");

} 
else if (tag.Name == "GPS Latitude")
{
   MetadataExtractor.Rational[] d = dir.GetRationalArray(tag.Type);

   string Latitude = (d[0].ToDouble() + (d[1].ToDouble() / 60) + (d[2].ToDouble() / 3600)).ToString();
   console.WriteLine($"Latitude: {Latitude}");
}

Organize images by years

Check if the image has the EXIF tag "Date/Time Original". Older phones and cameras sometimes don't tag the photos with it so if it's not there check for "File Modified Date". You can read the created date at the file system level, it can be wrong if you sync images with OneDrive or similar backup solutions. Sometimes images cannot have any of these tags and so you have to take the file system's creation date instead

using MetadataExtractor;

DirectoryInfo directory = new DirectoryInfo(@"c:\images\");

string OutPath = @"c:\images-organized\";

foreach (FileInfo file in directory.GetFiles("*.jpg"))
{

   string year = "";


   foreach (MetadataExtractor.Directory dir in ImageMetadataReader.ReadMetadata(file.FullName))
   {
       foreach (Tag tag in dir.Tags)
       {
           
           if (tag.Name == "Date/Time Original") 
           {
               //2023:09:12 20:00:00
               year = tag.Description.Split(":")[0];
               break;
           }
           else if (tag.Name == "File Modified Date" && year == "") 
           {
               //2023-09-12 20:00:00
               year = tag.Description.Split("-")[0];
               break;
           }
       }
   }

   string yearPath = Path.Combine(OutPath, year);
   
   //Create year folders if it does not exist
   if (System.IO.Directory.Exists(yearPath) == false)
   {
       System.IO.Directory.CreateDirectory(yearPath);
   }

   //Copy image to year folder
   string imagePath = Path.Combine(yearPath, file.Name);
   File.Copy(file.FullName, imagePath, true);
   
}
1 2 3 4 5 6 7 8 9