0

I would like to print the plot as a Base 64 encoded string to stdout. How can I do that?

So far, I can print to png by

f = figure
set(f, "visible", "off")
plot([1,2,3,4])
print("MyPNG.png", "-dpng")

but I really need the Base 64 encoded string for the png without having to save the file as a png first.

FullStack
  • 5,902
  • 4
  • 43
  • 77
  • First step is to get the image data. Is `getimage` sufficient? – Daniel Jul 25 '15 at 08:10
  • @Daniel: that will only work if there is an `image` in the figure (i.e the result of `image`, `imagesc`, or `imshow` functions) – Amro Jul 25 '15 at 08:12
  • 2
    @FullStack: see solutions mentioned here: http://stackoverflow.com/q/4137628/97160, that will get you an image back. Also in your code above, you could just read the saved PNG file as an array of bytes, and do the encoding yourself.. – Amro Jul 25 '15 at 08:14
  • Could you give an example of what you want to get. Do you need to base64 encode a PNG file, or do you want to convert a plot (not image) data to a base64 string? – texnic Jul 25 '15 at 12:57
  • @texnic I need to convert plot to base64 string directly. Updating my question now – FullStack Jul 26 '15 at 04:04
  • I am still missing an example of the output. Plot is a very generic thing in Matlab. You may assume serializing the whole figure, with all the settings, and converting it to a string, or just the data. Please be more specific to help us help you. Say, what do you expect for `plot(1:3, 2:2:6)`? If it's a poor example, please give a better one. – texnic Jul 26 '15 at 07:15
  • @Amro yes I can just read a saved PNG but preferred not to, hence the question – FullStack Jul 27 '15 at 03:47
  • @FullStack: did you see the post I linked to? you're basically looking for the GETFRAME function or the undocumented HARDCOPY function. Once you have the captured image, you'd want to use an in-memory PNG encoder. There is no solution out of the box, you're gonna have to use an external library (I'm sure you can find an implementation in Java, which you could easily call in MATLAB)... – Amro Jul 27 '15 at 05:38
  • @FullStack here's one: [ImageIO](http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html) – Amro Jul 27 '15 at 05:45
  • I sure see the example of the input, but please explain what you want to see as the *output* in this case. – texnic Jul 27 '15 at 16:00

0 Answers0