在R中的操作方法:加载图像文件,在图像上打印文本,保存修改后的图像 [英] How to do in R: load an image file, print text on image, save modified image

查看:109
本文介绍了在R中的操作方法:加载图像文件,在图像上打印文本,保存修改后的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...理想情况下,使用PNG图像文件(如果不是JPEG).我可以用

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

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

但是被困在那里.目的是在图像上键入"Hello world"并保存.

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

谢谢.

推荐答案

我认为这应该很好

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()

这篇关于在R中的操作方法:加载图像文件,在图像上打印文本,保存修改后的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆