library(OpenImageR)
我正在尝试将 RGB 图像转换为灰度图像然后保存它们
path = list.files("/dat",".jpg",
full.names = T, all.files = TRUE)
imgr=lapply(path, readImage)
imgs<-lapply(imgr, rgb_2gray)
下面是我用过但不知道如何修改的功能
writeImage(img.fi))
您可能还想在
lapply
上使用 writeImage()
。
> library(OpenImageR)
>
> path <- '~/sandbox/animals'
> setwd(path)
>
> fnms <- list.files(path, ".jpg", full.names=TRUE, all.files=TRUE)
> imgr <- lapply(fnms, readImage) |> lapply(rgb_2gray)
> new_fnms <- sub('.jpg', '_gry.jpg', fnms)
> lapply(seq_along(new_fnms), \(i) writeImage(imgs[[i]], new_fnms[i]))
[[1]]
NULL
[[2]]
NULL
> dir()
[1] "pic1_gry.jpg" "pic1.jpg" "pic2_gry.jpg" "pic2.jpg"
数据:
urls <- c('https://www.publicdomainpictures.net/pictures/50000/velka/lion-and-lioness-animals.jpg', 'https://c.pxhere.com/photos/21/01/elephant_animals_water_sri_lanka_proboscis-930457.jpg!d') > download.file(urls, Paste0('~/sandbox/animals/', sprintf('pic%s.jpg', 1:2)))