×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    31

    Problems with a python wrapper: convert PyObject to Image

    Hi all!
    I hope that somebody can help because I am stuck on a project!

    From Python, I need to call some C++ code that modifies an image and returns it. I am able to write the wrapper so that it converts correctly from Python to C++ but I am not able to do the opposite. I don't know much about Python so the mistake might be really stupid.

    The relevant part of the wrapper:

    Code:
    static PyObject* FunctionName(PyObject* self, PyObject* args){
    
    CFbsBitmap *image;
    PyObject *input;
    
    if (!PyArg_ParseTuple(args, "O", &input1)) {
      return Py_BuildValue("s", "Cannot parse arguments.");
    }
        
    // Check if the call was made on a python object
    if (!PyCObject_Check(input1) || !PyCObject_Check(input2)) {
        return Py_BuildValue("i", -1);
    }
    
    // Turn PyCObject into CFbsBitmap
    image = (CFbsBitmap *)PyCObject_AsVoidPtr(input);
    
    //here call some C++ code on image
    
    // From here on I am not sure it is correct
    PyObject *pyObj;
    pyObj = (PyObject *)PyCObject_FromVoidPtr(image, NULL);
    return pyObj;
    so now, from Python, I call:

    Code:
    imOut = wrapper.FunctionName(PyImage._bitmapapi())
    Everything works but now imOut is a PyCObject at 0x.... and I don't know how to convert it back to an image.

    Any idea? Even the reference to some useful documentation would be helpful: where I looked there wasn't even a mention for _bitmapapi().
    PLEASE!!
    Thanks,
    Pdcb



    PS: sorry for the double posting but I hit post by mistake and I wasn't able to delete the post
    Last edited by pdcb_pdcb; 2008-03-11 at 04:34.

  2. #2
    Super Contributor
    Join Date
    Dec 2004
    Posts
    643

    Re: Problems with a python wrapper: convert PyObject to Image

    Quote Originally Posted by pdcb_pdcb View Post
    From Python, I need to call some C++ code that modifies an image and returns it. I am able to write the wrapper so that it converts correctly from Python to C++ but I am not able to do the opposite. I don't know much about Python so the mistake might be really stupid.
    The easiest way to roll your own Image from CFbsBitmap* is to make a CObject myBitmap that wraps the CFbsBitmap* and then do:

    Code:
    myImage = Image.from_cfbsbitmap(myBitmap)
    This is undocumented because it was originally intended for internal communication between modules, but it works. See graphics.py for the details of how the Image object wraps the underlying native object.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    31

    Re: Problems with a python wrapper: convert PyObject to Image

    Thanks a lot jplauril!

    I actually found that one hour after posting my request and i posted it but somehow it got deleted, probably for the mess I created by double posting.

    Thanks again,
    Pdcb

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    31

    Re: Problems with a python wrapper: convert PyObject to Image

    Actually I could need some more help. Referring to my previous post, if I call from python:

    Code:
    imOut = wrapper.FunctionName(PyImage._bitmapapi())
    imOut = Image.from_cfbsbitmap(imOut )
    and then i blit imOut to the canvas the thing works OK, but if I create a function like this:

    Code:
    def CallOnViewfinder(arg):
        im = wrapper.FunctionName(PyImage._bitmapapi())
        im = Image.from_cfbsbitmap(im) #line causing problems
        appuifw.app.body.blit(im)
    and I attach it to the viewfinder callback so that whenever a new frame is available CallOnViewfinder gets called, python crashes.

    if I comment out the second line of the function (and blit arg) it works fine. If I leave it as is I see a frame drawn on the screen right before Python crashes. The function in my wrapper doesn't do anything other than changing the input image to cfbsbitmap and changing it back to a PyCObject (and this is consistent with the fact that if I just use the first fragment of code the thing works fine)

    Any ideas? Thanks a lot!
    Orazio

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    24

    Re: Problems with a python wrapper: convert PyObject to Image

    Hi pdcb_pdcb and jplauril.

    Did you succeed in converting the Python image to CFbsBitmap and then back to a Python image? I'm also looking to do the same, but without any success so far. If you succeeded, would you mind posting/sharing the solution (extension source code, i.e., the framework without the actual image processing)?

    Thanks a bunch in advance.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    31

    Re: Problems with a python wrapper: convert PyObject to Image

    Hi phishboh,

    unfortunately, as I said in my last post, the code that I used kept crashing. I tested it a bit more and it seemed to crash at the third call to the method:

    Code:
    imOut = Image.from_cfbsbitmap(imOut)
    Maybe a memory leak? Or maybe I am doing something wrong, I don't know. What I do know is that, for the moment, Python S60 is still too young to be used for way too many applications: lack of documentation and problems with some modules are too expensive, in terms of debugging time, for me to be able to use it... just to make an example, I do computer vision and the camera module is definitely still buggy/inefficient. I know that very good programmers are working really hard on Python and I don't mean to undermine their efforts, I am only saying that it still does not work for me. So I think that from now on I will try to use only Symbian C++ instead. I also hope that someone proves me wrong.

    Sorry for not being able to help. Ciao!
    Pdcb_pdcb

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    24

    Re: Problems with a python wrapper: convert PyObject to Image

    Thanks for your reply, pdcb_pdcb, but I'm sorry to hear that you are giving up using PyS60. I still would like to get it to work.

    jplauril (or anyone else), can you please explain a bit more what you mean with:
    The easiest way to roll your own Image from CFbsBitmap* is to make a CObject myBitmap that wraps the CFbsBitmap*
    preferrably by adjusting the source code provided in the first post by pdcb_pdcb?

    Thanks a lot.

Similar Threads

  1. Opening a JPEG Image
    By ummarbhutta in forum Mobile Java Media (Graphics & Sounds)
    Replies: 8
    Last Post: 2007-02-15, 06:34
  2. how to cut some part of Image
    By mshouab in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2006-08-04, 09:05
  3. Is there any ProgressBar wrapper for python?
    By papit01 in forum Symbian
    Replies: 0
    Last Post: 2006-03-11, 19:29
  4. CodeWarrior COM problems (Python)
    By huoneusto in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 1
    Last Post: 2005-06-16, 20:28
  5. How can i manage image with python
    By darly in forum Symbian
    Replies: 1
    Last Post: 2005-02-16, 09:14

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×