Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Basthon
Basthon Kernel
Commits
adb510f7
Commit
adb510f7
authored
Oct 07, 2022
by
Romain Casati
Browse files
Python 3: patching cv2.imshow.
parent
037e278f
Changes
2
Hide whitespace changes
Inline
Side-by-side
packages/kernel-python3/src/basthon/_patch_modules.py
View file @
adb510f7
...
...
@@ -326,3 +326,20 @@ def patch_pkg_resources(pkg_resources):
before setuptools. See https://github.com/pypa/setuptools/blob/main/_distutils_hack/__init__.py
"""
import
setuptools
@
importhook
.
on_import
(
'cv2'
)
def
patch_cv2
(
cv2
):
import
io
import
base64
def
imshow
(
winname
,
mat
):
is_success
,
buffer
=
cv2
.
imencode
(
".png"
,
mat
)
io_buf
=
io
.
BytesIO
(
buffer
)
png
=
base64
.
b64encode
(
io_buf
.
read
()).
decode
()
dummy
=
type
(
'Dummy'
,
(
object
,),
{})
dummy
.
_repr_png_
=
lambda
:
png
kernel
.
display
(
dummy
)
imshow
.
__doc__
=
cv2
.
imshow
.
__doc__
cv2
.
imshow
=
imshow
tests/test_python3_20_patched_modules.py
View file @
adb510f7
...
...
@@ -66,6 +66,7 @@ def test_PIL(selenium_py3):
dctx = ImageDraw.Draw(img)
dctx.rectangle(bbox, fill="#ddddff", outline="blue")
img.show()
img.save("pil.png")
"""
)
assert
result
[
'stdout'
]
==
""
assert
result
[
'stderr'
]
==
""
...
...
@@ -258,3 +259,19 @@ grid.show_image()
png
=
base64
.
b64decode
(
result
[
'display'
][
'content'
][
'image/png'
])
target
=
read_and_backup
(
_test_data
/
"python3_ipythonblocks_save_image.png"
,
png
,
binary
=
True
)
assert
png
==
target
def
test_cv2
(
selenium_py3
):
# opencv hangs CPU so we remove test
pass
# result = selenium_py3.run_basthon("""
# import cv2
# img = cv2.imread("pil.png")
# cv2.imshow("image", img)
# """)
# assert result['stdout'] == ""
# assert result['stderr'] == ""
# assert result['display']['display_type'] == 'multiple'
# img = result['display']['content']['image/png']
# target = read_and_backup(_test_data / "python3_cv2.png", img)
# assert target == img
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment