R 如何按定义的顺序将图像合并到一个文件中
我有大约100张图片(png)。我不想手动操作,而是希望将它们按定义的顺序(基于文件名)放在一个单独的pdf中(每行12个图像) 有人有什么建议吗 我试着按照托马斯在下面告诉我的,把它们贴在旁边,留一个黑色的边距,我怎样才能去掉它R 如何按定义的顺序将图像合并到一个文件中,r,R,我有大约100张图片(png)。我不想手动操作,而是希望将它们按定义的顺序(基于文件名)放在一个单独的pdf中(每行12个图像) 有人有什么建议吗 我试着按照托马斯在下面告诉我的,把它们贴在旁边,留一个黑色的边距,我怎样才能去掉它 setwd(workingDir); files <- list.files(path=".", pattern="*.png", all.files=T, full.names=T) filelist <- lapply(files, readPNG)
setwd(workingDir);
files <- list.files(path=".", pattern="*.png", all.files=T, full.names=T)
filelist <- lapply(files, readPNG)
names(filelist) <- paste0(basename((files)))
list2env(filelist, envir=.GlobalEnv)
par(mar=rep(0,4))
layout(matrix(1:length(names(filelist)), ncol=15, byrow=TRUE))
for(i in 1:length(names(filelist))) {
img <- readPNG(names(filelist[i]))
plot(NA,xlim=0:1,ylim=0:1,xaxt="n",yaxt="n")
rasterImage(img,0,0,1,1)
}
dev.print(pdf, "output.pdf")
setwd(工作目录);
文件您可以使用光栅图像
功能和png包将它们全部打印在一起。下面是一个简单的例子,展示了如何在PNG中读取,然后(多次)打印
library(“png”)#用于在png中阅读
#示例图像
img请注意,Thomas概述的解决方案在多平面图像中引入了一些源图像中不存在的空白。将参数xaxs='i'
和yaxs='i'
添加到plot()
将删除它
library("png") # for reading in PNGs
# example image
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
# setup plot
dev.off()
par(mai=rep(0,4)) # no margins
# layout the plots into a matrix w/ 12 columns, by row
layout(matrix(1:120, ncol=12, byrow=TRUE))
# do the plotting
for(i in 1:120) {
plot(NA,xlim=0:1,ylim=0:1,bty="n",axes=0,xaxs = 'i',yaxs='i')
rasterImage(img,0,0,1,1)
}
# write to PDF
dev.print(pdf, "output.pdf")
library(“png”)#用于在png中阅读
#示例图像
img有时你只想把一堆PNG图像放入一个PDF文件中,每页一个图像,以便使用PDF阅读器快速方便地查看它们。我是这样做的,在上面的解决方案的基础上,如果需要缩小图像以适应输出PDF(但保留纵横比)。这使您可以轻松地捆绑各种大小的PNG图像
library(png)
# Define a function to get the xright and ytop arguments for rasterImage()
get.raster.image.area.scaling <- function(pngfile) {
imga <- attr(readPNG(pngfile, info=TRUE), "info")
img.width <- imga$dim[1] / imga$dpi[1] # width of PNG image in inches
img.height <- imga$dim[2] / imga$dpi[2] # height of PNG image in inches
img.aspect <- img.height/img.width # aspect ratio of PNG image
ri <- list()
if(img.width > my.width || img.height > my.height) {
# shrink to fit output device page
diff.width <- img.width - my.width
diff.height <- img.height - my.height
if(diff.width >= diff.height) {
# shrink to fit width
ri$width <- 1.0 # xright for rasterImage (fraction of device area)
new.height <- img.aspect * my.width # (in)
ri$height <- new.height/my.height # ytop for rasterImage (fraction of device area)
} else if(diff.height > diff.width) {
# shrink to fit height
ri$height <- 1.0 # ytop for rasterImage (fraction of device area)
new.width <- my.height / img.aspect # (in)
ri$width <- new.width/my.width # xright for rasterImage (fraction of device area)
} else stop("need to debug unexpected situation\n")
} else {
# no shrinking of PNG image needed
ri$width <- img.width / my.width
ri$height <- img.height / my.height
}
return(ri)
} # end of get.raster.image.area.scaling()
pngfiles <- c("yourfile1.png", "yourfile2.png", "yourfile3.png")
num.png <- length(pngfiles)
my.width <- 8 # dimensions of desired PDF device output (in)
my.height <- 10
pdf(file = "testplot.pdf", width=my.width, height=my.height, pointsize=12)
par(mai=rep(0,4)) # no margins
for(i in 1:num.png) {
img <- readPNG(pngfiles[i], native=TRUE)
ri <- get.raster.image.area.scaling(pngfiles[i])
plot(NA, xlim=0:1, ylim=0:1, bty="n", axes=0, xaxs = 'i', yaxs='i')
rasterImage(img, 0, 0, ri$width, ri$height)
}
dev.off()
库(png)
#定义一个函数以获取光栅图像()的xright和ytop参数
老实说,get.graster.image.area.scaling不知道怎么做。我以为使用“par”定义了一行中有多少图像…我尝试了你告诉我的,但我得到了一个错误。我更新了我的帖子。请看一下好吗?@EpiMan在调用plot
时,指定bty=“n”
(意思是boxtype=none)。
library(png)
# Define a function to get the xright and ytop arguments for rasterImage()
get.raster.image.area.scaling <- function(pngfile) {
imga <- attr(readPNG(pngfile, info=TRUE), "info")
img.width <- imga$dim[1] / imga$dpi[1] # width of PNG image in inches
img.height <- imga$dim[2] / imga$dpi[2] # height of PNG image in inches
img.aspect <- img.height/img.width # aspect ratio of PNG image
ri <- list()
if(img.width > my.width || img.height > my.height) {
# shrink to fit output device page
diff.width <- img.width - my.width
diff.height <- img.height - my.height
if(diff.width >= diff.height) {
# shrink to fit width
ri$width <- 1.0 # xright for rasterImage (fraction of device area)
new.height <- img.aspect * my.width # (in)
ri$height <- new.height/my.height # ytop for rasterImage (fraction of device area)
} else if(diff.height > diff.width) {
# shrink to fit height
ri$height <- 1.0 # ytop for rasterImage (fraction of device area)
new.width <- my.height / img.aspect # (in)
ri$width <- new.width/my.width # xright for rasterImage (fraction of device area)
} else stop("need to debug unexpected situation\n")
} else {
# no shrinking of PNG image needed
ri$width <- img.width / my.width
ri$height <- img.height / my.height
}
return(ri)
} # end of get.raster.image.area.scaling()
pngfiles <- c("yourfile1.png", "yourfile2.png", "yourfile3.png")
num.png <- length(pngfiles)
my.width <- 8 # dimensions of desired PDF device output (in)
my.height <- 10
pdf(file = "testplot.pdf", width=my.width, height=my.height, pointsize=12)
par(mai=rep(0,4)) # no margins
for(i in 1:num.png) {
img <- readPNG(pngfiles[i], native=TRUE)
ri <- get.raster.image.area.scaling(pngfiles[i])
plot(NA, xlim=0:1, ylim=0:1, bty="n", axes=0, xaxs = 'i', yaxs='i')
rasterImage(img, 0, 0, ri$width, ri$height)
}
dev.off()