Skip to content

Instantly share code, notes, and snippets.

@f0nzie
Created May 27, 2021 01:17
Show Gist options
  • Save f0nzie/e76d6d68fc4c3f7a47083ac83cbdfa82 to your computer and use it in GitHub Desktop.
Save f0nzie/e76d6d68fc4c3f7a47083ac83cbdfa82 to your computer and use it in GitHub Desktop.
#!/home/msfz751/.pyenv/shims/python3
# #!/usr/bin/env python
# change to location of your preferred virtual environment
# Source:
# https://itectec.com/ubuntu/ubuntu-taking-screenshot-of-a-specific-area-from-the-command-line/
import subprocess
import os
import sys
# setting default directories / filenames
home = os.environ["HOME"]
temp = home + "/" + ".scrot_images"
img_in = temp + "/in.png"
#! remove temp previous image. ARR
os.remove( img_in)
# if you prefer, you can change the two line below:
output_directory = home +"/" + "scrot_images" # output directory
filename = "outputfile" # output filename
# creating needed directories if not exist
for dr in [temp, output_directory]:
if not os.path.exists(dr):
os.mkdir(dr)
# creating filename with suffix (-number) to prevent overwriting previous shots
n = 1
while True:
img_out = output_directory + "/" + filename + "_" + str(n) + ".png"
if os.path.exists(img_out):
n = n + 1
else:
break
# reading arguments,arranging commands to perform
coords = sys.argv[1:5] # read from command line
cmd1 = ["scrot", img_in] # first command
# crop image with ImageMagik
cmd2 = ["convert", img_in, "-crop", coords[2] + "x"+coords[3] + "+"+coords[0] + "+" + coords[1], "+repage", img_out]
# Take screnshot, crop image. Execute
for cmd in [cmd1, cmd2]:
subprocess.call(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment