Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do in R: load an image file, print text on image, save modified image [duplicate]

Tags:

r

image

png

... 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.

like image 219
Dimitri Shvorob Avatar asked May 22 '14 12:05

Dimitri Shvorob


1 Answers

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()
like image 106
MrFlick Avatar answered Sep 20 '22 00:09

MrFlick