How do I load an image from an array

I have a JPEG encoded image that I loaded from the web using HTTP.get. How do I decode it using FileIO.load?

I would like to know both how to deal with data in an array instead of in the file system, and whether there’s a better way to load images straight from the web.

1 Like

I think Images.jl is what you’re looking for, the Loading your first image section describes how to load an image from file.

Thanks, but the idea is that I already have the data in memory and I want to decompress it straight from the array, avoiding to store it in a file first.

It’s a bit awkward, but this should work:

using ImageMagick, ImageShow

bytes = read(joinpath(homedir(), "Desktop", "profile.jpg"))

ImageMagick.load_(bytes)
1 Like

Works like a charm, thanks a lot! No idea how I missed it!

Well, it’s an internal interface and pretty hidden :wink: FileIO should just support loading from Vector{UInt8}^^

The following works for me

HTTP.get(url).body |> IOBuffer |> load

Tested with jpg and png, but maybe in some cases the format cannot be inferred and in this case the extension or the content-type should be used to determine the format.

3 Likes