Created
June 8, 2018 04:53
-
-
Save nkymut/1cb40ea6ae4de0cf9ded7332f1ca0d55 to your computer and use it in GitHub Desktop.
convert opencv image to pyglet image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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