Get pixel of an image

how can I get the color value of any pixel of an image?

thx :slight_smile:

Work e.g. as follows
start Blender (2.59 used!) and give the default cube a material and a texture (an image!)
Open Scripting to see the Python console and see what I did (from a png you get (r,g,b,a) one after the other, so you have to calculate row and column yourself)

>>> t =[0]
>>> dir(t)
[‘doc’, ‘module’, ‘slots’, ‘animation_data’, ‘animation_data_clear’, ‘animation_data_create’, ‘bl_rna’, ‘checker_distance’, ‘color_ramp’, ‘contrast’, ‘copy’, ‘crop_max_x’, ‘crop_max_y’, ‘crop_min_x’, ‘crop_min_y’, ‘extension’, ‘factor_blue’, ‘factor_green’, ‘factor_red’, ‘filter_eccentricity’, ‘filter_probes’, ‘filter_size’, ‘filter_type’, ‘image’, ‘image_user’, ‘intensity’, ‘invert_alpha’, ‘library’, ‘name’, ‘node_tree’, ‘repeat_x’, ‘repeat_y’, ‘rna_type’, ‘saturation’, ‘tag’, ‘type’, ‘update_tag’, ‘use_alpha’, ‘use_calculate_alpha’, ‘use_checker_even’, ‘use_checker_odd’, ‘use_color_ramp’, ‘use_derivative_map’, ‘use_fake_user’, ‘use_filter_size_min’, ‘use_flip_axis’, ‘use_interpolation’, ‘use_mipmap’, ‘use_mipmap_gauss’, ‘use_mirror_x’, ‘use_mirror_y’, ‘use_nodes’, ‘use_normal_map’, ‘use_preview_alpha’, ‘user_clear’, ‘users’]

>>> im = t.image
>>> im[‘basRelief.png’]

>>> dir(im)
[‘doc’, ‘module’, ‘slots’, ‘animation_data_clear’, ‘animation_data_create’, ‘bindcode’, ‘bl_rna’, ‘copy’, ‘depth’, ‘display_aspect’, ‘field_order’, ‘file_format’, ‘filepath’, ‘filepath_raw’, ‘fps’, ‘frame_end’, ‘frame_start’, ‘generated_height’, ‘generated_type’, ‘generated_width’, ‘gl_free’, ‘gl_load’, ‘has_data’, ‘is_dirty’, ‘library’, ‘mapping’, ‘name’, ‘packed_file’, ‘pixels’, ‘reload’, ‘resolution’, ‘rna_type’, ‘save’, ‘save_render’, ‘size’, ‘source’, ‘tag’, ‘tiles_x’, ‘tiles_y’, ‘type’, ‘update’, ‘update_tag’, ‘use_animation’, ‘use_clamp_x’, ‘use_clamp_y’, ‘use_fake_user’, ‘use_fields’, ‘use_generated_float’, ‘use_premultiply’, ‘use_tiles’, ‘user_clear’, ‘users’]

>>> len(im.pixels)

>>> im.size[:]
(640, 360)

Hope this helps

thank you very much
but how can I get the color of a fixed pixel?

I tried like this:

>>> im.pixels[20:20]

then I don’t get any color
it only works like this:

>>> im.pixels[795500]

but I don’t get the color from rgb and I have to write the pixel number but I’d like to get it with x and y.

do you know how that works?


the pixel array gives you r0,g0,b0,a0,r1,g1,b1,a1, ETC

so that means you have to use width and heigth (size) of the image to know At which index of the pixel array you find the 4 values
for ONE pixel (i, i+1, i+2,i+3) thus
ri,gi,bi,ai = pixel[i:i+4]
IF i is correct computed! (I think i % 4 == 0 must be True)

It depends of what you want to know … a name of the colour ot a pixel? (bah horrible lookup in colournames with there r,g,b, values, MEANING I do not know a better method)