rna Read Pixel Value (2.5 api)

I guess I might as well try and make my first 2.5 script.

Does anyone know how to get the pixel value from an image texture using the Blender 2.5 API?

I just found out how to read and write the pixels in an image.
Still testing though
Sorry the Python is mostly in dutch and is somewhat clums. I; just stumbled over your question and I have no time to translate the lot now.
Annyway here it is:

import Blender
from Blender import Texture,Image,Material
scene = Blender.Scene.GetCurrent()

Get proper object

for ob in scene.objects:
print ob
if ob.name == ‘Bomen’:
bomen = ob
print 'Gevonden bomen? = ', bomen

#Get proper material
materials = bomen.getMaterials(0)
for mat in materials:
print mat
if mat.name == ‘MijnKaart’:
MijnKaart = mat
print 'Materiaal Kaart gevonden = ', MijnKaart

#Get proper MaterialTexture (MTex)
Textures = MijnKaart.getTextures()
for Mtex in Textures:
print Mtex
if Mtex <> None:
print Mtex.tex.name
if Mtex.tex.name == ‘Kaart’:
MijnTexture = Mtex
print 'Texture van de kaart gevonden = ', MijnTexture
print 'BestandsNaam van kaart = ', MijnTexture.tex.image.filename

#Get size of Image
Kaart = MijnTexture.tex.image
MaxCoord = Kaart.getMaxXY()
MaxX = MaxCoord[0]
MaxY = MaxCoord[1]
print ‘Grootte van deze afbeelding = ‘,MaxX,’,’,MaxY

#Read all pixels in the image
for xpos in range (0,MaxX-1):
for ypos in range (0,MaxY-1):
rgbA = Kaart.getPixelHDR(xpos,ypos)
#print xpos,’,’,ypos,’,’,rgbA (don’t run this line; printing take a lot of time)

if you do it with english name and working properly

can you upload a new working script
this might a good example for further use in futur!

Happy blendering

I shall do so in a view day’s.

But this isn’t for Blender 2.5 is it?

I thought “import Blender” no longer works in 2.5?

You are right, I haven 2.49a
Didn’t know there was a 5.0

You are right, I have 2.49a
Didn’t know there was a 5.0

Atom,
pixel access is not in the API yet.

Hey Blenderers,

I got things working now, though maybe not like some of you like it to.

There is indeed no way to get the RGB-value of a point on an object on a simpel way
(say: you throw a ray on the object, get its hitposition and get its RGB-value).

But there is a way to get the RGBA-value of pixels on a plane.
That is what this reply is about.

Ok what did I do:

  • I took a plane and gave it a texture (Orco-mapped; the picture is taken from a game I like to play)
  • The plane must be somewhere in the XY-plane of the Blender-3D-world, soo for all point on the plane the Z-coördinate is zero.
  • The ObjectData must be in the center of the plane (eg when translation is activated: the arrows are in the
    middel of the plane)
    _ To get the RGBA-value of a point in the plane I use a MouseOver- and a MouseClick-Sensor
  • If Clicked on the plane my script is activated.
    It then:
    a. gets the hitposition of the MouseOver-sensor (these are world-coordinates),
    b. gets the material of ‘MyObject’ eg. the plane I use,
    c. gets the texture I used on the object (´MyTexture´),
    d. gets the size of the picture used as a texture,
    e. gets the size of the object clicked on,
    f. From these last two coordinates and the position of the object in the BlenderWorld it calculates the
    position where the object was hit in Local-coordinates.
    g. From these coodinates, the size of the object and the size of the picture (= texture-picture) it
    calculates the coordinates (Xpos,Ypos) of the corresponding pixel in the picture.
    h. Using these last coordinates it uses the picture.getPixelI-method to get the RGBA-value of this position.

Okay, this is not straight forward simple, but it works.
I build in a lot of debug-controls and left them in, so you can see what the results are during the running of the script (also check the console).

To test the lot I made another script (SendData) that sends the RGB-value to a memory-mapped file called ‘MyMapping’.
This file is shared with a little program called ‘GetBlenderRGB.exe’. This is an ‘always-on-top’ program that shows you the RGB-values just found and the corresponding color.

To test for your self:

  • download both the next files:

http://members.home.nl/j.j.balster/Blender/GetBlenderRGB.exe

http://members.home.nl/j.j.balster/Blender/Pick_RGBA.blend

  • first start GetBlenderRGB.exe (it will open the memory-mapped file),
  • then start Blenders GE with my .blend-file,
  • start the game-engine,
  • Start clicking on the plane.
    (You could also change view to camera and try again).

I hope this works as well for you as it worked for me and satisfies some of your needs.

Greeting to you all

JBal

I just want the pixel value of an image texture, not an object in the scene. Say I have a 64x64 image. I want the RGBA value for any given coordinate, like 32,17. I also would need the width and height of the image as well.

I basically need all the code for the material portion as well.

Say I have the name of a material, and I am talking about API 2.5, I will need to be able to fetch the material and determine if there is a texture associated with it. Then I need to be able to examine the texture and see if it’s type is an image. Then I need to examine the image for the width, height and and RGBA value for a given pixel within the image itself.

I might misunderstand you, but is’nt this just what my script does?
(appart from the check for the pressence of a picture. I skip this because I know for sure there is a picture).

JBal

by the way:
Did you run the test?
What do you think of it?