cffi export python code to dll , how to read image object in @ffi.def_extern()

2024/10/5 18:58:16

i am trying to convert my python code to dll using cffi so i can access this code in my c# apllication, and i am trying to send image my c# code to python function, below is my code to read the file and convert to bytes array

[DllImport(@"plugin-1.5.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr test2(byte[] img, StringBuilder url);
void call(){
StringBuilder sb = new StringBuilder(20);
sb.Append("test");Bitmap target = (Bitmap)Bitmap.FromFile(@"C:\Users\LaserTrac\Desktop\lala_192_1.jpg");ImageFormat fmt = new ImageFormat(target.RawFormat.Guid);var imageCodecInfo = ImageCodecInfo.GetImageEncoders().FirstOrDefault(codec => codec.FormatID == target.RawFormat.Guid);//this is for situations, where the image is not read from disk, and is stored in the memort(e.g. image comes from a camera or snapshot)if (imageCodecInfo == null){fmt = ImageFormat.Jpeg;}byte[] image_byte_array = null;long length = 0;//Image img = Image.FromFile(@"");using (MemoryStream ms = new MemoryStream()){target.Save(ms, fmt);image_byte_array = ms.ToArray();length = ms.Length;}
test2(image_byte_array, sb);
}

and below is my code in python what i tried , plugin.h

extern char* test2(unsigned char*, char* url);

python file

@ffi.def_extern()def test2(img, url):print(img)url = ffi.string(url)#img1 = Image.open(io.BytesIO(ffi.string(img)))#img1.save("lala1.jpg")decoded = cv2.imdecode(np.frombuffer(ffi.buffer(img)))#img_np = cv2.imdecode(ffi.buffer(img), cv2.IMREAD_COLOR)cv2.imwrite("filename.jpg", decoded) p = ffi.new("char[]", "test".encode('ascii'))return p

error i got is buffer size must be a multiple of element size how can i convert this img object to any python image object

in the code above when i tried below code

img_np = cv2.imdecode(ffi.buffer(img), cv2.IMREAD_COLOR)

i got type error in test2 TypeError: Expected Ptr<cv::UMat> for argument 'buf'

and with this line decoded = cv2.imdecode(np.frombuffer(ffi.buffer(img))) i got ValueError ValueError: buffer size must be a multiple of element size

a workaround is i can convert image to base64 string and string and int variables are working, but i am also looking this to work because i have done this in c++ dll, and for some reason i am trying to convert my python code to dll.

Answer

ffi.buffer(p) must return a buffer of some size. If p was of an known-length array type like char[100] then it would return a buffer of 100 bytes. If it was pointing to a struct, i.e. of type mystruct_t *, then it would be of size sizeof(mystruct_t). But here it is a unsigned char *, so the returned buffer contains only sizeof(unsigned char) bytes, i.e. a single byte.

Use instead the ffi.buffer(p, size) variant. To know the size, pass the length variable as an extra argument from C# to Python.

https://en.xdnf.cn/q/120476.html

Related Q&A

Python 2.7.5 and Python 3.6.5

I have installed Python 3.6.5 however when i type Python it shows Python 2.7.5. Id like to use Python 3.[aravind@aravind05 Python-3.6.5]$ python3 --version Python 3.6.5[aravind@aravind05 Python-3.6.5]$…

How use creating polynomial expression like function in Python?

Id like to write a program in Python where user define a deegre of polynomial and coefficients (a,b,c). When program create a polynomial expression with this data Id like to use it like function becaus…

how to merge two sublists sharing any number in common? [duplicate]

This question already has an answer here:Using sublists to create new lists where numbers dont repeat(1 answer)Closed 9 years ago.Given thatg=[[1,2,3,4],[4,5,6],[6,7],[10,11]]What code should I use to …

\n is treated as \ and n [duplicate]

This question already has an answer here:Compiler error "error: stray \ in program" in macro definition(1 answer)Closed 10 years ago.The following python codeenv.Command(versionFile, allSrcs …

How to locate an element and extract required text with Selenium and Python

I am using Selenium to enter data on a web page, but have run into an issue with one of the input fields. This is the HTML code causing my difficulty:<div class="form-group"> <label …

Determine type of disk on Windows with Python

I want to list all of my disk and can be able to know its type: "SATA" "NVME" "M.2" or "PCI" on Windows computer. I made some research with wmi and I get the int…

How to get a list the visible vertices and segments of a mesh

I work on pose estimation of a 3d objects. I am using CAD model of that object to generate all the possible hypothesis of its pose. I am using pyopengl to render the view of the object from a specific…

Python function calls the wrong method/target

The following program simulates a traffic light system with some buttons. The buttons appear correctly, but if Im trying to call the method to create/change the LEDs, it ends up in the wrong method. He…

How to make a triangle of xs in python?

How would I write a function that produces a triangle like this:xxxxxxxxxx xxxxxLets say the function is def triangle(n), the bottom row would have n amount of xsAll I know how to do is make a box:n = …

Pip freeze --local

I am following a video tutorial and that guy did this:$ pip freeze --local > requirement.txt $ cat requirement.txtthis is to export all these packages with their versions in another project, but how…