Skip to content

Instantly share code, notes, and snippets.

@mmdbalkhi
Created March 10, 2022 15:18
Show Gist options
  • Save mmdbalkhi/357bc37d9eff2f14d0accffbd31c3e1f to your computer and use it in GitHub Desktop.
Save mmdbalkhi/357bc37d9eff2f14d0accffbd31c3e1f to your computer and use it in GitHub Desktop.
script Take a screenshot by ImageMagick

script Take a screenshot by ImageMagick

This script requires version 3.9 of Python or higher and you must also have imagemagick installed.

useage

  • To take a screenshot in the Pictures folder in the user's home:
python screenshot.py
  • To take a screenshot with custom path:
python screenshot.py -f /tmp/abc.png
#!/bin/python
import logging
import os
import time
from pathlib import Path
from platform import system
from sys import argv, exit, version_info
__version__ = "0.1"
__author__ = "mmdbalkhi"
__license__ = "GPL-3"
home = Path.home()
screenshot_dir = os.path.join(home, "Pictures", "screenshots")
time_format = "%Y-%m-%d-%H:%M:%S"
image_path = os.path.join(screenshot_dir, time.strftime(time_format) + ".png")
class NotFoundError(BaseException):
""""""
class UnSupputedos(BaseException):
""""""
class UnSupputedPython(BaseException):
""""""
if not len(argv) > 1:
argv = None
if not (version_info[0] == 3 and version_info[1] > 9):
raise UnSupputedPython("we need python 3.9 or upper")
if not system() == "Linux":
raise UnSupputedos("this script only support the Linux")
if not os.path.exists("/usr/bin/import"):
logging.warning("imagemagick not installed")
install_image_magic = input("do you want to install imagemagick?").lower()
if (
install_image_magic == "y"
or install_image_magic == "yes"
or install_image_magic == ""
):
if os.path.exists("/etc/debian_version"):
os.system("sudo apt install imagemagic")
elif os.path.exists("/etc/arch-release"):
os.system("sudo pacman -S imagemagic")
else:
raise UnSupputedos(
"we can't install ImageMagick, please install ImageMagick with Manually"
)
else:
exit(1)
if not os.path.isdir(screenshot_dir):
logging.warning("screenshot directory does not exist")
os.mkdir(screenshot_dir)
logging.info("screenshot directory created")
def print_help() -> None:
print(f"** screenshot script version {__version__}**")
print("this script use `import` from image magic")
print("Usage: screenshot [options ...] [ file ]")
print("")
print("-h --help: print this help")
print("-f --file: screenshot image with custom path")
print("")
if argv:
match argv[1]: # TODO: fullscreen add screenshot
case ("-h" | "--help"):
print_help()
case ("-f" | "--file"):
try:
os.system(f"import {argv[2]} -verbose -monitor")
except IndexError:
raise IndexError("we need a file path in this option")
case _:
print_help()
exit(1)
else:
os.system(f"import {image_path} -verbose -monitor")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment