{"id":21732260,"url":"https://github.com/pwhiddy/tensor-canvas","last_synced_at":"2025-04-13T00:36:26.008Z","repository":{"id":57474131,"uuid":"282529710","full_name":"PWhiddy/tensor-canvas","owner":"PWhiddy","description":"A 2D graphics library that draws directly onto tensors and is cross-framework compatible","archived":false,"fork":false,"pushed_at":"2021-10-24T04:23:05.000Z","size":21,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T00:36:20.504Z","etag":null,"topics":["2d","graphics","jax","ml","pytorch","sdf","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PWhiddy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-25T21:43:50.000Z","updated_at":"2024-12-06T11:56:53.000Z","dependencies_parsed_at":"2022-09-12T19:40:17.747Z","dependency_job_id":null,"html_url":"https://github.com/PWhiddy/tensor-canvas","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PWhiddy%2Ftensor-canvas","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PWhiddy%2Ftensor-canvas/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PWhiddy%2Ftensor-canvas/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PWhiddy%2Ftensor-canvas/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PWhiddy","download_url":"https://codeload.github.com/PWhiddy/tensor-canvas/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650417,"owners_count":21139671,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["2d","graphics","jax","ml","pytorch","sdf","tensorflow"],"created_at":"2024-11-26T04:29:27.633Z","updated_at":"2025-04-13T00:36:25.984Z","avatar_url":"https://github.com/PWhiddy.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Tensor Canvas 🎨  \n[![PyPI version](https://badge.fury.io/py/tensor-canvas.svg)](https://badge.fury.io/py/tensor-canvas)  \n  \nTensor Canvas provides a standard API for 2D rendering directly onto tensors with pytorch, tensorflow, jax, and numpy.\nSDF representations are used to implement rendering in these gpu-accelerated frameworks, which is inefficient compared to normal gpu rasterization but much more predictable than matplotlib. Integration with ML frameworks also means that it is fully-differentiable. Cross-framework support is possible thanks to the [eagerpy](https://github.com/jonasrauber/eagerpy) library.  \n  \nCurrently only cirlces and lines are supported, but it is straightforward to port [any 2D SDF](https://www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm).\n\n### Installation  \n\n```bash\npip install tensor-canvas\n```\n\n### Example\n```python\nimport tensorcanvas as tc\nimport torch\nimport tensorflow as tf\nimport jax.numpy as jnp\nimport numpy as np\n\n# define 3 cirlces with different positions, radii, and colors\nx1, y1, r1, c1 = 34.8,  5.3, 2.0, [0.3, 0.2, 1.0]\nx2, y2, r2, c2 = 14.8, 15.3, 5.0, [0.1, 0.9, 0.8]\nx3, y3, r3, c3 = 30.8, 20.3, 3.0, [0.0, 0.9, 0.0]\n\n# canvas dimensions\nheight, width, channels = 32, 64, 3\n\n# draw 3 colored circles on a pytorch image tensor\npt_canvas = torch.zeros(channels, height, width)\npt_canvas = tc.draw_circle(x1, y1, r1, torch.tensor(c1), pt_canvas)\npt_canvas = tc.draw_circle(x2, y2, r2, torch.tensor(c2), pt_canvas)\npt_canvas = tc.draw_circle(x3, y3, r3, torch.tensor(c3), pt_canvas)\n\n# draw 3 colored cirlces on a tensorflow image tensor\ntf_canvas = tf.zeros([height, width, channels])\ntf_canvas = tc.draw_circle(x1, y1, r1, tf.convert_to_tensor(c1), tf_canvas)\ntf_canvas = tc.draw_circle(x2, y2, r2, tf.convert_to_tensor(c2), tf_canvas)\ntf_canvas = tc.draw_circle(x3, y3, r3, tf.convert_to_tensor(c3), tf_canvas)\n\n# draw 3 colored cirlces on a jax image tensor\njx_canvas = jnp.zeros([height, width, channels])\njx_canvas = tc.draw_circle(x1, y1, r1, jnp.array(c1), jx_canvas)\njx_canvas = tc.draw_circle(x2, y2, r2, jnp.array(c2), jx_canvas)\njx_canvas = tc.draw_circle(x3, y3, r3, jnp.array(c3), jx_canvas)\n\n# draw 3 colored cirlces on a numpy image tensor\nnp_canvas = np.zeros([height, width, channels])\nnp_canvas = tc.draw_circle(x1, y1, r1, np.array(c1), np_canvas)\nnp_canvas = tc.draw_circle(x2, y2, r2, np.array(c2), np_canvas)\nnp_canvas = tc.draw_circle(x3, y3, r3, np.array(c3), np_canvas)\n\n# check results are indentical\nassert(np.allclose(np_canvas, pt_canvas.permute(1,2,0), atol=1e-6))\nassert(np.allclose(np_canvas, tf_canvas, atol=1e-6))\nassert(np.allclose(np_canvas, jx_canvas, atol=1e-6))\n\n```\n\n### Notebook Example\n\n[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/PWhiddy/TensorCanvasDemo/blob/master/TensorCanvasDemo.ipynb)  \n  \n\u003cimg src=\"https://i.imgur.com/sspmxHa.png\" width=\"653\" height=\"763\"\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpwhiddy%2Ftensor-canvas","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpwhiddy%2Ftensor-canvas","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpwhiddy%2Ftensor-canvas/lists"}