Sending numpy array (image) to c++ library with ctypes

Question:

I’m trying to make a python program that can run Photoshop Plugins by using this DLL library from Spetric – https://github.com/spetric/Photoshop-Plugin-Host

I need to send an image loaded by opencv to the dll (while I can read c++ I cannot program in it and have never been able to get the dll to compile for edits such as loading the image directly from a filename)

The description of the function is

pspiSetImage(TImgType type, int width, int height, void *imageBuff, int imageStride, void *alphaBuff = 0, int alphaStride = 0);

I have tried several suggestions and solutions found here on stackoverflow but they all result in the same thing "OSError: exception: access violation writing 0x00000000"

Which is weird because I thought the idea was to pass in a pointer to a long buffer of data and that number wouldn’t be 0. I’ve checked the value/output and it never is zero that I pass in.

I have tried accessing the __array_interface++[‘data’][0], using ctypes.data_as built into the numpy object, various versions of POINTER and c_void_p. My fundamental misunderstanding of what’s needed and how to get it and pass it is my problem.

Are there any suggestions or helpful hints to point me in the right direction?

EDIT: Here is the code I’m currently working with

import ctypes
import cv2

plugins = {}

def main(args=None): 
    plugin_host = ctypes.CDLL('.\pspiHost.dll')

    plugin_host.pspiSetPath(".\8bf filters\")

    # Any image will do, I used a PNG to test alpha transparency
    im = cv2.imread('.\fish.png', cv2.IMREAD_UNCHANGED)

    array = im.ctypes.data_as(ctypes.POINTER(ctypes.c_void_p))
    width = im.shape[0]
    height = im.shape[1]

    # 1 in first parameter is for RGBA format, I'm using a png with transparency
    plugin_host.pspiSetImage(ctypes.c_int(1), 
                             ctypes.c_int(width), 
                             ctypes.c_int(height), 
                             array, 
                             ctypes.c_int(0))

if __name__=='__main__':
    main()
Asked By: LeviFiction

||

Answers:

This is a duplicate of [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati’s answer), but I’m going to detail.

When working with CTypes, the .dll must export functions using C compatible interface (extern "C"), while pspiSetImage seems to be C++ (default arguments).

Also check [SO]: How to write a function in C which takes as input two matrices as numpy array (@CristiFati’s answer) for details converting NumPy arrays.

Here’s a snippet that should fix things.

import ctypes as cts
import cv2
import numpy as np


plugin_host = ctypes.CDLL(r".pspiHost.dll")

pspiSetPath = plugin_host.pspiSetPath
pspiSetPath.argtypes = (cts.c_wchar_p,)  # Got this from the passed argument
pspiSetPath.restype = None  # void return?

# ...

img = cv2.imread(r".fish.png", cv2.IMREAD_UNCHANGED)

# ...

pspiSetImage = plugin_host.pspiSetImage
pspiSetImage.argtypes = (
    cts.c_int, cts.c_int, cts.c_int,
    np.ctypeslib.ndpointer(dtype=img.dtype, shape=img.shape, flags="C"), #cts.c_void_p,  # Extra checks for NumPy array
    cts.c_int,
    cts.c_void_p,  # Not sure what the array properties are
    cts.c_int,
)
pspiSetImage.restype = None  # void return?

# ...

pspiSetPath(".\8bf filters\")

img_stride = img.strides[0]

pspiSetImage(
    1, *img.shape[:2],
    img,  #np.ctypeslib.as_ctypes(img),
    img_stride, None, 0
)

Notes:

  • Not sure what the function does internally, but bear in mind that the array has 3 dimensions (and you’re only passing 2), also its dtype

  • Also, after taking a (shallow) look at Photoshop-Plugin-Host sources, imageStride argument should (most likely) be img.strides[0]

  • Function argument imageBuff is tailored on this specific array. If you need to call it multiple times, with different arrays (shapes and dtypes), you should take the generic approach (have a cts.c_void_p argument and convert it via np.ctypeslib.as_ctypes (or img.ctypes.data_as))

Answered By: CristiFati
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.