New Python Function - Image

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

mchs3d
Posts: 0
Joined: Thu Feb 03, 2005 6:05 am
Location: Loveland, CO

New Python Function - Image

Post by mchs3d »

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"},

mchs3d
Posts: 0
Joined: Thu Feb 03, 2005 6:05 am
Location: Loveland, CO

Post by mchs3d »

BTW is returns a precision float from 0 - 1 not from 0 -255.

kakapo
Posts: 0
Joined: Sat Sep 04, 2004 2:32 pm

Post by kakapo »

will this be in the next blender release? i hope so!

ideasman
Posts: 0
Joined: Tue Feb 25, 2003 2:37 pm

Post by ideasman »

Please add this to Blenders patch tracker (if you havnt alredy) -
- Cam

mchs3d
Posts: 0
Joined: Thu Feb 03, 2005 6:05 am
Location: Loveland, CO

Post by mchs3d »

Yah, I've been procrastinating adding to the tracker... :lol:

mchs3d
Posts: 0
Joined: Thu Feb 03, 2005 6:05 am
Location: Loveland, CO

Post by mchs3d »

The code is up on the tracker now. And it's updated (for the most part :D ).

joeri
Posts: 96
Joined: Fri Jan 10, 2003 6:41 pm
Contact:

Post by joeri »

Any change of getting this into a BF release?
Looks very usefull.

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv »

joeri wrote:Any change of getting this into a BF release?
Not without some documentation. Don't make me give my "An API is a documented interface" speech.

mchs3d
Posts: 0
Joined: Thu Feb 03, 2005 6:05 am
Location: Loveland, CO

Post by mchs3d »

Yes, OK, I'll post documentation later today :roll:

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv »

mchs3d wrote:Yes, OK, I'll post documentation later today :roll:
Good. While you are at it, go ahead and add the .blend or script you used to test with.

mchs3d
Posts: 0
Joined: Thu Feb 03, 2005 6:05 am
Location: Loveland, CO

Post by mchs3d »

It's all up except for a script or .blend file.

mchs3d
Posts: 0
Joined: Thu Feb 03, 2005 6:05 am
Location: Loveland, CO

Post by mchs3d »

It's been committed! :D

mchs3d
Posts: 0
Joined: Thu Feb 03, 2005 6:05 am
Location: Loveland, CO

Post by mchs3d »

Also, I have a Wiki page up, so any more discussion can go there now.

kakapo
Posts: 0
Joined: Sat Sep 04, 2004 2:32 pm

Post by kakapo »

do all images in blender have an alpha channel? how could i find out if the returned alpha should be used or not in my exporter?

mchs3d
Posts: 0
Joined: Thu Feb 03, 2005 6:05 am
Location: Loveland, CO

Post by mchs3d »

Well, yes, essentially all images in Blender have an Alpha channel. Typically, however, if the alpha is not being used it will be set to 1.0. Check the file format you are exporting to and see if it supports alpha.

Post Reply