Skip to content

Instantly share code, notes, and snippets.

@aisouard
Last active April 26, 2017 08:37
Show Gist options
  • Save aisouard/482776d7c5495a1dadb24523c2908d85 to your computer and use it in GitHub Desktop.
Save aisouard/482776d7c5495a1dadb24523c2908d85 to your computer and use it in GitHub Desktop.
Sample cefpython
# Hello world example. Doesn't depend on any third party GUI framework.
# Tested with CEF Python v55.3+.
from cefpython3 import cefpython as cef
import numpy as np
import cv2
import platform
import sys
VIEWPORT_SIZE = (359, 640)
PRELOAD = """
(function () {
document.body.style.overflow = "hidden";
})();
"""
class BrowserHandler(object):
def OnLoadingStateChange(self, browser, is_loading, **_):
"""Called when the loading state has changed."""
if not is_loading:
browser.ExecuteJavascript(PRELOAD)
def OnPaint(self, browser, element_type, paint_buffer, **_):
if element_type == cef.PET_VIEW:
buffer = paint_buffer.GetString(mode="bgra", origin="top-left")
bytes = np.fromstring(buffer, np.uint8).reshape((640, 359, 4))
bytes = cv2.cvtColor(bytes, cv2.COLOR_BGRA2BGR)
cv2.imshow('image', bytes)
def GetViewRect(self, rect_out, **_):
# rect_out --> [x, y, width, height]
rect_out.extend([0, 0, VIEWPORT_SIZE[0], VIEWPORT_SIZE[1]])
return True
def main():
check_versions()
sys.excepthook = cef.ExceptHook # To shutdown all CEF processes on error
cef.Initialize(settings={"windowless_rendering_enabled": True})
parent_window_handle = 0
window_info = cef.WindowInfo()
window_info.SetAsOffscreen(parent_window_handle)
browser = cef.CreateBrowserSync(window_info=window_info, url="https://apps.facebook.com/611307059053310")
browser.SetClientHandler(BrowserHandler())
browser.SendFocusEvent(True)
browser.WasResized()
cef.MessageLoop()
cef.Shutdown()
def check_versions():
print("[hello_world.py] CEF Python {ver}".format(ver=cef.__version__))
print("[hello_world.py] Python {ver} {arch}".format(
ver=platform.python_version(), arch=platform.architecture()[0]))
assert cef.__version__ >= "55.3", "CEF Python v55.3+ required to run this"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment