Skip to content

Instantly share code, notes, and snippets.

@nkymut
Created June 8, 2018 04:53
Show Gist options
  • Save nkymut/1cb40ea6ae4de0cf9ded7332f1ca0d55 to your computer and use it in GitHub Desktop.
Save nkymut/1cb40ea6ae4de0cf9ded7332f1ca0d55 to your computer and use it in GitHub Desktop.
convert opencv image to pyglet image
"""
convert opencv image to pyglet image
"""
import pyglet
import cv2
from PIL import Image
import numpy as np
def cv2glet(img,format):
'''Assumes image is in BGR color space. Returns a pyimg object'''
if format == 'GRAY':
rows, cols = img.shape
channels = 1
else:
rows, cols, channels = img.shape
raw_img = Image.fromarray(img).tobytes()
top_to_bottom_flag = -1
bytes_per_row = channels*cols
pyimg = pyglet.image.ImageData(width=cols,
height=rows,
format=format,
data=raw_img,
pitch=top_to_bottom_flag*bytes_per_row)
return pyimg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment