簡體   English   中英

在R中重命名JPG文件

[英]Renaming JPG files in R

我有一張田野地圖,我在那里拍攝了地塊的圖像。 這些照片是從左到右拍攝的,像蛇一樣; 所以,從101,118,119,136,137,138,135,120,117,102,103,116 ......等。 圖片也按此順序排列; 但是,它們的名稱不同,與繪圖不對應,文件名看起來像100_498,100_499,100_500,100_501。 因此,對於進一步的細節,圖101等於文件100_498並且圖118等於文件100_499等...我需要能夠通過他們如何將場中的圖片拍攝到它們對應的圖形來重命名文件。地圖如下。 如果我用我的代碼重命名它們將從上到下進行重復,因為它是蛇形的,圖片將在文件夾中按順序排列。

現在我需要重新做R代碼...任何建議??? 我需要重命名這些文件,使其對應於此圖。

這是我的代碼,如果他們將從底部到頂部和蛇形數據... 101,102,103,104,105,106,107,108,109,110,111,112 ......等等。

Plot map
109 110 127 128 145
108 111 126 129 144
107 112 125 130 143
106 113 124 131 142
105 114 123 132 141
104 115 122 133 140
103 116 121 134 139
102 117 120 135 138
101 118 119 136 137

f <-list.files(pattern="*.JPG") #imports files names
head(f) #first 6 rows of data

new_names <- paste("Plot_", #create new file name
               formatC(seq(length(file_names)), #writes the number in 
                #sequential order
               width=2,flag="0"), 
               ".JPG",sep="")
head(new_names) #first 6 rows of data
file.rename(from=f, to=new_names) #replaces old file name with new file name
list.files(pattern=".JPG") #check to make sure it was done

以下函數將fileno轉換為您要查找的100_498格式。 此示例適用於您的特定情況,但如果您需要更普遍地使用它,您可能希望對其進行參數化。

filecoords <- function(fileno){
  fcol <- 498 + (fileno - 101) %/% 9
  frow <- ifelse(fcol %% 2, 108-((fileno-101) %% 9),100+((fileno-101) %% 9))
  return(paste(frow, fcol, sep="_"))
}

filecoords(c(101, 105, 124))

[1] "100_498" "104_498" "105_500"

反函數也可能有用。 它需要一個字符串,如“104_498”,並使用繪圖中的數字返回文件名“Plot_nnn”。

filenos <- function(coord){
  coords <- as.numeric(unlist(strsplit(coord,"_")))
  base <- 9*(coords[2]-498)+101
  fileno <- base+ifelse(coords[2] %% 2,8-((coords[1]-100) %% 9),(coords[1]-100) %% 9)
  return(paste0("Plot_",fileno))
}

filenos("105_500")
[1] "Plot_124"

所以你可以使用這樣的東西重命名你的文件......

oldfiles <- list.files(pattern="\\.JPG") #get list of files
oldfilestems <- gsub("\\.JPG","",oldfiles) #remove the suffix
newfilestems <- sapply(oldfilestems,filenos) #assumes your old file stems are "100_498" format
newfiles <- paste0(newfilestems,".JPG") #add the suffix
file.rename(oldfiles,newfiles) #rename

以下函數創建一個查找表,將一個方向的鋸齒形轉換為另一個方向的鋸齒形...

zigzag <- function(n=45,r=9){ #n=number of plots, r=number of rows
  #create desired new order as a matrix
  new <- matrix(1:n,nrow=r)
  even <- 2*seq_len(ncol(new)/2)
  new[,even] <- apply(new[,even],2,rev)

  #create old order as a matrix
  old <- t(matrix(1:n,ncol=r))
  even <- 2*seq_len(nrow(old)/2)
  old[even,] <- t(apply(old[even,],1,rev))

  #reduce to vectors and return look-up table as a dataframe
  df <- data.frame(old=as.vector(old),new=as.vector(new))
  return(df)
}

然后,您可以按如下方式重命名文件...

lookup <- zigzag() #create lookup table

oldfiles <- paste0("100_",497+lookup$old,".JPG") 
newfiles <- paste0("Plot_",100+lookup$new,".JPG")

file.rename(oldfiles,newfiles)

head(oldfiles,10)
 [1] "100_498.JPG" "100_507.JPG" "100_508.JPG" "100_517.JPG" "100_518.JPG" "100_527.JPG" "100_528.JPG" "100_537.JPG"
 [9] "100_538.JPG" "100_499.JPG"
head(newfiles,10)
 [1] "Plot_101.JPG" "Plot_102.JPG" "Plot_103.JPG" "Plot_104.JPG" "Plot_105.JPG" "Plot_106.JPG" "Plot_107.JPG"
 [8] "Plot_108.JPG" "Plot_109.JPG" "Plot_118.JPG"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM