خرید بک لینک

Vote count: 0

I am new to R. I have a folder full of images(RGB) which are not of the same dimensions. My requirement is to have them all in the same dimensions which would involve resizing a bunch of them. I wrote the following code to get this done

#EBImage
library(EBImage)
path = "G:/Images/"
file.names = dir(path,full.names = TRUE, patte =".jpeg")
reqd_dim = c(3099,2329,3)
sprintf("Number of Image Files is: %d", length(file.names))

for(i in 1:length(file.names)){
  correction_flag = FALSE
  print("Loop Number:")
  flush.console()
  print(i)
  flush.console()
  img = readImage(file.names[i])
  # Checking if the dimensions are the same
  for (j in 1:length(reqd_dim)) {
    if(dim(img)[j]!=reqd_dim[j]){
      correction_flag = TRUE
      break
    }
  }
  if(correction_flag==TRUE){
    print("Correcting dimensions of the image")
    flush.console()
    writeImage(img[1:3099, 1:2329, 1:3],file.names[i],quality = 100)
  }
}

My problem is that while the images are originally between 500-600 kb in size, the ones that are resized end up being between 1.8 to 2 Mb. In my particular case the images are in either of the two sizes - 3100x2329 or 3099x2329. So my resizing involves removing an extra 2329 pixels to make all images 3099x2329. I am ok with the file size of the files going down a bit as I expect some information to be lost; but in my case the file size is increasing more than three-fold. Alteatively I have thought of converting the images into matrices(which is supported by EBImage) and remove the extra row. But I have two issues here, one is that I don't know how to do it and two is even if I found a way to do it, I'm afraid I might loose some information if I ever needed to convert it back to an image. I'm open to an improvement over this approach, or a totally different one as well. My only requirement is that I need to be able to do resize my images in R without adding or losing any information(apart from the information in the pixels to be removed)

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: چهارشنبه 12 خرداد 1395 ساعت: 13:30

صفحه بندی