Inside all the if conditions, the lines involving `image` cannot be translated to pure C because it is checked that `image` is not None. If you use a Python function (defined with `def` instead of `cpdef`) you can use the "not None" instruction for `image`, and all these lines will be translated to pure Python code. Some profiling would be needed to check whether this brings a real gain, if you have the time to check it...