# https://www.r-bloggers.com/2019/01/extracting-colours-from-your-images-with-image-quantization/
    library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.1     v dplyr   1.0.6
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.0.5
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## Warning: package 'readr' was built under R version 4.0.5
## Warning: package 'dplyr' was built under R version 4.0.5
## Warning: package 'forcats' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
  library(tmap)
## Warning: package 'tmap' was built under R version 4.0.5
  library(sf)
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
    library(magick) 
## Warning: package 'magick' was built under R version 4.0.5
## Linking to ImageMagick 6.9.12.3
## Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fontconfig, x11
    library(scales) 
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
    library(imager) 
## Warning: package 'imager' was built under R version 4.0.5
## Loading required package: magrittr
## Warning: package 'magrittr' was built under R version 4.0.5
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
## 
##     set_names
## The following object is masked from 'package:tidyr':
## 
##     extract
## 
## Attaching package: 'imager'
## The following object is masked from 'package:magrittr':
## 
##     add
## The following object is masked from 'package:stringr':
## 
##     boundary
## The following object is masked from 'package:tidyr':
## 
##     fill
## 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
    wd <- "C://Users//np83zg//OneDrive - Aalborg Universitet//Skrivebord//kadinsky//"
    setwd(wd)
    no <- 3
    file_name <- paste("kadinsky_",no,".jpg",sep="")

    img <- image_read(file_name)

    get_colorPal <- function(im, n=8, cs="RGB"){
          #print(cs) 
          tmp <-im %>% image_resize("100") %>% 
                image_quantize(max=n, colorspace=cs) %>%  ## reducing colours! different colorspace gives you different result
                magick2cimg() %>%  ## I'm converting, becauase I want to use as.data.frame function in imager package.
                RGBtoHSV() %>% ## i like sorting colour by hue rather than RGB (red green blue)
                as.data.frame(wide="c") %>%  #3 making it wide makes it easier to output hex colour
                mutate(hex=hsv(rescale(c.1, from=c(0,360)),c.2,c.3),
                        hue = c.1,
                        sat = c.2,
                        value = c.3) %>%
                    count(hex, hue, sat,value, sort=T) %>% 
                    mutate(colorspace = cs)
  
            return(tmp %>% select(colorspace,hex,hue,sat,value,n)) ## I want data frame as a result.
  
        }



    # Municipality data
    mun_df <- read.table("foo.txt",sep="",header=TRUE)
    mun_shp <- st_read("municipalities_DK_WGS84.shp")
## Reading layer `municipalities_DK_WGS84' from data source `C:\Users\np83zg\OneDrive - Aalborg Universitet\Skrivebord\kadinsky\municipalities_DK_WGS84.shp' using driver `ESRI Shapefile'
## Simple feature collection with 98 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 8.07361 ymin: 54.55909 xmax: 15.15593 ymax: 57.75255
## Geodetic CRS:  WGS 84
    ntm_shp <- st_read("ntm.shp")
## Reading layer `ntm' from data source `C:\Users\np83zg\OneDrive - Aalborg Universitet\Skrivebord\kadinsky\ntm.shp' using driver `ESRI Shapefile'
## Simple feature collection with 907 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 8.073759 ymin: 54.55905 xmax: 15.19324 ymax: 57.75167
## Geodetic CRS:  WGS 84
    varname <- "qol"
    
    
    temp <- merge(mun_shp,mun_df[,c("ID_muni",varname)],by.x="KOMKODE",by.y="ID_muni",all.x=TRUE)
    colors <- get_colorPal(img,110,"Lab")[,2]
    temp$colors <- colors[1:98]
    tm1 <- qtm(temp,fill="colors")
    
    temp <- ntm_shp 
    colors <- get_colorPal(img,1200,"Lab")[,2]
    temp$colors <- colors[1:907]
    tm2 <- qtm(temp,fill="colors")
    
    tmap_arrange(tm1, tm2)