first working with live preview

This commit is contained in:
Patrick Haßel 2025-06-27 20:59:39 +02:00
parent 0e7ed030fe
commit 0502f2256d
5 changed files with 46 additions and 20 deletions

View File

@ -1,5 +1,6 @@
import os.path import os.path
import time import time
from typing import List from typing import List
from PIL import Image from PIL import Image
from escpos.escpos import Escpos from escpos.escpos import Escpos
@ -24,10 +25,25 @@ class State(Enum):
CHOOSE = 4 CHOOSE = 4
QR = 5 QR = 5
res = (1024,768)
class Fotobox: class Fotobox:
def __init__(self, camera): def __init__(self, camera):
camera.vflip = False
camera.hflip = True
camera.resolution = (2592, 1944)
camera.rotation = 270
camera.preview_configuration.main.size = res
camera.preview_configuration.main.format = 'BGR888'
camera.configure("preview")
#####################
# here configure still capture
self._capture_config = camera.create_still_configuration()
camera.still_configuration.main.size = (2592, 1944)
#####################
camera.start()
self._camera = camera self._camera = camera
self._running: bool = True self._running: bool = True
self._state: State = State.IDLE self._state: State = State.IDLE
@ -64,16 +80,25 @@ class Fotobox:
def _shooting_callback(self): def _shooting_callback(self):
print("SHOT: %d/%d" % (self._shooting_timer.current(), SHOOTING_COUNT)) print("SHOT: %d/%d" % (self._shooting_timer.current(), SHOOTING_COUNT))
for i in range(24):
array = self._camera.capture_array()
img = pygame.image.frombuffer(array.data, res, 'RGB')
screen.blit(img, (0, 0))
pygame.display.update()
screen.fill((255, 255, 255)) screen.fill((255, 255, 255))
pygame.display.flip() pygame.display.flip()
if not DEBUG: if not DEBUG:
time.sleep(0.2) time.sleep(0.2)
stream = io.BytesIO() frame = self._camera.switch_mode_and_capture_array(self._capture_config, "main")
self._camera.capture(stream, format='rgb') image = Image.fromarray(frame)
stream.seek(0) self._photos.append(Photo(self._shooting_timer.current(), image))
self._photos.append(Photo(self._shooting_timer.current(), Image.open(stream)))
#stream = io.BytesIO()
#self._camera.capture(stream, format='rgb')
#stream.seek(0)
#self._photos.append(Photo(self._shooting_timer.current(), Image.open(stream)))
screen.fill((0, 0, 0)) screen.fill((0, 0, 0))
pygame.display.flip() pygame.display.flip()
@ -184,7 +209,10 @@ class Fotobox:
print(new_state) print(new_state)
self._state = new_state self._state = new_state
if new_state == State.COUNTDOWN: if new_state == State.COUNTDOWN:
self._camera.start_preview() #config = self._camera.create_preview_configuration(main={"size": (PHOTO_WIDTH, PHOTO_HEIGHT)})
#self._camera.configure(config)
#self._camera.start_preview()
#self._camera.start()
self._countdown_timer.restart() self._countdown_timer.restart()
elif new_state == State.SHOOTING: elif new_state == State.SHOOTING:
self._photos = [] self._photos = []
@ -236,7 +264,7 @@ class Fotobox:
filename, filename,
)) ))
path.parent.mkdir(parents=True, exist_ok=True) path.parent.mkdir(parents=True, exist_ok=True)
img.save(path, format='JPEG', quality=95) img.convert('RGB').save(path, format='JPEG', quality=95)
upload = Path("./data/photos/upload/%s" % (filename,)) upload = Path("./data/photos/upload/%s" % (filename,))
upload.parent.mkdir(parents=True, exist_ok=True) upload.parent.mkdir(parents=True, exist_ok=True)
os.link(path, upload) os.link(path, upload)

View File

@ -1,7 +1,8 @@
sudo apt update sudo apt update
sudo apt install libcap-dev xinit sudo apt install libcap-dev xinit python3-libcamera python3-picamera2 #######??????######### libcamera-dev libcamera-apps
python3 -m venv venv
python3 -m venv venv --system-site-packages
. venv/bin/activate . venv/bin/activate
pip install pygame qrcode escpos picamera2 pip install pygame qrcode escpos picamera2

View File

@ -4,4 +4,7 @@ cd "$(dirname "$0")" || exit 1
. venv/bin/activate . venv/bin/activate
#while true; do
python main.py python main.py
# sleep 1
#done

View File

@ -19,7 +19,7 @@ PHOTO_HEIGHT = 1944
SHOOTING_COUNT: int = 2 SHOOTING_COUNT: int = 2
SHOOTING_INTERVAL: float = 0 if DEBUG else 1.75 SHOOTING_INTERVAL: float = 0 if DEBUG else 1.75
COUNTDOWN_COUNT: int = 1 if DEBUG else 3 COUNTDOWN_COUNT: int = 1 if DEBUG else 1
CHOICE_BORDER: int = 5 CHOICE_BORDER: int = 5

10
main.py
View File

@ -1,17 +1,11 @@
#!/usr/bin/python3 #!/usr/bin/python3
import pygame import pygame
import picamera import picamera2
from Fotobox import Fotobox from Fotobox import Fotobox
with picamera.PiCamera() as camera: with picamera2.Picamera2() as camera:
camera.vflip = False
camera.hflip = True
camera.resolution = (2592, 1944)
camera.rotation = 0
camera.start_preview()
fotobox = Fotobox(camera) fotobox = Fotobox(camera)
try: try:
fotobox.run() fotobox.run()