Skip to content

Commit

Permalink
Make the web example work with Python < 3.10 (again). Closes otaku42#21.
Browse files Browse the repository at this point in the history
  • Loading branch information
otaku42 committed May 19, 2023
1 parent 93fc9b3 commit 67a6e3b
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions examples/web/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,21 @@ def __init__(self, device: Device) -> None:


def frame_to_image(frame, output="jpeg"):
match frame.pixel_format:
case PixelFormat.JPEG | PixelFormat.MJPEG:
if output == "jpeg":
return to_image_send(frame.data, type=output)
else:
buff = io.BytesIO()
image = PIL.Image.open(io.BytesIO(frame.data))
case PixelFormat.GREY:
data = frame.array
data.shape = frame.height, frame.width, -1
image = PIL.Image.frombuffer("L", (frame.width, frame.height), data)
case PixelFormat.YUYV:
data = frame.array
data.shape = frame.height, frame.width, -1
rgb = cv2.cvtColor(data, cv2.COLOR_YUV2RGB_YUYV)
image = PIL.Image.fromarray(rgb)
if frame.pixel_format in (PixelFormat.JPEG, PixelFormat.MJPEG):
if output == "jpeg":
return to_image_send(frame.data, type=output)
else:
buff = io.BytesIO()
image = PIL.Image.open(io.BytesIO(frame.data))
elif frame.pixel_format == PixelFormat.GREY:
data = frame.array
data.shape = frame.height, frame.width, -1
image = PIL.Image.frombuffer("L", (frame.width, frame.height), data)
elif frame.pixel_format == PixelFormat.YUYV:
data = frame.array
data.shape = frame.height, frame.width, -1
rgb = cv2.cvtColor(data, cv2.COLOR_YUV2RGB_YUYV)
image = PIL.Image.fromarray(rgb)

buff = io.BytesIO()
image.save(buff, output)
Expand Down

0 comments on commit 67a6e3b

Please sign in to comment.