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.
char *
from C, because the memory pointed by the returned pointer will either leak or (like here) be freed automatically before the C# code has got a change to read it. Instead you need the more painful way of guessing how large a buffer the function will return, and allocating and passing this buffer from the caller. – Posehn