Tip of the day: Edit IPTC data in JPEG file with CGImageSource/CGImageDestination
A client asked for editing IPTC entries in an image and add copyright notice. The CoreGraphics Image Source/Destination classes allow to read/write EXIF, IPTC, GPS, PNG, TIFF, PNG and JFIF properties. We use it here to load the image, add entries for IPTC with keys credit, copyright notice and contact and write the file to disk:
dim InputFile as FolderItem = SpecialFolder.Desktop.Child("test.jpg")
dim OutputFile as FolderItem = SpecialFolder.Desktop.Child("output.jpg")
dim inputSource as CGImageSourceMBS = CGImageSourceMBS.CreateWithFile(InputFile)
dim outputDest as CGImageDestinationMBS = CGImageDestinationMBS.CreateWithFile(OutputFile, "public.jpeg")
dim ImageProperties as Dictionary = inputSource.PropertiesAtIndex(0)
dim GlobalProperties as Dictionary = inputSource.Properties
dim IPTC as Dictionary = ImageProperties.Lookup(CGImageSourceMBS.kCGImagePropertyIPTCDictionary, nil)
if IPTC = nil then
// create if missing
IPTC = new Dictionary
ImageProperties.Value(CGImageSourceMBS.kCGImagePropertyIPTCDictionary) = IPTC
end if
IPTC.Value(CGImageSourceMBS.kCGImagePropertyIPTCCredit) = "Credit Test"
IPTC.Value(CGImageSourceMBS.kCGImagePropertyIPTCCopyrightNotice) = "Copyright Test"
IPTC.Value(CGImageSourceMBS.kCGImagePropertyIPTCContact) = "Contact Test"
outputDest.SetProperties(GlobalProperties)
outputDest.AddImageFromSource(inputSource, 0, ImageProperties)
call outputDest.Finalize
For cross platform EXIF/IPTC/XMP changes, you can in general try our XMP plugin.Please note that in CGImageDestination/Source and XMP Plugin most keys are mapped. So setting Credit in IPTC, also sets the corresponding field in XMP.