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);
   
}

Install Meilisearch and run on windows

2023-09-11 Posted in SearchRust

meilisearch is good search engine that is simple and very fast with a good rest api. If you want to run meilisearch on Windows, you have to compile the code, but it's very simple.

Step 1 Clone meilisearch from Github

git clone https://github.com/meilisearch/MeiliSearch

Step 2 Install the Rust tools

In order to compile the project you have to install Rust, you will find a get started guid here

Step 3 Install C++ dev tools from Visual Studio

You need c++ dev tools from visual studio make sure it is installed. Open the Visual Studio installer and select Desktop development with C++

Visual Studio Workloads

Step 4 Compile the project

Go to the folder where you cloned the repository and run the commands:

rustup update

cargo build --release

Step 5 Run the program

When it has finished compiling, go to the folder /target/release

Now you can run meilisearch it will generate a master key if you don't submit one ./meilisearch.exe

Meilisearch have several libraries for 10 different languages here is an example in C#

MeilisearchClient client = new MeilisearchClient("http://localhost:7700", "masterkey");

try
{
   index = await client.GetIndexAsync("Movies");
}
catch (Meilisearch.MeilisearchApiError ex)
{

   if (ex.Code == "index_not_found")
   {
       await client.CreateIndexAsync("Movies", "MovieId");

       index = await client.GetIndexAsync("Movies");
   }
}

var MovieItem = new Movie(){
   Name = "Apollo 11",
   Year = 2019,
   IMDB = "tt8760684"
};

await index!.AddDocumentsAsync(MovieItem, "objectId");


var results = await index.SearchAsync<Movie>("Apollo");
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20