img2grid

Creates grid-art from images
git clone https://src.jayvii.de/pub/img2grid.git
Log | Files | Refs

commit 58cdb1ad88353faf8401197389800eb4866fc9b6
Author: JayVii <jayvii@posteo.de>
Date:   Sat, 21 Dec 2019 15:15:15 +0100

Dump, Cologne Project

Diffstat:
Acologne/bridge.png | 0
Acologne/bridge_grid.png | 0
Acologne/cutting.jpg | 0
Acologne/cutting.xcf | 0
Acologne/img2grid.R | 134+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acologne/original.jpg | 0
Acologne/output.png | 0
7 files changed, 134 insertions(+), 0 deletions(-)

diff --git a/cologne/bridge.png b/cologne/bridge.png Binary files differ. diff --git a/cologne/bridge_grid.png b/cologne/bridge_grid.png Binary files differ. diff --git a/cologne/cutting.jpg b/cologne/cutting.jpg Binary files differ. diff --git a/cologne/cutting.xcf b/cologne/cutting.xcf Binary files differ. diff --git a/cologne/img2grid.R b/cologne/img2grid.R @@ -0,0 +1,134 @@ +# Copyright (c) 2018 Jan "JayVii" <jayvii@posteo.de> +# SPDX-License-Identifier: GPL-3.0 + +# Load Packages ---------------------------------------------------------------- +# {{{ + +if(!require(imager)){ install.packages("imager"); library(imager) } +if(!require(ggplot2)){ install.packages("ggplot2"); library(ggplot2) } +if(!require(dplyr)){ install.packages("dplyr"); library(dplyr) } +#if(!require(geosphere)){ install.packages("geosphere"); library(geosphere) } + +# }}} + +# Functions -------------------------------------------------------------------- +# {{{ + +which.dist <- function(x, y, values = 50, verbose = FALSE){ + + # check whether x and y are numeric + if (!is.numeric(x)) x <- as.numeric(x) + if (!is.numeric(y)) y <- as.numeric(y) + + # calculate distances between all Xs and between all Ys + distances <- sapply(X = 1:length(x), FUN = function(i){ + if (verbose) cat(paste(i, "\n")) + distx <- x - x[i] + disty <- y - y[i] + distxy <- (abs(distx) + abs(disty)) + # which are the %values% lowest distances + ordexy <- which(order(distxy, + decreasing = FALSE) <= values) + return(distxy[ordexy]) + }) %>% t() + + # return object + return(distances) + +} + +dist.xy <- function(x, y, fast = TRUE){ + + # check whether x and y are numeric + if (!is.numeric(x)) x <- as.numeric(x) + if (!is.numeric(y)) y <- as.numeric(y) + + # calculate distances between all Xs and between all Ys + if (fast){ + distxy <- sapply(X = 1:length(x), FUN = function(i){ + distx <- x - x[i] + disty <- y - y[i] + + return(abs(distx) + abs(disty)) + }) + } else { + distxy <- matrix(data = NA, ncol = length(x), nrow = length(x)) + for (i in 1:length(x)){ + distx <- x - x[i] + disty <- y - y[i] + distxy[i, ] <- abs(distx) + abs(disty) + } + } + + # return combined distance measure + return(distxy) +} + +# }}} + +# Import image, grayscale, filter, and sample image ---------------------------- +# {{{ + +file <- "./cutting.jpg" + +# Kudos: @aschinchon +# https://github.com/aschinchon/travelling-salesman-portrait/ +data.df <- load.image(file) %>% + grayscale() %>% + threshold("35%") %>% + as.cimg() %>% + as.data.frame() + +data <- data.df %>% + sample_n(10000, weight = (1 - value)) %>% + select(x, y) + +# }}} + +# edit data -------------------------------------------------------------------- +# {{{ + +# Scale data and calculate distances between each point +data_st <- data / max(data) +distances <- dist.xy(x = data_st[, "x"], y = data_st[, "y"], fast = TRUE) +#distances <- distm(x = data_st) # dirty. FIXME (assumes ellipsoid or sphere) +#distances <- which.dist(x = data_st[, "x"], y = data_st[, "y"], values = 50, +# verbose = TRUE) + +# find nearest point for each pair of x/y +near <- matrix(data = NA, nrow = nrow(distances), ncol = ncol(distances)) +for (i in 1:nrow(distances)){ + near[i,] <- order(distances[i,], decreasing = FALSE) +} + +# plot the result {{{ ---- + +ratios <- sapply(X = data, FUN = function(a){ + max(a) - min(a) + }) +ratios <- ratios["x"] / ratios["y"] + +# we want a PDF file from that (10" x 10") +png(file = "./bridge_grid.png", height = 4000, width = 6000, bg = "#00000000") +#pdf(file = "./bridge_grid.pdf", height = 10, width = 10 * ratios, + #bg = "#00000000") + + # without margins around plot + par(mar=c(0, 0, 0, 0)) + + # dummy (empty) plot, with correct xylims + plot(-data_st, type = "l", col = rgb(0,0,0,alpha = 0), axes = FALSE, + xlab = "", ylab = "", main = "") + + # add lines connecting the nearest 20 xy-pairs to current point + # also mirror data points (-) + # color is black with some low alpha value + for (i in 1:length(data_st$x)){ + cat(paste(i, "\n")) + lines(-data_st[near[i, 1:50],], type = "l", + col = rgb(1, 1, 1, alpha = 0.5)) + } + +dev.off() + +# }}} diff --git a/cologne/original.jpg b/cologne/original.jpg Binary files differ. diff --git a/cologne/output.png b/cologne/output.png Binary files differ.