def create_block_frames(trans, path, blocksize=8, debug=False): """ This function creates a series of frames whereas each frame only contains one field from the DCT matrix. """ B = blocksize channelrows, channelcols = trans.shape blocksV = channelrows / B blocksH = channelcols / B count = 1 if debug: B = 2 # create the mask. There is only one white pixel within each block. # serialize the block using a wave pattern for (x, y) in wave(B): blockmask = np.zeros((blocksize, blocksize), np.float32) blockmask[y][x] = 1.0 mask = np.tile(blockmask, (blocksV, blocksH)) # apply the mask to the image: trans_masked = np.multiply(trans, mask) # decode the 'image': back = np.zeros((channelrows, channelcols), np.uint8) for row in range(blocksV): for col in range(blocksH): dequantblock = trans_masked[row*blocksize:(row+1)*blocksize, col*blocksize:(col+1)*blocksize] currentblock = np.round(cv2.idct(dequantblock))+128 currentblock[currentblock > 255] = 255 currentblock[currentblock < 0] = 0 back[row*blocksize:(row+1)*blocksize, col*blocksize:(col+1)*blocksize] = currentblock cv2.cv.SaveImage('%s/dct_%05d.png' % (path, count), cv2.cv.fromarray(back)) count += 1
def create_single_frame(path, number): """ This creates the animation frames using a single block. The frames are RGBA pngs. """ trans = dct_demo(crop=False, asset='assets/zhaw-gray.png') blocksize = 64 count = 1 for (x, y) in wave(blocksize)[:number]: mask = np.zeros((blocksize, blocksize), np.float32) mask[y][x] = 1.0 trans_masked = np.multiply(trans, mask) frame = np.round(cv2.idct(trans_masked))+128 frame[frame > 255] = 255 frame[frame < 0] = 0 cv2.cv.SaveImage('%s/dct_%05d.png' % (path, count), cv2.cv.fromarray(frame)) count += 1