Writing image metadata (EXIF/TIFF/IPTC) to image file in OS X
Asked Answered
S

2

6

I am creating a photo editing app, and so far I've managed to read the metadata from image files successfully (after getting an answer to this question: Reading Camera data from EXIF while opening NSImage on OS X).

source = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
NSDictionary *props = (__bridge_transfer NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);

This copies all the metadata of the image file to a dictionary, and it works faily well. However, I couldn't find out how to write this metadata back to a newly created NSImage (or to an image file). Here is how I save my file (where img is an NSImage instance without metadata and self.cachedMetadata is the dictionary read from the initial image):

NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[img TIFFRepresentation]];
[rep setProperty:NSImageEXIFData withValue:self.cachedMetadata];
NSData *data;
if([[fileName lowercaseString] rangeOfString:@".png"].location != NSNotFound){
    data = [rep representationUsingType:NSPNGFileType properties:nil];
}else if([[fileName lowercaseString] rangeOfString:@".tif"].location != NSNotFound){
    data = [rep representationUsingType:NSTIFFFileType properties:nil];
}else{ //assume jpeg
    data = [rep representationUsingType:NSJPEGFileType properties:@{NSImageCompressionFactor: [NSNumber numberWithFloat:1], NSImageEXIFData: self.cachedMetadata}];
}

[data writeToFile:fileName atomically:YES];

How can I write the metadata? I used to write just EXIF for JPEG (the dictionary was EXIF-only previously) successfully but because EXIF lacked some of the fields that the initial images had (IPTC and TIFF tags) I needed to change my reading method. Now I have all the data, but I don't know how to write it to the newly-created image file.

Thanks, Can.

Subrogation answered 30/10, 2013 at 19:45 Comment(1)
hello! I am more or less in the same situation but cannot understand your solution. I have alist of keywords, titile and description i must write to the IPTC section of a jpg i know the path of. how can this be done? could you please have a look at this question and maybe post an answer? https://mcmap.net/q/1319829/-write-iptc-data-to-fileAdamik
S
5

Found an answer from another StackOverflow question: How do you overwrite image metadata?:

(code taken from that question itself and modified for my needs, which contains the answer to my question)

//assuming I've already loaded the image source and read the meta
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) data, NULL);
CFStringRef imageType = CGImageSourceGetType(source);

//new empty data to write the final image data to
NSMutableData *resultData = [NSMutableData data];
CGImageDestinationRef imgDest = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)(resultData), imageType, 1, NULL);

//copy image data
CGImageDestinationAddImageFromSource(imgDest, source, 0, (__bridge CFDictionaryRef)(window.cachedMetadata));
BOOL success = CGImageDestinationFinalize(imgDest);

This worked perfectly for writing back all the metadata including EXIF, TIFF, and IPTC.

Subrogation answered 2/11, 2013 at 22:40 Comment(3)
does this also preserve the clipping path if present?Paolo
I opened a new question for this here: #32161028 will add Bounty later tonight. Might be interesting also for you if you have asoftware for photo editing yourself.Paolo
In the link I posted there is also a sample JPG with the clipping path.. could you please have a look at it? i will give another Bounty to whoever solves it. can not open the Bounty just yet because if the answer doesnt come within 7 days its gone.. thanksPaolo
L
0

You could try either using, or looking at the code in, the "exiv2" tool maybe.

Lorenalorene answered 31/10, 2013 at 18:47 Comment(1)
I have a commercial project and the commercial license requires contacting the creator. I'd better stick with the built-in solutions. There is a way to extract all that data completely, there also must be a way to write back all that data into the image.Flieger

© 2022 - 2024 — McMap. All rights reserved.