【问题标题】:Convert Base64 to PNG/JPEG file in R在 R 中将 Base64 转换为 PNG/JPEG 文件
【发布时间】:2016-08-11 00:43:02
【问题描述】:

我在下面的帖子中看到了将 png 文件转换为 base64 的过程 Convert R image to Base 64

我想做与你完全相反的事情。我有一个存储在变量“capimg”中的图像的 base64,现在我想将它转换为 png 或 jpeg 文件。你能帮我对这个过程进行逆向工程吗?

这可行吗?

我已经看到使用下面的 php 完成了这项工作,但我需要一个 R 脚本来做同样的事情

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

事实上,我还可以使用 base64enc 包将 base64 解码为向量,如下所示 y

但我不知道如何进一步进行

【问题讨论】:

    标签: javascript php r base64 png


    【解决方案1】:

    这对我有用:

    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 由 Wikipedia 提供,访问自 here

    【讨论】:

    • 谢谢,这对我也有帮助。
    猜你喜欢
    • 2017-07-02
    • 2012-12-22
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 2019-12-16
    • 1970-01-01
    • 2013-10-31
    相关资源
    最近更新 更多