Convert string to JPEG

Cheers. I have a string of over 4k characters that apparently is a JPEG image (the source I’ve downloaded says so). I’ve searched the web on how to convert the string to an image, without success.

The first 50 characters are as follows:

\xff\xd8\xff\xe0\0\x10JFIF\0\x01\x01\0\0\x01\0\x01\0\0\xff\xdb\0C\0\b\x06\x06\a\x06\x05\b\a\a\a\t\t\b\n\f\x14\r\f\v\v\f\x19\x12\x13\x0f

Any help is appreciated. Thanks.

yeah those are the magic bytes for JPEG File Interchange Format.

If you save that thing into a file, you should be able to open it with Images.jl: Getting started · JuliaImages

using Images, FileIO
# specify the path to your local image file
img_path = "/path/to/image.png"
img = load(img_path)

Have tried saving with:

using Images, FileIO
FileIO.save(“img01.jpg”, strimg)

And the following error message came out, apparently not accepting the input string in strimg:

All errors:
===========================================
MethodError: no method matching fileio_save(::File{DataFormat{:JPEG}, String}, ::String)
Closest candidates are:
  fileio_save(::File{DataFormat{:JPEG}}, ::AbstractArray; kwargs...) at ~/.julia/packages/JpegTurbo/FDKrF/src/fileio.jl:10
===========================================
MethodError: no method matching save(::File{DataFormat{:JPEG}, String}, ::String)
Closest candidates are:
  save(::File{DataFormat{:EXR}}, ::Any...; kwargs...) at ~/.julia/packages/ImageIO/xMHN9/src/ImageIO.jl:141
  save(::File{DataFormat{:QOI}}, ::Any...; kwargs...) at ~/.julia/packages/ImageIO/xMHN9/src/ImageIO.jl:152
  save(::File{DataFormat{:JPEG}}, ::AbstractArray; kwargs...) at ~/.julia/packages/ImageIO/xMHN9/src/ImageIO.jl:182
===========================================
MethodError: no method matching mapIM(::Char)
Closest candidates are:
  mapIM(::RGB{T}) where T<:Normed at ~/.julia/packages/ImageMagick/b8swT/src/ImageMagick.jl:311
  mapIM(::RGB{T}) where T at ~/.julia/packages/ImageMagick/b8swT/src/ImageMagick.jl:310
  mapIM(::GrayA{T}) where T<:Normed at ~/.julia/packages/ImageMagick/b8swT/src/ImageMagick.jl:307
  ...
===========================================
┌ Warning: Mapping to the storage type failed; perhaps your data had out-of-range values?
│ Try `map(clamp01nan, img)` to clamp values to a valid range.
└ @ ImageMagick /home/ubuntu/.julia/packages/ImageMagick/b8swT/src/ImageMagick.jl:180
Errors encountered while save File{DataFormat{:JPEG}, String}("img01.jpg").

no you don’t need FileIO for saving because you just have a string, usually string is not jpeg so this confuses the function you’re calling.

you just want write("img01.jpg", strimg) where this write() just dumps the text

and then if all goes well, you can open the file with Images.jl

4 Likes

It works. Thanks!