with open(image.png,"rb") as f: 在打字稿中?

发布于 2025-01-09 02:48:10 字数 716 浏览 0 评论 0 原文

如何打开 png 文件,如 python 的方法: with open(path+im,"rb") as f: 但在打字稿中? 如果我有一个包含一些 png 的文件夹,并且我想在 python 中读取它们并将它们转换为 base 64,我会使用该方法,但是 typescript 中的对应方法是什么? Sameone 可以帮我吗?

我在 python 中有这段代码:

def image_integrity():
    list_of_sha256 = []
    path = "./nft_test/"
    for im in os.listdir(path):
        with open(path+im,"rb") as f:
            f = base64.b64encode(f.read()) # read entire file as bytes
            h = hashlib.new("sha256")
            h.update(f)
            result = h.digest()
            result = base64.b64encode(result).decode("utf-8")
            print(result)
            list_of_sha256.append(result)
    return list_of_sha256

我想用打字稿重写它。

How to open png files like method : with open(path+im,"rb") as f: of python but in typescript?
If I have a folder that contain some png and i want to read and convert them to base 64 in python i use that method but which is the corrispective method in typescript?
Can sameone help me please?

I have this code in python :

def image_integrity():
    list_of_sha256 = []
    path = "./nft_test/"
    for im in os.listdir(path):
        with open(path+im,"rb") as f:
            f = base64.b64encode(f.read()) # read entire file as bytes
            h = hashlib.new("sha256")
            h.update(f)
            result = h.digest()
            result = base64.b64encode(result).decode("utf-8")
            print(result)
            list_of_sha256.append(result)
    return list_of_sha256

and I want to rewrite it in typescript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2025-01-16 02:48:10

看来你想从目录中读取。

fs.readFile( path.resolve(__dirname, './data.txt' ), 'utf-8', function(err, buf) 
{
    var data= buf.toString();
    console.log('load', 'OK'.green);
});

seems you are want read from dir.

fs.readFile( path.resolve(__dirname, './data.txt' ), 'utf-8', function(err, buf) 
{
    var data= buf.toString();
    console.log('load', 'OK'.green);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文