Search code examples
pythonpython-imaging-libraryframebuffer

Copy and paste in framebuffer


I am drawing a blue rectangle in framebuffer, and I want to erase it again. Before I draw the rectangle I copy the background where the rectangle will be pasted, and paste it over the rectangle after 2 seconds. The rectangle is not erased.

#!/usr/bin/env python

import numpy as np
import time
from PIL import Image
from io import BytesIO

h, w, c = 1024, 1280, 4
fb = np.memmap('/dev/fb0', dtype='uint8',mode='w+', shape=(h,w,c))
x0, y0 = 50, 200
w, h = 300, 400

# Copy backbround:      Does not work?
n0 = fb.read[y0:y0+h, x0:x0+w]

# Paste blue rectangle to framebuffer:      This works.
img = Image.new('RGBA', size=(w, h), color=(255,0,0,255))
n = np.array(img)
fb[y0:y0+h, x0:x0+w] = n

# Erase image:       Does not work?
time.sleep(2)
fb[y0:y0+h, x0:x0+w] = n0

What am I doing wrong? If I paste n0 to another place in framebuffer, I get a blue rectangle, not a black one.


Solution

  • I solved it myself by using np.copy:

    import numpy as np
    import time
    from PIL import Image
    
    h, w, c = 1024, 1280, 4
    fb = np.memmap('/dev/fb0', dtype='uint8',mode='w+', shape=(h,w,c))
    x0, y0 = 50, 200
    w, h = 300, 400
    
    # Copy background:
    ### n0 = fb.read[y0:y0+h, x0:x0+w]
    n0 = np.copy(fb[y0:y0+h, x0:x0+w])
    
    # Paste blue rectangle to framebuffer:      This works.
    img = Image.new('RGBA', size=(w, h), color=(255,0,0,255))
    n = np.array(img)
    fb[y0:y0+h, x0:x0+w] = n
    
    # Erase image:
    time.sleep(2)
    ### fb[y0:y0+h, x0:x0+w] = n0
    fb[y0:y0+h, x0:x0+w] = np.copy(n0)