New Python Function - Image
Moderators: jesterKing, stiv
New Python Function - Image
Here is the patch that allows individual pixel colors to be read from an image. To use it, you must use it like this:
image=Image.Load("C:\sample.jpg")
test=image.getPixelColor(x,y)
# test returns a list [r,g,b,a]
Please try it out and give me your feedback.
Index: source/blender/python/api2_2x/Image.c
===========================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/python/api2_2x/Image.c,v
retrieving 1.94
diff -u -p -r1.94Image.c
+++source/blender/python/api2_2x/Image.c 21 Feb 2005 18:39:20 -0700
@@ +218,1@@
static PyObject *Image_getPixelColor(BPy_Image * self, PyObject *args)
{
PyObject *attr;
Image *image=self->image;
char* pixel;
int ptr;
int x=0;
int y=0;
if( !PyArg_ParseTuple( args, "ii", &x, &y ) )
return EXPP_ReturnPyObjError( PyExc_TypeError,
"expected 2 integers" );
if( !image->ibuf||!image->ibuf->rect ) /* if no image data available */
load_image( image, IB_rect, "", 0 ); /* loading it */
if( !image->ibuf||!image->ibuf->rect ) /* didn't work */
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"couldn't load image data in Blender" );
if(x>image->ibuf->x||y>image->ibuf->y||image->ibuf->xorig>x||image->ibuf->yorig>y)
return EXPP_ReturnPyObjError( PyExc_RuntimeError, "x or y is out of range");
ptr=(x + y* image->ibuf->x)*4;
pixel=(char*)image->ibuf->rect;
attr = Py_BuildValue( "[f,f,f,f]", ((float)pixel[ptr])/255,((float)pixel[ptr+1])/255,((float)pixel[ptr+2])/255,((float)pixel[ptr+3]/255) );
if( attr )
return attr;
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"couldn't get pixel colors" );
};
@@++292,1@@
static PyObject *Image_getPixelColor(BPy_Image * self, PyObject *args); /* by Austin Benesh */
@@++299,4@@
{"getPixelColor", ( PyCFunction ) Image_getPixelColor, METH_VARARGS,
"(int) X,Y"},
image=Image.Load("C:\sample.jpg")
test=image.getPixelColor(x,y)
# test returns a list [r,g,b,a]
Please try it out and give me your feedback.
Index: source/blender/python/api2_2x/Image.c
===========================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/python/api2_2x/Image.c,v
retrieving 1.94
diff -u -p -r1.94Image.c
+++source/blender/python/api2_2x/Image.c 21 Feb 2005 18:39:20 -0700
@@ +218,1@@
static PyObject *Image_getPixelColor(BPy_Image * self, PyObject *args)
{
PyObject *attr;
Image *image=self->image;
char* pixel;
int ptr;
int x=0;
int y=0;
if( !PyArg_ParseTuple( args, "ii", &x, &y ) )
return EXPP_ReturnPyObjError( PyExc_TypeError,
"expected 2 integers" );
if( !image->ibuf||!image->ibuf->rect ) /* if no image data available */
load_image( image, IB_rect, "", 0 ); /* loading it */
if( !image->ibuf||!image->ibuf->rect ) /* didn't work */
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"couldn't load image data in Blender" );
if(x>image->ibuf->x||y>image->ibuf->y||image->ibuf->xorig>x||image->ibuf->yorig>y)
return EXPP_ReturnPyObjError( PyExc_RuntimeError, "x or y is out of range");
ptr=(x + y* image->ibuf->x)*4;
pixel=(char*)image->ibuf->rect;
attr = Py_BuildValue( "[f,f,f,f]", ((float)pixel[ptr])/255,((float)pixel[ptr+1])/255,((float)pixel[ptr+2])/255,((float)pixel[ptr+3]/255) );
if( attr )
return attr;
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
"couldn't get pixel colors" );
};
@@++292,1@@
static PyObject *Image_getPixelColor(BPy_Image * self, PyObject *args); /* by Austin Benesh */
@@++299,4@@
{"getPixelColor", ( PyCFunction ) Image_getPixelColor, METH_VARARGS,
"(int) X,Y"},