Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/matt-dray/pixeltrix

:space_invader::computer_mouse: R package: make pixel art interactively in a plot window, get a matrix, make a gif
https://github.com/matt-dray/pixeltrix

pixel-art rstats rstats-package shiny sprites

Last synced: 3 months ago
JSON representation

:space_invader::computer_mouse: R package: make pixel art interactively in a plot window, get a matrix, make a gif

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
eval = FALSE,
collapse = TRUE,
comment = "#",
fig.path = "man/figures/README-",
out.width = "100%"
)

library(pixeltrix)
```

# pixeltrix

[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![R-CMD-check](https://github.com/matt-dray/tamRgo/workflows/R-CMD-check/badge.svg)](https://github.com/matt-dray/tamRgo/actions)
[![Codecov test coverage](https://codecov.io/gh/matt-dray/pixeltrix/branch/main/graph/badge.svg)](https://app.codecov.io/gh/matt-dray/pixeltrix?branch=main)
[![Blog posts](https://img.shields.io/badge/rostrum.blog-posts-008900?labelColor=000000&logo=data%3Aimage%2Fgif%3Bbase64%2CR0lGODlhEAAQAPEAAAAAABWCBAAAAAAAACH5BAlkAAIAIf8LTkVUU0NBUEUyLjADAQAAACwAAAAAEAAQAAAC55QkISIiEoQQQgghRBBCiCAIgiAIgiAIQiAIgSAIgiAIQiAIgRAEQiAQBAQCgUAQEAQEgYAgIAgIBAKBQBAQCAKBQEAgCAgEAoFAIAgEBAKBIBAQCAQCgUAgEAgCgUBAICAgICAgIBAgEBAgEBAgEBAgECAgICAgECAQIBAQIBAgECAgICAgICAgECAQECAQICAgICAgICAgEBAgEBAgEBAgICAgICAgECAQIBAQIBAgECAgICAgIBAgECAQECAQIBAgICAgIBAgIBAgEBAgECAgECAgICAgICAgECAgECAgQIAAAQIKAAAh%2BQQJZAACACwAAAAAEAAQAAAC55QkIiESIoQQQgghhAhCBCEIgiAIgiAIQiAIgSAIgiAIQiAIgRAEQiAQBAQCgUAQEAQEgYAgIAgIBAKBQBAQCAKBQEAgCAgEAoFAIAgEBAKBIBAQCAQCgUAgEAgCgUBAICAgICAgIBAgEBAgEBAgEBAgECAgICAgECAQIBAQIBAgECAgICAgICAgECAQECAQICAgICAgICAgEBAgEBAgEBAgICAgICAgECAQIBAQIBAgECAgICAgIBAgECAQECAQIBAgICAgIBAgIBAgEBAgECAgECAgICAgICAgECAgECAgQIAAAQIKAAA7)](https://www.rostrum.blog/tags/pixeltrix/)

Create static and animated pixel art from an interactive plot window in R. Returns your image's 'blueprint' as a matrix. Pixel + matrix = {pixeltrix}.

## How to

You can install {pixeltrix} [from GitHub](https://github.com/matt-dray/pixeltrix). It has no dependencies unless you want to make gifs, which requires you to install [{gifski}](https://cloud.r-project.org/web/packages/gifski/index.html).

```{r eval=FALSE}
install.packages("remotes") # if not yet installed
remotes::install_github("matt-dray/pixeltrix")
library(pixeltrix)
```

Basic use:

1. Use `click_pixels()` to begin an interactive, clickable plot of squares ('pixels').
2. Click individual pixels repetitively to cycle through their states.
3. Press the Esc key when you're done, or the 'Finish' button in RStudio's plot window, to return a matrix that encodes your image.

You can also:

* draw your matrix to the plotting window as an image with `draw_pixels()`
* make changes by passing the matrix output from `click_pixels()` into `edit_pixels()`
* create animation frames (a list of matrices) with `frame_pixels()` and write them to a gif with `gif_pixels()`

## Limitations

This package does what I need it to do; it doesn't match the quality of a real pixel art editor. Some known limitations are that:

* you can only click one pixel at a time
* each click only increments the pixel state by 1
* you can't change the number of pixel states on the fly, nor the colour palette

If your editor opens a separate graphics window (i.e. not RStudio), each click may result in a brief flash as the image refreshes, while a resized window may return to its original dimensions. You may also hear a bell sound on click, which you can disable by setting `options(locatorBell = FALSE)`.

## Examples

### Static sprite

Let's create a sprite of the player character from _Pokémon_ (1996) using `click_pixels()`:

```{r}
pkmn_sprite <- click_pixels(
n_rows = 16,
n_cols = 14,
n_states = 3, # number of states that a pixel can take
colours = c("white", "#879afb", "grey20") # Pokémon Blue palette
)
# Click squares in the plot window. Press to end.
```

This opens an interactive plot window. You can click to cycle the pixels through each of the three states. Here's how the finished version looks in RStudio:

An RStudio window. The console has run the function click_pixels(blue) and has printed the message 'click squares in the plot window, press Esc to end.' In the plot pane is a 16 by 14 pixel grid with a sprite of the main character from the first generation of Pokemon games for the Game Boy. The background is white, the outlines are dark grey and the highlights are light blue There's a black grid around the pixels.

A matrix is returned when you've finished clicking and pressed Esc. Note that the values are zero-indexed and the corresponding colour palette is stored in a `colours` attribute. The object also gets the class 'pixeltrix'.

```{r eval=TRUE}
str(pkmn_sprite)

is_pixeltrix(pkmn_sprite)
```

The `pkmn_sprite` object is built-in to {pixeltrix} for demo purposes.

You can pass the 'pixeltrix' object to `edit_pixels()` to reopen the interactive plotting window to make adjustments. You can also increase `n_states` or change the colour palette. You could also create a matrix 'by hand' and use `as_pixeltrix()` to coerce it to a 'pixeltrix' object with a greyscale palette.

The `draw_pixels()` function simply plots your matrix, optionally with a new colour palette:

```{r eval=FALSE}
draw_pixels(
m = pkmn_sprite,
colours = c("#9bbc0f", "#8bac0f", "#306230") # Game Boy palette
)
```

A 14 by 16 pixel grid with a sprite of the main character from the first generation of Pokemon games for the Game Boy. It's coloured using the green shades of the original Game Boy.

### Animated sprite

You can create multiple animation frames with `frame_pixels()`. The prior frame is used as a template for the next.

Here's how it might look to recreate Mario's walk cycle from _Super Mario Brothers_ (1983), including an interactive prompt to add more frames as required:

```{r}
mario_frames <- frame_pixels(
n_rows = 16,
n_cols = 16,
n_states = 4,
colours = c("#8861FE", "#F6B95B", "#EF151A", "#7F6D14")
)
# Click squares in the plot window. Press to end.
# Add a frame? y/n: y
# Click squares in the plot window. Press to end.
# Current frame count: 2
# Add a frame? y/n: y
# Click squares in the plot window. Press to end.
# Current frame count: 3
# Add a frame? y/n: n
# Final frame count: 3
```

The structure of the object is a list of matrices, where each matrix is a frame of the animation. This object is provided in {pixeltrix} as a demo dataset.

```{r eval=TRUE}
str(mario_frames)
```

You can then convert the frames to a gif with `gif_pixels()`.

```{r}
gif_pixels(
frames = mario_frames,
file = "mario.gif", # write location
delay = 0.15 # passed to gifski::save_gif()
)
# Inserting image 3 at 0.30s (100%)...
# Encoding to gif... done!
# [1] "mario.gif"
```

Which results in this:

An animated 16 by 16 pixel grid with a coloured sprite of Mario from the original Super Mario Bros for the NES. There are three frames that each show a step in Mario's walk cycle.