Coder Social home page Coder Social logo

ddrawdx's Introduction

ddrawdx

A small set of differentiable and jittable drawing primitives in JAX. A Canvas consists of a data array with pixel values and a mesh of evaluation points. This mesh can be translated, scaled and rotated. Any of the drawing functions evaluates a smooth function on the mesh to create a "mixing" matrix, and mixes pixels in the image with the color supplied. Currently all smooth transitions are sigmoid based. The sharpness of the transition is not scale invariant, i.e. if one zooms out and draws a shape using the same sharpness this will result in a sharper image

It is a bit janky.

(Outdated) Function signature reference here.

Installation

clone the repo and run pip install . in the root directory.

Examples

import ddrawdx as drx
import jax.numpy as jnp
import jax
import matplotlib.pyplot as plt
reddish = jnp.array([0.8, 0.2, 0.1])
c = drx.canvas(200, 200, "RGB")
c, r = drx.origin(c)
def flower(c):
for s in jnp.arange(0.0, 0.8, 0.05):
c, _ = drx.rotate(c, 1.2 * s)
c = drx.fill_rect(c, -0.9 + s, -0.9 + s, 0.9 - s, 0.9 - s, reddish * (1 - s), 400 * (0.88 - s))
return c
flower = jax.jit(flower)
c = flower(c)
fig, ax = drx.show(c)
ax.set_title("Box flower")
fig.savefig("boxflower.png")
ex-box-flower

c = drx.canvas(200, 200, "RGB")
c = drx.fill_rect(c, 0.2, 0.2, 0.8, 0.6, jnp.array([0.0, 1.0, 0.0]))
ps = jnp.array(
[[0.1, 0.1], [0.1, 0.5], [0.5, 0.1]]
)
c = drx.fill_poly(c, ps, color=jnp.array([1.0, 0.0, 0.0]))
c, r = drx.translate(c, 0.5, 0.5)
c = drx.draw_line(c, 0.0, 0.0, 0.25, 0.25)
c, _ = drx.rotate(c, jnp.pi/2)
c = drx.draw_line(c, 0.0, 0.0, 0.25, 0.25)
c = drx.restore(c, r)
fig, ax = drx.show(c)
ax.set_title("Clock")
fig.savefig("clock.png")
ex-clock

c = drx.canvas(200, 200, "GRAY")
c = drx.fill_rect(c, 0.3, 0.3, 0.7, 0.4, jnp.array([0.2]))
fig, ax = drx.show(c)
ax.set_title("Grayscale")
fig.savefig("gray.png")
ex-gray

ddrawdx's People

Contributors

vincentmolin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.