简体   繁体   中英

Python screenshot to base64

Here is the way I take screenshots:

import wx

app = wx.App(False)
s = wx.ScreenDC()
w, h = s.Size.Get()
b = wx.EmptyBitmap(w, h)
m = wx.MemoryDCFromDC(s)
m.SelectObject(b)
m.Blit(0, 0, w, h, s, 0, 0)
m.SelectObject(wx.NullBitmap)
b.SaveFile("hey.png", wx.BITMAP_TYPE_PNG)

I don't want to save the screenshot as an image file "hey.png", I want it as base64 string.
My final purpose is just to print the base64 string, then I want to put it manually in html code , here:

<img src="data:image/png;base64,here" />

How can I do that? Thanks in advance.

You have to somehow get the bytes of that file. This can be done two ways:

  1. Through the wx API, if possible (I am familair with the API but you should be able to look it up if it is possible)
  2. By saving the file, reading the file and finally deleting the file.

For the actual encoding, you can use the built in base64 API. Note that I linked to the Python 2 wiki instead of the Python 3 wiki, since to my knowledge wxPython is only available for python 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM