Skip to content

Instantly share code, notes, and snippets.

@Valexandre
Created August 22, 2022 20:35
Show Gist options
  • Save Valexandre/ffebc9588e3eb1d7fb2298efd2f258ef to your computer and use it in GitHub Desktop.
Save Valexandre/ffebc9588e3eb1d7fb2298efd2f258ef to your computer and use it in GitHub Desktop.
library(imager)
library(tidyverse)
library(usethis)
#Source: https://twitter.com/k_koi/status/1538198569689268226/photo/1
#Kensuke Koike
SortLesGraphsGrandVersPetit<-function(url_image, nom_image, squares_width=20,keep_1_every=4){
IMAGE <- imager::load.image(url_image)
df1 <- as.data.frame(IMAGE,wide="c") %>% mutate(rgb.val=rgb(c.1,c.2,c.3))
initwidth<-max(df1$x)
initheight<-max(df1$y)
#squares_width<-50
df1<-df1%>%mutate(x_squ=cut(x,breaks=seq(from=0,to=initwidth,by=squares_width)),
y_squ=cut(y,breaks=seq(from=0,to=initheight,by=squares_width)))
list_sq_x<-sort(unique(df1$x_squ))
list_sq_y<-sort(unique(df1$y_squ))
df1<-df1%>%mutate(order_init_x_sq=ntile(x_squ,length(list_sq_x)),
order_init_y_sq=ntile(y_squ,length(list_sq_y)))
sq_kept_x<-list_sq_x[1:length(list_sq_x)%%keep_1_every==0]
sq_kept_y<-list_sq_y[1:length(list_sq_y)%%keep_1_every==0]
df2<-df1%>%filter(x_squ%in%sq_kept_x & y_squ%in%sq_kept_y)
df2<-df2%>%mutate(order_x_sq=ntile(x_squ,length(sq_kept_x)),
order_y_sq=ntile(y_squ,length(sq_kept_y)))%>%
group_by(order_x_sq)%>%
mutate(new_x=ntile(x,squares_width))%>%
ungroup()%>%
group_by(order_y_sq)%>%
mutate(new_y=ntile(y,squares_width))%>%
ungroup()
df2$new_x<-ifelse(df2$order_x_sq!=1,((df2$order_x_sq-1)*squares_width)+df2$new_x,df2$new_x)
df2$new_y<-ifelse(df2$order_y_sq!=1,((df2$order_y_sq-1)*squares_width)+df2$new_y,df2$new_y)
jpeg(filename = paste0(nom_image,"_init.jpg"), width=initwidth, height = initheight, quality=100, units = "px",type="cairo")
plot(ggplot(df1,aes(x,y))+geom_tile(aes(fill=rgb.val),alpha=1)+scale_fill_identity()+scale_y_reverse()+coord_fixed(ratio=1)+theme_void() )
dev.off()
usethis::ui_done("First image")
jpeg(filename = paste0(nom_image,"_interm.jpg"), width=initwidth, height = initheight, quality=100, units = "px",type="cairo")
plot(ggplot(df2,aes(x,y))+geom_tile(aes(fill=rgb.val),alpha=1)+scale_fill_identity()+scale_y_reverse(limits=c(initheight,0))+coord_fixed(ratio=1)+theme_void() )
dev.off()
usethis::ui_done("Second image")
jpeg(filename = paste0(nom_image,"_fin.jpg"), width=max(df2$new_x)*2, height = max(df2$new_y)*2, quality=100, units = "px",type="cairo")
plot(ggplot(df2,aes(new_x,new_y))+geom_tile(aes(fill=rgb.val),alpha=1)+scale_fill_identity()+scale_y_reverse(limits=c(max(df2$new_y),0))+coord_fixed(ratio=1)+theme_void() )
dev.off()
usethis::ui_done("Third image")
}
SortLesGraphsGrandVersPetit(url_image = "https://cdn.mediatheque.epmoo.fr/link/ylg189jq00hstq8.jpg",nom_image = "Sisley_Pont_de_Morey",squares_width = 20,keep_1_every = 4)
SortLesGraphsGrandVersPetit(url_image = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/77/Tesla_Sarony.jpg/800px-Tesla_Sarony.jpg",nom_image = "Tesla_Portrait",squares_width = 10,keep_1_every = 3)
SortLesGraphsGrandVersPetit(url_image = "https://resize-parismatch.lanmedia.fr/img/var/news/storage/images/paris-match/people-a-z/jacques-chirac/5972114-20-fre-FR/Jacques-Chirac.jpg",nom_image = "Chirac_ParisMatch",squares_width = 10,keep_1_every = 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment