knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
glitchr is a toy package for databending and creating glitch art in R. To read more about the idea behind it, take a look at this blog post.
-- WORK IN PROGRESS --
You can install the development version of glitchr from Github with:
# install.packages("devtools") devtools::install_github("pj398/glitchr")
First, you need to read an image (currently only PNG images are confirmed to work) as a raw vector in R.
If you have a PNG image on file, you can read this in as raw using readr::read_file_raw("path/to_file")
. Alternatively, use one of the demo images included in the package:
library(glitchr)
data("demo_img1")
This is the raw vector for the following image:
class(demo_img1)
This raw data is what we will databend:
demo_img1[1:100]
Now, we can glitch this data using the glitch_png
function. We need to choose between two methods for glitching. One (method = "far"
) uses a find-and-replace approach which randomly picks unique values from the raw vector and replaces them with another randomly selected unique value. The other (method = "chops"
) randomly selects change points in the raw vector and then performs chance operations (either adding, moving, cloning or deleting values) at each change point.
For each method, we can use the n_changes
argument to specify how many changes to make. The find-and-replace method can be fine-tuned using tune
by specifying what percentage of the found values should be replaced. The chance operations method can be fine-tuned by using the noise
argument to set a ceiling for how much data is modified at each operation.
Although the "chops"
method is more fun in terms of what's happening under the hood, it is a little unstable so the safest method to use is generally "far"
.
Here's an example:
my_glitch <- glitch_png(demo_img1, method = "far", n_changes = 3, tune = 5)
We can take a look at what we just did using glitchr::plot_vector
which uses the excellent magick package to read the databent vector as an image once again so that we can print it in R.
my_glitch %>% plot_vector(width_px = 500)
If using RStudio, this will open the image in the built-in Viewer.
With a little luck, you will see a glitchy version of your original image:
Check out the package documentation for more explanation of how to use the functions.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.