Skip to content

Instantly share code, notes, and snippets.

@kolibril13
Last active June 30, 2022 12:30
Show Gist options
  • Save kolibril13/9dd0150aa43936f7f377d9c483d0ce12 to your computer and use it in GitHub Desktop.
Save kolibril13/9dd0150aa43936f7f377d9c483d0ce12 to your computer and use it in GitHub Desktop.
Manim&Napari
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from manim import *\n",
"\n",
"paramp = \"-v WARNING -qh --format=png -r 1000,1000 NapariExample \"\n",
"#paramp = \"-v WARNING -qh -r 1000,1000 NapariExample \""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%manim $paramp \n",
"\n",
"class NapariExample(Scene):\n",
" def construct(self):\n",
" self.camera.background_color = WHITE\n",
" star = Star(color=BLACK)\n",
" star.set_opacity(1)\n",
" star.scale_to_fit_height(0.00000001)\n",
" self.add(star)\n",
" self.play(star.animate.scale_to_fit_height(4), rate_func=linear)\n",
" self.play(star.animate.scale_to_fit_height(0.00000001), rate_func=linear)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 255], dtype=uint8)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pathlib import Path\n",
"from PIL import Image\n",
"import numpy as np\n",
"import napari\n",
"\n",
"root = Path.cwd() / \"media\"\n",
"all_imgs = []\n",
"for path in sorted(root.rglob(\"*.png\")):\n",
" if \"NapariExample\" in path.name:\n",
" with Image.open(path) as img:\n",
" my_img = np.array(img.convert('L'))\n",
" my_img = my_img > 128\n",
" all_imgs.append(~my_img)\n",
"all_imgs = np.array(all_imgs, dtype= \"uint8\")*255\n",
"np.unique(all_imgs)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(120, 1000, 1000)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all_imgs.shape"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import napari\n",
"\n",
"viewer = napari.Viewer(ndisplay=3)\n",
"viewer.add_labels(all_imgs, scale=[1, 2, 2] , seed=0.03 , opacity=1)\n",
"viewer.camera.angles = (-12, 24, 62)\n",
"viewer.camera.zoom = 1"
]
}
],
"metadata": {
"interpreter": {
"hash": "bb7a7b842930851728e0a2dafc827a6069e16b7869feb3e5989a9987e40ae401"
},
"kernelspec": {
"display_name": "Python 3.9.11 ('napari-env')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.11"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment