为图像创建词典

2024-09-26 18:06:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试向OSX中的图像文件添加TIFF字典。 From Apple's documentation,看起来kCGImagePropertyTIFFDictionary定义了一个字典,字典中有kCGImagePropertyTIFFXResolution这样的键。你知道吗

我不知道如何在我的python代码中实现它。目前,我刚把这本词典作为一把钥匙和其他钥匙放在一起。(因此它不会将任何键设置到文件中。) 这是我的密码:

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

下面是来自open source script的一些其他python代码,可能可以工作,但我不明白它在做什么。你知道吗

imageProps = CGImageSourceCopyPropertiesAtIndex(imageSrc, 0, None)
        if imageProps is not None :
            tiffProps = imageProps[kCGImagePropertyTIFFDictionary]
            if tiffProps is not None :
                xRes = tiffProps[kCGImagePropertyTIFFXResolution]
                yRes = tiffProps[kCGImagePropertyTIFFYResolution]

Tags: 代码noneif字典isnotdestinationoptions
1条回答
网友
1楼 · 发布于 2024-09-26 18:06:48

结果是需要嵌套字典。另一个问题是数字必须是数字,这并不奇怪。你知道吗

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

一个更好的解决方案是根本不使用TIFF字典,因此它可以用于其他文件格式:

        options = {
            kCGImagePropertyDPIHeight: 300,
            kCGImagePropertyDPIWidth: 300
            }

相关问题 更多 >

    热门问题