python - Gettin alpha channel data from an image in Pyglet quickly -
i'm trying array alpha channel value of sprite image, using pyglet library. wrote code works:
mask = [] x in range(image.width): mask.append([]) y in range(image.height): mask[x].append(ord(image.get_region(x, y, 1, 1).get_image_data().get_data("rgba", 4)[3])) return mask
the problem it's really slow, guess i'm using wrong function retrieve alpha channel. can make faster?
update
i find solution following code faster:
rawimage = image.get_image_data() format = 'rgba' pitch = rawimage.width * len(format) pixels = rawimage.get_data(format, pitch) data = unpack("%ib" % (4 * image.width * image.height), pixels) mask = data[3::4] return mask
i don't know pyglet, i'm guessing performance issue related many queries single pixel. instead want entire image gpu in 1 call, including colour , alpha values, , extract alpha. i'd use struct.unpack
instead of ord()
.
note: code untested , purely based on example in question. there better way.
from struct import unpack ... region = image.get_region(0, 0, image.width, image.height) packed_data = region.get_image_data().get_data("rgba", 4) data = unpack("%ib" % (4 * image.width * image.height), packed_data) mask = data[3::4]
i don't think it'd worth if didn't want drag colour gpu explore copying alpha texture first (there might way gl unpack format conversion or reinterpretation).
Comments
Post a Comment