pixel access patch

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Post Reply
blendix
Posts: 51
Joined: Wed Oct 16, 2002 1:00 pm

pixel access patch

Post by blendix »

I wanted to be able to edit Images in blender from python, but this is, as you might know, not possible. So i've created a patch wich allows you to edit Images pixel by pixel.

You can find it at:
http://users.pandora.be/blendix/verse/p ... -patch.txt

The patch is still preliminary, also because I'm still not sure how the best way would be to represent the image buffer to python scripts. Currently it works like this: you can treat the Image object as if it was a sequence/mapping object. Suppose img is a Blender Image object, with a depth of 32 bits per pixel, and thus 4 bytes per pixel (it also works for 24, 16 and 8 ). You can then write:

img[ (y * img.width + x) * 4 ] # get red value of pixel at position x, y
img[ (y * img.width + x) * 4 + 3 ] # get alpha value of pixel at position x, y

# but we can do this a lot easier
img[ x, y, 0 ] # get a list with 1 item, containg the red value of pixel at position x, y
img[ x, y, 2 ] # get a list with 1 item, containg the blue value of pixel at position x, y

# we can also use slices
img[ x, y, : ] # get a list with 4 items, containg the red, green, blue and value of pixel at position x, y
img[ :, y, 1 ] # get a list containing al the green values of the y row
img[ :, :, : ] # get a list containing the complete image
img[ 10:15, 0:5, : ] # get a list of all the rgba values of the rect with offset 10, 0 and width 5, 5

# we can also use img in a for loop
for i in img:
print i

You can also assign values in a similar way. There are also some other things i would like to implement: the Image.New function, the ability to draw Blender Images with BGL, ... . Any ideas, comments? Would anybody want this functionality?

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

Post by stiv »

You might want to look at/use the Python Imaging Library ( PIL ) or the python OpenGL module ( pyOGL ). They already have image manipulation and opengl functions.

If you just want to edit images:

import os
os.system( 'gimp somefile.jpg')

Image.Load( 'somefile.jpg')

verence
Posts: 33
Joined: Thu Nov 07, 2002 12:42 pm

Re: pixel access patch

Post by verence »

i think a two-way conversion tween the blender image data and the BGL.Buffer would be the easiest way. a BGL.Buffer you can draw in python (glDrawPixels) and it is easy to change. and this must already be done inside blender (uv editor), but it is not visible to python :cry: .

yust my 0.5€

verence

blendix
Posts: 51
Joined: Wed Oct 16, 2002 1:00 pm

Post by blendix »

I might have expressed myself wrong, but I'm certainly not planning to make an image editor or something, I'm using this to create a experimental python script wich connects blender Images to verse Bitmaps. I posted this because i couldn't really think of another appliance than my script.

Anyaway, I think using BGL.Buffer is indeed the way to go. I know nearly nothing about how OpenGL really works, so I would need to look into this further. Anyway, for now I'll be using my patch, since I focusing more on the verse side of things.

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

Post by stiv »

Ahhhh, I misunderstood completely! Being able to get/store images in verse is definitely cool! Do you see Images being loaded out of verse on demand the same way they are loaded from the filesystem?

Regarding the patch, may I suggest that perhaps better names could be found for the Image.ass_item() and Image.ass_subscript() methods. 'Ass item' sounds a little funny in English.

blendix
Posts: 51
Joined: Wed Oct 16, 2002 1:00 pm

Post by blendix »

Well, it would we possible to load images from verse as if they were on a filesystem. A problem might be that with verse, it is impossible to know wether you have received the complete image or not. But that is in my view not really what verse is for. Verse allows the images to be updated in realtime, when some other verse client changes them. You could for example paint your textures in the gimp and see them being updated in realtime on your model in blender (wich is actually the thing I am trying to accomplish).

About the ass_item name :). I don't think this name is visible to the python programmer, since this the name of an internal C function. This name is, as far as I know, pretty much standard in the Python / Blender Python API code. To assign values you could, for example, use:

img[ 0, 0, : ] = [255, 196, 0, 255]

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

Post by stiv »

This name is, as far as I know, pretty much standard in the Python / Blender Python API code.
You are right. I just grepped thru the code. What a surprise! Guess I made an 'item' of myself, eh?

Post Reply