6

I want to embed a compressed image in a notebook so that only an icon is visible (and not the whole image).

The following procedure seems to work :

ExampleData[{"TestImage", "Lena"}] // ExportString[#, "JPG"] & // 
 Iconize[#, "lena 00 compressed"] &  

enter image description here

One can copy-paste the icon above in the following code and evaluate it.
Then the image is retrieved.

ImportString["paste the icon here","JPG"]     

enter image description here

enter image description here

The problem

If I quit, reopen and evaluate only the cell with ImportString[..., it doesn't work anymore.

enter image description here

Any solution/workaround for this problem ?

Notes :

  • My real aim is to embed a video as a list of 200 compressed images under a simple icon. The compression is necessary because of this problem. Morever, I need to retrieve a list of independently compressed images in order to Manipulate[] them with good reactivity (forward and backward. The decompression, which is quick, is made on the fly)

  • I have tried without success to replace Iconize[...] by some code with Interpretation[...].

Mathematica 11.3 Windows 7 (64bits)

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
andre314
  • 18,474
  • 1
  • 36
  • 69

1 Answers1

6

Update

@andre mentions that an alternative to the two step procedures in my original answer is to convert the image to a byte array directly using ExportByteArray. So:

Iconize[
    ExportByteArray[ExampleData[{"TestImage", "Lena"}], "JPG"],
    "lena 00 compressed"
]

enter image description here

enter image description here

Original answer

I don't know what the issue is (perhaps a character encoding issue), and it would be worth reporting it to support. As a workaround, you can also compress the string:

Iconize[
    Compress @ ExportString[ExampleData[{"TestImage", "Lena"}], "JPG"],
    "lena 00 compressed"
]

enter image description here

enter image description here

The import still works after quiting and reopening.

Addendum

Probably a better approach is to convert to a ByteArray instead of a compressed string, since the JPEG is already compressed. The byte array will be smaller and faster to convert back to the JPG string.

Iconize[
    StringToByteArray @ ExportString[ExampleData[{"TestImage", "Lena"}], "JPG"],
    "lena 00 compressed"
]

enter image description here

enter image description here

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • @andre I would think so. Maybe make it clear this is simply you reporting a bug and not needed "help" per se. – chuy May 18 '18 at 19:04
  • Thanks to your answer, I have investigated in another direction and I finally discovered ExportByteArray[...,"JPG"] and ImportByteArray[...,"JPG"] which seem to be better in all points of view. Do I post a answer or do you prefer to complete yours ? – andre314 May 20 '18 at 18:53