pythonimage-processingcanon-sdk

Converting a .CR3 file to .JPG using canon_cr3 library in Python


I want to convert a .CR3 file (camera is a Canon PowerShot SX70 HS) to a .JPG file using Python. I found a project on GitHub which actually should do the desired task: https://github.com/lclevy/canon_cr3

I've installed the canon_cr3 library using the setup.py file. Indeed, the canon_cr3-13mar2019-py3.6.egg file exists after installing the library. Hence, when I type

from canon_cr3 import Image

in the Python 3.6.1 shell and press Enter, it seems that the import is successful (no error message arises). However, when I try the example on https://github.com/lclevy/canon_cr3#example-usage

image = 'D:\pic\test_img.CR3'
image = image.replace("\\", "/") #working on Windows
img = Image(image)

an error arises:

NameError: name 'getLongBE' is not defined

The error arises in the 'cr3_lib.py' file executing the stsz() function. The getLongBE() function is defined in the library's 'parse_cr3.py' file. However, I cannot see the link between the 'parse_cr3.py' file and the 'cr3_lib.py' file.

I don't know what I'm missing, what to try or how to resolve this issue. Do you have any suggestions? Or is there another method to convert .CR3 files to .png in Python?


Solution

  • Update:

    Just add the missing function inside cr3_lib.py file:

    def getLongBE(d, a):
        return unpack('>L',(d)[a:a+4])[0]
    

    And your cr3_lib.py file should look like this:

    from struct import unpack, Struct
    from binascii import unhexlify, hexlify
    # from parse_cr3 import getLongBE # remove this line
    
    def getLongBE(d, a):
        return unpack('>L',(d)[a:a+4])[0]
    
    ...
    

    Note: remove the following line from the current file if you added before:

    from parse_cr3 import getLongBE