# Convert string to JPEG

**URL:** <https://discourse.julialang.org/t/convert-string-to-jpeg/97254>\
**Category:** New to Julia\
**Tags:** images\
**Created:** [April 8, 2023, 7:10pm UTC](https://discourse.julialang.org/t/convert-string-to-jpeg/97254 "2023-04-08T19:10:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cirobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cirobr/32/219994_2.png) [@cirobr](https://discourse.julialang.org/u/cirobr)\
**Post date:** [April 8, 2023, 7:10pm UTC](https://discourse.julialang.org/t/convert-string-to-jpeg/97254/1 "2023-04-08T19:10:49Z")

</div>

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:

```julia
\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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [April 8, 2023, 7:38pm UTC](https://discourse.julialang.org/t/convert-string-to-jpeg/97254/2 "2023-04-08T19:38:28Z")

</div>

yeah those are the magic bytes for [JPEG File Interchange Format](https://en.wikipedia.org/wiki/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](https://juliaimages.org/latest/install/#sec_imageio)

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

```

---

<div class="post-metadata">

**Author:** ![cirobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cirobr/32/219994_2.png) [@cirobr](https://discourse.julialang.org/u/cirobr)\
**Post date:** [April 8, 2023, 9:52pm UTC](https://discourse.julialang.org/t/convert-string-to-jpeg/97254/3 "2023-04-08T21:52:50Z")

</div>

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:

```julia
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").

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [April 8, 2023, 10:02pm UTC](https://discourse.julialang.org/t/convert-string-to-jpeg/97254/4 "2023-04-08T22:02:10Z")

</div>

> [@cirobr](#):
>
> FileIO.save(“img01.jpg”, strimg)

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

---

<div class="post-metadata">

**Author:** ![cirobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cirobr/32/219994_2.png) [@cirobr](https://discourse.julialang.org/u/cirobr)\
**Post date:** [April 8, 2023, 10:06pm UTC](https://discourse.julialang.org/t/convert-string-to-jpeg/97254/5 "2023-04-08T22:06:16Z")

</div>

It works. Thanks!
