Posted on

这个教程来自Y叔的听说你用R把证件照给一键换底了

转自:https://www.jianshu.com/p/cb36064cb4b4

首先准备一张证件照(打码照),比如蓝底图,png或jpg格式的都可以,用magick这个包的image_read()函数把它读进来,用ggplotify转换成ggplot图片

library(magick)
x<-image_read('你的照片.jpg')
ggplotify::as.ggplot(x)
蓝底

用image_fill()函数直接就可以处理图片,选一个自己想要换的颜色,比如白色,代码一输,as.ggplot一转,就成了白底图 。

y<-image_fill(x,'white',fuzz = 20)
ggplotify::as.ggplot(y)
白底

再试试别的颜色,组合一下

p1<- ggplotify::as.ggplot(image_fill(x,'red',fuzz = 20))
p2<- ggplotify::as.ggplot(image_fill(x,'brown',fuzz = 20))
p3<- ggplotify::as.ggplot(image_fill(x,'navyblue',fuzz = 20))
p4<- ggplotify::as.ggplot(image_fill(x,'steelblue',fuzz = 20))
p5<- ggplotify::as.ggplot(image_fill(x,'green',fuzz = 20))
p6<- ggplotify::as.ggplot(image_fill(x,'white',fuzz = 20))
cowplot::plot_grid(p1,p2,p3,p4,p5,p6,ncol = 3,labels = "AUTO")
cowplot::plot_grid(p1,p2,p3,p4,p5,p6,ncol = 3,labels = "AUTO")


最后用ggsave保存一下,还可以自己设置照片的长度和宽度,以及dpi

比如,保存为宽2.5cm,高3.5cm、分辨率为150的jpg照片

ggsave("pic.jpg",width=2.5,heigh=3.5,unit=c('cm'),dpi=150)

想试一下吗?

2 Replies to “使用R语言给照片换底色”

欧阳松进行回复 取消回复

邮箱地址不会被公开。 必填项已用*标注