【问题标题】:Julia download image from URL directly into memoryJulia 将 URL 中的图像直接下载到内存中
【发布时间】:2020-12-23 05:36:15
【问题描述】:

我想从网上下载这张图片。 Julia中有没有办法避免将文件下载到磁盘然后将其重新加载,并将其直接加载到内存中? 例子: https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png

目前我可以做到这一点,这需要我将数据写入磁盘,然后将其加载回内存:

Using Images
Using HTTP
download("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png", "google.png")
img = load("google.png")

julia> typeof(img)
Array{RGB{Normed{UInt8,8}},2}

我可以看到我期望的类型是一个 RGB 值数组

但是,如果我尝试直接使用 HTTP 发出请求,我会得到一个矢量,它似乎不容易转换为图像数组格式

r = HTTP.get("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")

julia> r.body
13504-element Array{UInt8,1}:
 0x89
 0x50
 0x4e
 0x47
 0x0d
 0x0a
 0x1a
    ⋮
 0x45
 0x4e
 0x44
 0xae
 0x42
 0x60
 0x82

直接将此图像数据转换为正确图像格式的最佳方法是什么?

【问题讨论】:

    标签: arrays image io julia http-get


    【解决方案1】:

    这已在ImageMagick.jl 中编程

    设置:

    using HTTP, ImageMagick
    r = HTTP.get("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png")
    buffer = IOBuffer(r.body)
    

    解析:

    julia> ImageMagick.load(buffer)
    184×544 Array{RGBA{N0f8},2} with eltype ColorTypes.RGBA{FixedPointNumbers.Normed{UInt8,8}}:
     RGBA{N0f8}(0.0,0.0,0.0,0.0)        …          RGBA{N0f8}(0.0,1.0,1.0,0.0)
    ...
    

    【讨论】:

    • 谢谢 - 我认为这会存在,但不知道去哪里找(不幸的是,关于这个主题的很多文档/问题已经过时了)。另外请注意,我必须使用 HTTP.get() 而不是 HTTP.open(),不确定是不是只有我一个人,否则工作得很好!
    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    相关资源
    最近更新 更多