Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have seen the process to convert a png file to base64 work in the post below Convert R image to Base 64

I would like to do the exact opposite of what you did. I have a base64 of a image stored in a variable "capimg" and now I want to convert it into a png or jpeg file. Can you help me reverse engineer the process.

Is this doable?

I have seen this done using php like below but I need a R script to do the same

<?php
$data = urldecode($_POST['imageData']);
list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);
file_put_contents('image.png', $data);
?>

In fact I am also able to decode the base64 into a vector using the base64enc package like below y <- base64decode(capimg)

But I do not know how to proceed any further

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
453 views
Welcome To Ask or Share your Answers For Others

1 Answer

this works for me:

library(base64enc)

enc <- base64encode("images.jpg")
conn <- file("w.bin","wb")
writeBin(enc, conn)
close(conn)

inconn <- file("w.bin","rb")
outconn <- file("img2.jpg","wb")
base64decode(what=inconn, output=outconn)
close(inconn)
close(outconn)

images.jpg courtesy of Wikipedia accessed from here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...