getPixelRGBA(image, x, y)

a year ago i tried to write a script to get the pixel-RGBs from a texture , but back then there was no way do to that (inside blenders python api)

basedow just posted a request for a very similar problem and i got curious again. so i wrote this little script. maybe someone can use it. i would have needed it a year ago… if only timetravel would be possiple. :slight_smile:

you need to add a texture of the type ‘image or movie’ and then rename ‘image_name.png’ in the code to your imagename.

import bpy
from math import ceil

def getPixelRGBA(img, x ,y):
    """
    img ... image:
    bpy.data.images['image_name.png']
    x   ... x-coordinate
    y   ... y-coordinate
    
    returns a tuple (R,G,B,A)
    R, G, B, A = {0, ..., 255}
    
    w   ... width
    h   ... height
    coordinates:
    (0,h) (1,h).......(w,h)
    (0,h-1)................
    .......................
    (0,1)..................
    (0,0).....(w-1,0) (w,0)
    """
    
    width = img.size[0]        
    height = img.size[1]
    
    
        
    if x < 0 or x >= width:
        print('x=' + str(x) + ', coordinate is out of range!'
              + ' Valid x: 0 <= x < ' + str(width -1))
        return None
    
    if y < 0 or y >= height:
        print('y=' + str(y) + ', coordinate is out of range!'
              + ' Valid y: 0 <= y < ' + str(height -1))
        return None
    
    ret = (    
          ceil(255 * img.pixels[((y * width + x) * 4) + 0]),
          ceil(255 * img.pixels[((y * width + x) * 4) + 1]),
          ceil(255 * img.pixels[((y * width + x) * 4) + 2]),
          ceil(255 * img.pixels[((y * width + x) * 4) + 3])
          )
    
    return ret

def getPixelList(img):
    width = img.size[0]
    height = img.size[1]
    
    for i in range(height):
        for j in range(width):
            ret = (    
                  ceil(255 * img.pixels[((i * width + j) * 4) + 0]),
                  ceil(255 * img.pixels[((i * width + j) * 4) + 1]),
                  ceil(255 * img.pixels[((i * width + j) * 4) + 2]),
                  ceil(255 * img.pixels[((i * width + j) * 4) + 3])
                  )
            yield ret

image = bpy.data.images['image_name.png']

print('----------------------------------')
print(getPixelRGBA(image, 4, 1))

print('----------------------------------')
for i in getPixelList(image):
    print(i)

print('----------------------------------')    
print(list(getPixelList(image)))

niko
.

Thanks, this helped me out a lot.

here’s what I ended up using


def arrco(x, y, wd):
    return y * wd + x

def getpixel(img, x ,y):
    co = arrco(x, y, img.size[0])
    ret = (    
          ceil(255 * img.pixels[((co) * 4) + 0]),
          ceil(255 * img.pixels[((co) * 4) + 1]),
          ceil(255 * img.pixels[((co) * 4) + 2]),
          ceil(255 * img.pixels[((co) * 4) + 3])
          )
    return ret

def setpixel(img, x ,y, rgba):
    co = arrco(x, y, img.size[0])
    img.pixels[((co) * 4) + 0] = rgba[0]
    img.pixels[((co) * 4) + 1] = rgba[1]
    img.pixels[((co) * 4) + 2] = rgba[2]
    img.pixels[((co) * 4) + 3] = rgba[3]