9

... Ideally, with a PNG image file, if not JPEG. I can load a flie with

library(png)
img = readPNG("chart.png")

but am stuck there. The goal is to type "Hello world" on the image and save it.

Thank you.

Dimitri Shvorob
  • 495
  • 1
  • 6
  • 24

1 Answers1

20

I think this should work just fine

library(png)

#read file
img<-readPNG("33.png")

#get size
h<-dim(img)[1]
w<-dim(img)[2]

#open new file for output
png("out.png", width=w, height=h)
par(mar=c(0,0,0,0), xpd=NA, mgp=c(0,0,0), oma=c(0,0,0,0), ann=F)
plot.new()
plot.window(0:1, 0:1)

#fill plot with image
usr<-par("usr")    
rasterImage(img, usr[1], usr[3], usr[2], usr[4])

#add text
text(.5,.5, "hello", cex=5, col=rgb(.2,.2,.2,.7))

#close image
dev.off()
MrFlick
  • 195,160
  • 17
  • 277
  • 295