Problem with image property dictionaries

Can anyone talk me through the concepts of image Property Dictionaries? I want to save a TIFF file that has a TIFF Dictionary with some keys.


I'm working in python, but any advice would be welcome.


My current code is:


options = { 
     kCGImagePropertyTIFFDictionary: 'TIFFDictionary', 
     kCGImagePropertyTIFFXResolution: "300", 
     kCGImagePropertyTIFFYResolution: "300", 
     kCGImagePropertyTIFFResolutionUnit: 2 } 
CGImageDestinationAddImage(destination, image, options)


But they don't get saved to the TIFF file. I don't understand where and how I need to use

kCGImagePropertyTIFFDictionary.


https://developer.apple.com/documentation/imageio/cgimageproperties/format_specific_dictionaries


From the documentation it looks like the keys need to be inside kCGImagePropertyTIFFDictionary somehow.

Accepted Reply

In fact, I don't even need a TIFF Dictionary. I can just use:


  resolution = 300
  options = {
       kCGImagePropertyDPIHeight: resolution
       kCGImagePropertyDPIWidth: resolution
  }


, which will work for other formats, too.

Software is never finished!

Replies

You may have a look at this other thread to see how the dictionary is referenced.


h ttps://forums.developer.apple.com/thread/86498

Thanks. That seems to suggest that I need a dictionary with kCGImagePropertyTIFFDictionary as a key and another dictionary as the value; the second dict contains all the other key-value pairs.

(Not sure why kCGImagePropertyDepth is at the same meta-level as the TIFF Dictionary..... but still.)


I tried that in python but it completely stopped the images being created altogether. I'll keep experimenting.

Fixed it! It turns out that the numbers need to be, unsurprisingly, numbers, not strings, which I'd inferred from Apple's documentation.

And the Dictionary constant is indeed a key whose value is another dictionary.


options = {
Quartz.kCGImagePropertyTIFFDictionary: {
Quartz.kCGImagePropertyTIFFXResolution: 300,
Quartz.kCGImagePropertyTIFFYResolution: 300,
Quartz.kCGImagePropertyTIFFResolutionUnit: 2
}}

Happy you solved the issue and thanks for having given this feedback.

Don't forget to close the thread as solved.

In fact, I don't even need a TIFF Dictionary. I can just use:


  resolution = 300
  options = {
       kCGImagePropertyDPIHeight: resolution
       kCGImagePropertyDPIWidth: resolution
  }


, which will work for other formats, too.

Software is never finished!