0

I want to take a screenshot and read the bytes of it. This is doing what I need:

from PIL import ImageGrab

img = ImageGrab.grab((0, 0, 1920, 1080))
img.save("tmp.jpg")

with open("tmp.jpg", "rb") as reader:
    image = reader.read() # image is the needed output

As you can see I first have to save it before I can use it in the way I want to. Please show me a better way for this problem because saving it before the use takes to much time.

Deepthought
  • 118
  • 9
  • Is [this](https://stackoverflow.com/q/2846947/8353711) helpful? – shaik moeed Jul 06 '19 at 13:24
  • sadly not... the output has to be a variable with type bytes – Deepthought Jul 06 '19 at 13:32
  • 1
    using `io.BytesIO` you can create file in memory. Maybe it will work faster. – furas Jul 06 '19 at 15:27
  • see [Get compressed image byte representation in memory](https://stackoverflow.com/questions/44225249/get-compressed-image-byte-representation-in-memory). It uses `io.BytesIO` – furas Jul 06 '19 at 15:28
  • It's not really faster. The actual problem is PIL. That's why I'll use other ways for taking screenshots. Thank you anyway! – Deepthought Jul 06 '19 at 15:43
  • What do you do next in your code with the JPEG-encoded image? I mean what comes after `image = reader.read()`? – Mark Setchell Jul 06 '19 at 16:01
  • I'm streaming it on a flask (python web application framework) website... However, this `b'--frame\r\n'b'Content-Type: image/png\r\n\r\n' + image + b'\r\n'` is the src of an ``. That's how I want to build a remote desktop accessed by the browser. – Deepthought Jul 06 '19 at 16:36

0 Answers0