I would like to display a NumPy array as an image on the screen from a Python script. I need to do this without starting X, so I can't use OpenCV, PIL, etc.
Is it possible to do this using fbi in a subprocess started in the Python script?
Below is what I have tried to do. I expected to see the images in the 'images' folder displayed with a 1-second delay between them, but instead I get the error BrokenPipeError: [Errno 32] Broken Pipe
import os
import time
import numpy as np
from PIL import Image
from subprocess import Popen, PIPE
# Define the folder containing the images
folder_path = 'images'
# Loop through each file in the folder
for filename in os.listdir(folder_path):
# Check if the file is an image
if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.png'):
# Load the image into a NumPy array using PIL
image = np.array(Image.open(os.path.join(folder_path, filename)))
# Edit the image as desired
edited_image = image
# Build the fbi command to display the edited image in full-screen mode
command = 'sudo fbi -T 1 -noverbose -a -t 1 -u -blend 500 - '
# Create a new process for fbi, and pass the edited image to its standard input
with Popen(command.split(), stdin=PIPE) as process:
process.stdin.write(Image.fromarray(edited_image).tobytes())
process.communicate() # Wait for fbi to complete
# Wait for 1 second before displaying the next image
time.sleep(1)
Thanks in advance!
I am still confused by your question, but now I think you are generating Numpy arrays rather than images and want to send them to the frame buffer from Python - maybe on a Raspberry Pi?
Here is an example of that:
Obviously the code can be refactored to be tidier, but I wanted to just demonstrate the techniques rather than confuse anyone with super-compact, efficient, non-repetitive tricky code.