Magick程序包中的问题,在图像中添加页边距,然后注释文本

问题描述

我想读取1500x1060px的图像,在其上添加页边距(右侧约3厘米),并在此页边处绘制文本。 (它是在一个简单的应用程序中,可以让用户更改透明度)。

如果我先输入文字,然后再输入空白:

  image1 = jpeg::readJPEG(file.path(paste(input$map1,'.jpg',sep='')))  
  image1 = abind::abind(image1,image1[,1]) # add an alpha channel
  image1[,4] = input$trans1/100 # set alpha to semi-transparent
  image1<- image_read(image1)
    image1 <-     image_annotate(image1,"Generated with my.webpage",location = "+1550+1000",degrees = 270,size = 40,color = "black",strokecolor = NULL,Boxcolor = NULL)

  png(outfile,width = 1500,height = 1060,units = 'px',res = 300)
  par(mar=c(0,3))
  plot.new()
  rasterImage(image1,1,1)
  dev.off()

在不存在的区域中注释了文本,因此它不会出现。

所以我试图在par(mar =())之后使用image_annotate,但是我现在不知道如何做,因为image_annotate必须与对象一起用作第一个变量。

解决方法

由于您大概不想更改图像以包含光栅化的文本(质量较差),因此我建议使用R buitlin函数 mtext 在空白处打印文本,例如>

img <- jpeg::readJPEG("image.jpeg")
plot.new()
rasterImage(img,rasterImage(img,1,1)
mtext("comment on image",side=1)

在调用 plot.new()之前打开其他设备(pdf,png或其他格式)时,还可以将带注释的图像保存到示例中所示的文件中(不要忘记使用 dev.off()关闭设备,以使其真正被写入)。