在 R 中将 Base64 转换为 PNG/JPEG 文件

Convert Base64 to PNG/JPEG file in R(在 R 中将 Base64 转换为 PNG/JPEG 文件)
本文介绍了在 R 中将 Base64 转换为 PNG/JPEG 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

这篇关于在 R 中将 Base64 转换为 PNG/JPEG 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)