im2pix is a tool to convert image to pseudo pixel art in R. It’s a reduced version of Nathan Harper’s Python script. The author of this package do not own the copyright of this algorithm.
You can install the released version of im2pix from GitHub with:
devtools::install_github("TengMCing/im2pix")
This package heavily depends on the imager
package. You may get an
error like this when you run the imtopix()
function or load the
imager
package on macOS.
Loading required package: imager
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/Library/Frameworks/R.framework/Versions/3.3/Resources/library/imager/libs/imager.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.3/Resources/library/imager/libs/imager.so, 6): Library not loaded: /opt/X11/lib/libX11.6.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.3/Resources/library/imager/libs/imager.so
Reason: image not found
This is because you are missing X11
so you need to install/reinstall
the latest version of XQuartz
(https://www.xquartz.org/).
library(im2pix)
library(imager)
#> Loading required package: magrittr
#>
#> Attaching package: 'imager'
#> The following object is masked from 'package:magrittr':
#>
#> add
#> The following objects are masked from 'package:stats':
#>
#> convolve, spectrum
#> The following object is masked from 'package:graphics':
#>
#> frame
#> The following object is masked from 'package:base':
#>
#> save.image
The built-in image
plot(sample_im)
Covert the image to pseudo pixel art using palette “contra”. Increase the block size will decrease the output resolution.
The imtopix()
function only accepts cimg
class object/image which
can be loaded from disk by imager::load.image(<path_to_image>)
or
created from a matrix-like object by
imager::as.cimg(<matrix-like_object>)
.
out_pic <- imtopix(sample_im, pal = "contra", blockSize = 8)
plot(out_pic)
Using another palette
out_pic <- imtopix(sample_im, pal = "gameboy", blockSize = 8)
plot(out_pic)
Available palettes include
names(pal_collection)
#> [1] "appleii" "atari2600" "commodore64" "contra"
#> [5] "gameboy" "grayscale" "hyrule" "intellivision"
#> [9] "kungfu" "mario" "nes" "sega"
#> [13] "tetris"