Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

August 07, 2012

adjusting image pixels (walkthrough)

Experiment with small images

For example experiment along using this image (24px wide, 7px high)

Number of pixels

This shows how to get the number of pixels in an image
import bpy

D = bpy.data

test_file = 'firefly_test_pattern_2.tga'
img = D.images[test_file]

# double division symbol forces integer result.
# 'pixels' here are ungrouped, every sequence of 
# 4 consecutive items in pixels is an rgba when
# combined
print( len(img.pixels)//4)

Dimensions

This shows how to get the width and height of an image
import bpy

D = bpy.data

test_file = 'firefly_test_pattern_2.tga'
img = D.images[test_file]

#alias if you can
w = width = img.size[0]
h = height = img.size[1]

print('image width: %d' % w)
print('image height: %d' % h)

Indexing!

This shows how to extract the array of pixels into a new list, for faster access.
import bpy
D = bpy.data

test_file = 'firefly_test_pattern_2.tga'
img = D.images[test_file]

# work on a copy instead, it's much faster
pixels = list(img.pixels)

# this collects every 4 items in pixels and stores them inside a tuple
# and sticks all tuples into the grouped_list
grouped_list = [pixels[ipx:ipx+4] for ipx in range(0, len(pixels), 4)]

print(len(grouped_list))
print(grouped_list)

coordinates from pixel index and back

This shows how to extract the array of pixels into a new list, for faster access.
import bpy
D = bpy.data

test_file = 'firefly_test_pattern_2.tga'
img = D.images[test_file]
pixels = list(img.pixels)
grouped_list = [pixels[ipx:ipx+4] for ipx in range(0, len(pixels), 4)]

some convenience functions

contrast and compare the above, with the following slightly more convenient ways to name objects. Some objects deserve short names.
import bpy
D = bpy.data
test_file = 'firefly_test_pattern_2.tga'
# my test image is 24 px wide and 7 px heigh
# download here if you want to test along.
#alias if you can
img = D.images[test_file]
# operate on a copy, for fast access
pxs = pixels = list(img.pixels)
w = width = img.size[0]
h = height = img.size[1]
num_pixels = len(pxs)
# grouped_list = [pxs[ipx:ipx+4] for ipx in range(0, num_pixels, 4)]
gl = grouped_list = [pxs[i:i+4] for i in range(num_pixels)[::4]]
print(gl)
# row, col
def idx_to_co(idx, width):
r = int(idx / width)
c = idx % width
return r, c
def co_to_idx(r, c, width):
return r * width + c
# 0,0 is first pixel bottom left, index should be 0
print( co_to_idx(0, 0 ,w) ) # >>> 0
# 1,1 is 1 px up and 1px to the right, the index should be 24+1
print( co_to_idx(1, 1 ,w) ) # >>> 25
# 6, 23 is top right, because we count from 0. index should be the same
# as the number of grouped pixels in the image minus one. ( 24*7)-1
print( co_to_idx(6,23, w) ) # >>> 167
# and the other way around too
# idx 0 -> coordinate 0 ,0
# idx 25 -> coordinate 1 ,1
# idx 167 -> coordinate 6,23
print( idx_to_co(0, w) ) # >>> 0, 0
print( idx_to_co(25, w) ) # >>> 1, 1
print( idx_to_co(167, w) ) # >>> 6,23
and
# y,x = r,c # you might be familiar with the other way around x,y = c,r
import bpy
D = bpy.data
img = D.images['Chester mG_fireflies.tga']
pxs_slow = pixels = img.pixels
#recast, faster
pxs = list(pxs_slow)
w = width = img.size[0]
h = height = img.size[1]
num_pixels = len(pxs)
#equivalent statements
#gl = grouped_list = [pxs[i:i+4] for i in range(0, num_pixels, 4)]
gl = grouped_list = [pxs[i:i+4] for i in range(num_pixels)[::4]]
def idx_to_co(idx, width):
r = int(idx / width)
c = idx % width
return r, c
def co_to_idx(r, c, width):
return r * width + c
def rgba_from_index(idx, pxs):
start_raw_index = idx * 4
return pxs[start_raw_index:start_raw_index+4]
def is_firefly(rgba):
if (any(rgba) == 1.0) and (sum(rgba) > 3.8):
return True
""" found coordinates to look at """
pixels_of_interest = [
[424,706], [178,864], [67,68], [58,110],
[16,123], [21,169], [81,218], [49,422],
[56,603], [29,637], [60,643], [31,686],
[6, 720], [28,764]]
print('-----------------')
for r,c in pixels_of_interest:
idx = co_to_idx(r, c, w)
rgba = rgba_from_index(idx, pxs)
print(r,c, '-->', rgba,'-->', is_firefly(rgba))
view raw yx.py hosted with ❤ by GitHub