Skip to content

Instantly share code, notes, and snippets.

@pudquick
Last active December 20, 2015 16:18
Show Gist options
  • Select an option

  • Save pudquick/6160408 to your computer and use it in GitHub Desktop.

Select an option

Save pudquick/6160408 to your computer and use it in GitHub Desktop.
ColorSync in python
# A new way
import objc
from CoreFoundation import CFURLRef, CFErrorRef
objc.parseBridgeSupport("""<?xml version='1.0'?>
<!DOCTYPE signatures SYSTEM "file://localhost/System/Library/DTDs/BridgeSupport.dtd">
<signatures version='1.0'>
<function name='ColorSyncProfileCreateWithURL'>
<arg type='^{__CFURL=}'/>
<arg type='^^{__CFError}' type_modifier='N'/>
<retval already_retained='true' type='^{ColorSyncProfile=}'/>
</function>
<function name='ColorSyncProfileVerify'>
<arg type='^{ColorSyncProfile=}'/>
<arg type='^^{__CFError}' type_modifier='N'/>
<arg type='^^{__CFError}' type_modifier='N'/>
<retval type='B'/>
</function>
</signatures>
""", globals(), '/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ColorSync.framework')
ColorSyncProfileRef = objc.createOpaquePointerType("ColorSyncProfileRef", "^{ColorSyncProfile=}", "ColorSyncProfileRef")
profile_path = CFURLRef.alloc().initFileURLWithPath_('/Users/mike/Desktop/Generic RGB Profile-not.icc')
profile_ref, profile_err = ColorSyncProfileCreateWithURL(profile_path,None)
is_valid = ColorSyncProfileVerify(profile_ref, None, None)
# pyobjc_id(...)
#other way
from ctypes import *
from ctypes.util import find_library
import sys, os.path
global _CF, _ColorSync
_CF = cdll.LoadLibrary(find_library("CoreFoundation"))
_ColorSync = cdll.LoadLibrary(find_library("ApplicationServices"))
_CF.CFStringGetCStringPtr.argtypes = [c_void_p, c_int]
_CF.CFStringGetCStringPtr.restype = c_char_p
_CF.CFURLCreateFromFileSystemRepresentation.argtypes = [c_void_p, c_char_p, c_int, c_bool]
_CF.CFURLCreateFromFileSystemRepresentation.restype = c_void_p
_CF.CFRelease.argtypes = [c_void_p]
_CF.CFURLGetString.argtypes = [c_void_p]
_CF.CFURLGetString.restype = c_void_p
CFErrorRef = c_void_p
_ColorSync.ColorSyncProfileCreateWithURL.argtypes = [c_void_p, POINTER(CFErrorRef)]
_ColorSync.ColorSyncProfileCreateWithURL.restype = c_void_p
_ColorSync.ColorSyncProfileVerify.argtypes = [c_void_p, POINTER(CFErrorRef), POINTER(CFErrorRef)]
_ColorSync.ColorSyncProfileVerify.restype = c_bool
class CFObject(object):
def __init__(self, obj):
if obj == 0:
raise ValueError('object is zero')
self._obj = obj
def __del__(self):
if _CF:
_CF.CFRelease(self._obj)
class CFURL(CFObject):
def __init__(self, filename):
if not isinstance(filename, bytes):
filename = filename.encode(sys.getfilesystemencoding())
filename = os.path.abspath(os.path.expanduser(filename))
url = _CF.CFURLCreateFromFileSystemRepresentation(0, filename, len(filename), False)
super(CFURL, self).__init__(url)
def __str__(self):
cfstr = _CF.CFURLGetString(self._obj)
out = _CF.CFStringGetCStringPtr(cfstr, 0)
return out
# Example usage:
profile_path = '/Users/mike/Desktop/TestProfile.icc'
profile_url = CFURL(profile_path)
error_ref = CFErrorRef()
profile_ref = _ColorSync.ColorSyncProfileCreateWithURL(profile_url._obj, byref(error_ref))
verify_err = CFErrorRef()
verify_warn = CFErrorRef()
is_valid = _ColorSync.ColorSyncProfileVerify(profile_ref, byref(verify_err), byref(verify_warn))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment