简体   繁体   English

Python屏幕截图到base64

[英]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. 我不想将屏幕截图另存为图像文件“ hey.png”,而是将其保存为base64字符串。
My final purpose is just to print the base64 string, then I want to put it manually in html code , here: 我的最终目的只是打印base64字符串,然后我想在html代码中手动将其放在此处:

<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) 如果可能,通过wx API(我熟悉该API,但如果可以的话,您应该可以查找它)
  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. 对于实际的编码,您可以使用内置的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. 请注意,我链接到Python 2 Wiki而不是Python 3 Wiki,因为据我所知wxPython仅适用于python 2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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