Created
August 6, 2020 11:30
-
-
Save jirilukavsky/2f5b4822112e1547ca4f56811f54f640 to your computer and use it in GitHub Desktop.
Processing images for memory experiments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# make the grid file | |
grid_size <- 900 | |
n_square <- 6 | |
ss <- 900 / n_square # square size | |
d <- array(0, c(grid_size, grid_size, 4)) | |
fillcolor <- c(235, 220, 36, 128) | |
for (i in 1:n_square) { | |
for (j in 1:n_square) { | |
if ((i + j) %% 2 == 0) { | |
d[((i - 1) * ss + 1):(i * ss), ((j - 1) * ss + 1):(j * ss), 1] <- | |
fillcolor[1] | |
d[((i - 1) * ss + 1):(i * ss), ((j - 1) * ss + 1):(j * ss), 2] <- | |
fillcolor[2] | |
d[((i - 1) * ss + 1):(i * ss), ((j - 1) * ss + 1):(j * ss), 3] <- | |
fillcolor[3] | |
d[((i - 1) * ss + 1):(i * ss), ((j - 1) * ss + 1):(j * ss), 4] <- | |
fillcolor[4] | |
} | |
} | |
} | |
png::writePNG(d / 255, here::here("grid.png")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magick) | |
# Image processing pipeline ------------------------------------------ | |
# 0) We start with images we want to process | |
# 1) Manually, in GIMP we crop them to square format | |
# => we save the result in `square-hires` folder | |
# with suffix `-cut` | |
# 2) The following script resizes the images and overlays a grid | |
# => results saved to `900px` folder | |
# with suffix `-cut-900x900` and `-cut-900x900x` (for grid version) | |
# | |
# We use magick package | |
# https://cran.r-project.org/web/packages/magick/vignettes/intro.html#Transformations | |
# SETUP: folders and files ------------------------------------------- | |
path_to_square_hires <- here::here("square-hires") | |
path_to_900px <- here::here("900px") | |
fn_grid <- here::here("grid.png") | |
# tool for renaming/suffixes ----------------------------------------- | |
fname <- function(fn, folder, suffix = "") { | |
name_wo_ext <- tools::file_path_sans_ext(fn) | |
ext <- tools::file_ext(fn) | |
file.path(folder, paste0(name_wo_ext, suffix, ".", ext)) | |
} | |
# load the grid ------------------------------------------------------ | |
img_grid <- image_read(fn_grid) | |
img_grid_900px <- image_resize(img_grid, "900x900") | |
# list files to process and process one at the time ------------------ | |
files <- dir(path = path_to_square_hires) | |
for (fn in files) { | |
# read | |
img <- image_read(fname(fn, path_to_square_hires)) | |
# resize | |
img_900px <- image_resize(img, "900x900") | |
# add grid | |
img_x <- c(img_900px, img_grid_900px) # try image_info(img_x) | |
img_f <- image_mosaic(img_x) | |
# save both | |
image_write(img_900px, path = fname(fn, path_to_900px, "-900x900"), | |
format = "jpeg", quality = 95) | |
image_write(img_f, path = fname(fn, path_to_900px, "-900x900x"), | |
format = "jpeg", quality = 90) | |
} | |
# notes ------------------------------------------------------------ | |
# utility functions | |
# - preview: image_ggplot(img) | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment