#libraries that I need:

library(jpeg)
library(imager)
## Warning: package 'imager' was built under R version 4.3.2
## 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
library(knitr)
## Warning: package 'knitr' was built under R version 4.3.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.2
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:imager':
## 
##     where
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# import the file: 

files = list.files(path = "C:/Users/Chafiaa/OneDrive/Documents/shoes", pattern="\\.jpg")
files
##  [1] "RC_2500x1200_2014_us_53446.jpg" "RC_2500x1200_2014_us_53455.jpg"
##  [3] "RC_2500x1200_2014_us_53469.jpg" "RC_2500x1200_2014_us_53626.jpg"
##  [5] "RC_2500x1200_2014_us_53632.jpg" "RC_2500x1200_2014_us_53649.jpg"
##  [7] "RC_2500x1200_2014_us_53655.jpg" "RC_2500x1200_2014_us_53663.jpg"
##  [9] "RC_2500x1200_2014_us_53697.jpg" "RC_2500x1200_2014_us_54018.jpg"
## [11] "RC_2500x1200_2014_us_54067.jpg" "RC_2500x1200_2014_us_54106.jpg"
## [13] "RC_2500x1200_2014_us_54130.jpg" "RC_2500x1200_2014_us_54148.jpg"
## [15] "RC_2500x1200_2014_us_54157.jpg" "RC_2500x1200_2014_us_54165.jpg"
## [17] "RC_2500x1200_2014_us_54172.jpg"
# view the shoes:

par(mfrow = c(4, 5), mar = c(0.6, 0.6, 0.6, 0.6))

for (file in files) {
  file1 <- file.path("C:/Users/Chafiaa/OneDrive/Documents/shoes", file)
  img <- readJPEG(file1)
  plot(0:1, 0:1, type = "n", xlab = "", ylab = "", xlim = c(0, 1), ylim = c(0, 1), xaxt = "n", yaxt = "n", bty = "n")
  rasterImage(img, 0, 0, 1, 1)
}

# resize the shoes:

height <- 1200
width <- 2500
scale <- 20

# Load the Data into an Array:

newdata <- array(0, dim = c(length(files), height/scale * width/scale * 3))

for (i in 1:length(files)) {
  file1 <- file.path("C:/Users/Chafiaa/OneDrive/Documents/shoes", files[i])
  img <- resize(load.image(file1), height/scale, width/scale)
  newdata[i,] <- as.vector(img)
}
# Generate Principal Components
mypca <- princomp(t(newdata), scores = TRUE, cor = TRUE)

# Verify that sum of variance is 1
sum(mypca$sdev^2 / sum(mypca$sdev^2))
## [1] 1
# Extract squared eigenvalues
mycomponents <- (mypca$sdev^2) / sum(mypca$sdev^2)

mycomponents=mypca$sdev^2/sum(mypca$sdev^2)
sum(mycomponents[1:5]) # the first 5 components account for 88.42% of the variability
## [1] 0.8842959
sum(mycomponents[1:15]) # the first 15  components account for 98.78 % of the variability
## [1] 0.9878529
mypca2 <- t(mypca$scores)

# score reshape:

dim(mypca2) <- c(length(files), height/scale, width/scale, 3)


# plot
par(mfrow = c(4, 5), mar = c(0.9, 0.9, 0.9, 0.9))


for (i in seq_along(files)) {
  new_img <- as.cimg(mypca2[i,,,])
  plot(0:1, 0:1, type = "n", xlab = "", ylab = "", xlim = c(0, 1), ylim = c(0, 1), xaxt = "n", yaxt = "n", bty = "n")
  rasterImage(new_img, 0, 0, 1, 1)
}
## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour

## Warning in as.cimg.array(mypca2[i, , , ]): Assuming third dimension corresponds
## to colour