Skip to content

Instantly share code, notes, and snippets.

@vfig
Created November 10, 2021 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfig/b59659c27770f0fc696e3235d53e0ecb to your computer and use it in GitHub Desktop.
Save vfig/b59659c27770f0fc696e3235d53e0ecb to your computer and use it in GitHub Desktop.
# blitting (y, x; remember y is bottom up in blender, which is fine)
atlas = bpy.data.images.new(name='Atlas', width=256, height=256, alpha=False, float_buffer=False)
px0 = np.array(im0.pixels)
px1 = np.array(im1.pixels)
pxa = np.array(atlas.pixels)
px0.shape = (64,64,4)
px1.shape = (64,64,4)
pxa.shape = (256,256,4)
pxa[ 0:64, 0:64, : ] = px0
pxa[ 0:64, 64:128, : ] = px1
atlas.pixels = pxa.reshape((-1,))
# equivalent to: pxa.flatten(), but i think .flatten() always copies?
# reading raw rgb bytes into an array
raw = open('e:/temp/rails.raw', 'rb').read()
rgb = np.frombuffer(raw, dtype=np.uint8) # can take count, offset kw
rgb.shape = (256,256,3)
# rgb[0][0] is: array([158, 151, 141], dtype=uint8)
# expanding to rgba
rgba = np.insert(rgb, 3, 255, axis=2)
# rgba.shape is: (256, 256, 4)
# rgba[0][0] is: array([158, 151, 141, 255], dtype=uint8)
# expanding paletted data to rgb(a):
# here using rgb ega half-palette with uint8, but this could be rgba floats
pal = np.array([[0,0,0],[0,0,128],[0,128,0],[0,128,128],[128,0,0],[128,0,128],[128,64,0],[128,128,128]], dtype='uint8')
# paletted 2x11 image:
imp = np.array([
[0,1,2,3,4,5,4,3,2,1,0],
[7,6,5,4,3,2,3,4,5,6,7]], dtype='uint8')
# imp.shape is: (2, 11)
rgb = pal[imp]
# rgb.shape is: (2, 11, 3)
# rgb is:
# array([[[ 0, 0, 0],
# [ 0, 0, 128],
# [ 0, 128, 0],
# [ 0, 128, 128],
# [128, 0, 0],
# [128, 0, 128],
# [128, 0, 0],
# [ 0, 128, 128],
# [ 0, 128, 0],
# [ 0, 0, 128],
# [ 0, 0, 0]],
#
# [[128, 128, 128],
# [128, 64, 0],
# [128, 0, 128],
# [128, 0, 0],
# [ 0, 128, 128],
# [ 0, 128, 0],
# [ 0, 128, 128],
# [128, 0, 0],
# [128, 0, 128],
# [128, 64, 0],
# [128, 128, 128]]], dtype=uint8)
# for 16-bit 1555 rgb -> 24-bit 888 rgb, probably check out:
# np.bitwise_and() and np.right_shift()
# repeating greyscale data into rgb channels:
a = np.array([1, 2, 3, 4, 5])
a.shape = (-1, 1)
# a is:
# array([[1],
# [2],
# [3],
# [4],
# [5]])
b = np.repeat(a, repeats=3, axis=1)
# b is:
# array([[1, 1, 1],
# [2, 2, 2],
# [3, 3, 3],
# [4, 4, 4],
# [5, 5, 5]])
# expanding a square array to 2x width, 2x height:
np.block([[q2,q3],[q0,q1]])
# array([[222, 222, 222, 222, 333, 333, 333, 333],
# [222, 222, 222, 222, 333, 333, 333, 333],
# [222, 222, 222, 222, 333, 333, 333, 333],
# [222, 222, 222, 222, 333, 333, 333, 333],
# [ 0, 0, 0, 0, 111, 111, 111, 111],
# [ 0, 0, 0, 0, 111, 111, 111, 111],
# [ 0, 0, 0, 0, 111, 111, 111, 111],
# [ 0, 0, 0, 0, 111, 111, 111, 111]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment