Python Forum
unable to load an image with C (Cython) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: unable to load an image with C (Cython) (/thread-36061.html)



unable to load an image with C (Cython) - HLD202 - Jan-13-2022

Hello, I just learn about cython todays and im trying to play with it and now im trying to load and image from local storage with .pyx extension but it just return 4 bytes but when i try to load a .txt it works fine...

Here's the main .py
import pyximport; pyximport.install()
import test_c


def main():
    file: bytes = test_c.req()
    print(file) 

if __name__=='__main__':
    main()
and Here's the test_c.pyx file:
from libc.stdio cimport fopen, fread, fclose, fseek, ftell, SEEK_END, rewind
from libc.stdlib cimport malloc

cpdef req():
    
    cdef long lSize
    cdef char *buffer

    fp = fopen('img.jpg', 'rb')
    fseek(fp, 0, SEEK_END)
    lSize = ftell(fp)
    rewind(fp)
    buffer = <char *>malloc(lSize)

    fread(buffer, 1, lSize, fp)
    fclose(fp)

    return buffer
and here's the result:
Output:
b'\xff\xd8\xff\xe0'