Exif Data Read and Write

EXIF data read and write

I'm using this to get EXIF infos from an image file:

import ImageIO

let fileURL = theURLToTheImageFile
if let imageSource = CGImageSourceCreateWithURL(fileURL as CFURL, nil) {
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)
if let dict = imageProperties as? [String: Any] {
print(dict)
}
}

It gives you a dictionary containing various informations like the color profile - the EXIF info specifically is in dict["{Exif}"].

R write EXIF data to JPEG file

Thanks to all who responded. As a result, I obtained the following solution.

Install ExifTool,
I use Ubuntu comand:

sudo apt install libimage-exiftool-perl

Then in my R code, to add GPS coordinates to image I use:

exiftool_cmd <- paste("exiftool -GPSLongitudeRef=E -GPSLongitude=",latlon_exif[i,11]," -GPSLatitudeRef=N -GPSLatitude=",latlon_exif[i,10]," ","./nodejpg/",latlon_exif[i,4],".jpg",sep='')
system(exiftool_cmd)

Where latlon_exif[i,11] and latlon_exif[i,10] - coordinates, latlon_exif[i,4] - name of file.

To add data and time to image I use:

exiftool_cmd <- paste("exiftool -alldates=",shQuote(date_exif[which(date_exif[,4]%in%latlon_exif[i,4]),8])," ","./nodejpg/",latlon_exif[i,4],".jpg",sep='')
system(exiftool_cmd)

Where shQuote(date_exif[which(date_exif[,4]%in%latlon_exif[i,4]),8]) data and time in format: '2017-11-16 22:33:17'

How to get the EXIF data from a file using C#

Check out this metadata extractor. It is written in Java but has also been ported to C#. I have used the Java version to write a small utility to rename my jpeg files based on the date and model tags. Very easy to use.


EDIT metadata-extractor supports .NET too. It's a very fast and simple library for accessing metadata from images and videos.

It fully supports Exif, as well as IPTC, XMP and many other types of metadata from file types including JPEG, PNG, GIF, PNG, ICO, WebP, PSD, ...

var directories = ImageMetadataReader.ReadMetadata(imagePath);

// print out all metadata
foreach (var directory in directories)
foreach (var tag in directory.Tags)
Console.WriteLine($"{directory.Name} - {tag.Name} = {tag.Description}");

// access the date time
var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
var dateTime = subIfdDirectory?.GetDateTime(ExifDirectoryBase.TagDateTime);

It's available via NuGet and the code's on GitHub.

FreeImage: how to read and write image with EXIF?

If the documentataion does not help you, it is always worth looking at the examples and the unit tests. There is a program in the examples directory that shows the EXIF information, it seems to be a good starting point.

https://github.com/imazen/freeimage/blob/171744f4bc384f8872fd9437b23bbb5d54563253/Examples/Generic/
ShowMetadata.cpp

Exif Data READ Write

Have you looked at this library?

Write EXIF data after have added image to MediaStore

I solved.
The problem was that the file was not syncronized on disk.

So, here how it works for saving the image:

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, filename);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATE_ADDED, now);
values.put(MediaStore.MediaColumns.DATE_MODIFIED, now);

ContentResolver resolver = context.getContentResolver();
Uri uri = resolver.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values );

try( ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri,"w") )
{
FileDescriptor fd = pfd.getFileDescriptor();

try( OutputStream stream = new FileOutputStream(fd) )
{
// Perform operations on "stream".
mBitmap.compress( Bitmap.CompressFormat.JPEG, 90, stream );
}

// Synch data with disk. It's mandatory to be able later to call writeExif
fd.sync(); // <---- HERE THE SOLUTION

} catch( IOException e )
{
e.printStackTrace();
}

And how I add Exif metadata:

private void writeExif( Uri uri, List<String> exifStr )
{
try( ParcelFileDescriptor imagePfd = getContentResolver().openFileDescriptor(uri, "rw") )
{
ExifInterface exif = new ExifInterface( imagePfd.getFileDescriptor() );

for( int i = 0; i < ExifAttributes.length; i++ )
{
String value = exifStr.get(i);
if( value != null )
exif.setAttribute(ExifAttributes[i], value);
}

exif.saveAttributes();
}
catch( Exception e )
{
e.printStackTrace();
}
}


Related Topics



Leave a reply



Submit