My Python script needs to read RGBA values from a specific pixel of a texture bitmap.
How can I do this in Blender?
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It only takes a minute to sign up.Sign up to join this community
import bpy D = bpy.data image_file = 'Image.png' # this refers to an image file loaded into Blender img = D.images[image_file] # we need to know the image dimensions width = img.size height = img.size print ( "Image size: ", width, " x ", height ) # define location of the target pixel we want to read target = [150, 33] # X, Y print ("Target vector: ", target) # target.Y * image width + target.X * 4 (as the table Pixels contains separate RGBA values) index = ( target * width + target ) * 4 print ("Field index: ", index) # aggregate the read pixel values into a nice array pixel = [ img.pixels[index], # RED img.pixels[index + 1], # GREEN img.pixels[index + 2], # BLUE img.pixels[index + 3] # ALPHA ] # print the values for debug print ("R: ", pixel) print ("G: ", pixel) print ("B: ", pixel) print ("A: ", pixel)
I wrote this based on articles from this blog: http://blenderscripting.blogspot.com/2012/08/adjusting-image-pixels-internally-in.html