Skip to content

Instantly share code, notes, and snippets.

@smellslikeml
Last active September 25, 2023 09:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smellslikeml/b9ce80c358d583f85798155c6a01019f to your computer and use it in GitHub Desktop.
Save smellslikeml/b9ce80c358d583f85798155c6a01019f to your computer and use it in GitHub Desktop.
hidden screenshot with xlib and PIL
#!/usr/bin/env python
import os
from Xlib import display, X
from PIL import Image
dsp = display.Display()
screen = dsp.screen()
W = screen.width_in_pixels
H = screen.height_in_pixels
root = screen.root
raw = root.get_image(0, 0, W, H, X.ZPixmap, 0xffffffff)
image = Image.frombytes("RGB", (W, H), raw.data, "raw", "BGRX")
file_path = os.path.join(os.environ['HOME'], '.screenshot.png')
image.save(file_path)
@prachib-vision
Copy link

you can simply get resolution of screen by dsp.screen().width_in_pixels, dsp.screen().height_in_pixels

@smellslikeml
Copy link
Author

That's right, thanks!
Updating to be more concise using your suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment