Skip to content

Instantly share code, notes, and snippets.

@shanenoi
Last active July 31, 2020 01:59
Show Gist options
  • Save shanenoi/d4843b1915897bf2068ec69572a6160d to your computer and use it in GitHub Desktop.
Save shanenoi/d4843b1915897bf2068ec69572a6160d to your computer and use it in GitHub Desktop.
hiện thông số hoạt động của máy tính lên wallpaper (cpu, ram, ....)
# python3 -m pip install psutil
# python3 -m pip install opencv-python
import cv2
import copy
import os
import psutil
import time
import shutil
class Metric:
def __init__(
self, name, function,
attr, ops_scale_of_screen,
metric_size, metric_thickness
):
self.name = name
self.function = function
self.attr = attr
self.ops = ops_scale_of_screen
self.metric_size = metric_size
self.metric_thickness = metric_thickness
def __str__(self):
if self.attr:
return f"{self.name}: {eval(f'{self.function}().{self.attr}')}%"
else:
return f"{self.name}: {eval(f'{self.function}()')}%"
def main():
METRICS = [
Metric("CPU", "psutil.cpu_percent", None, (9/10, 1/10), 2, 3),
Metric("RAM", "psutil.virtual_memory", "percent", (9/10, 1.5/10), 2, 3),
]
original_file = os.popen(
'gsettings get org.gnome.desktop.background picture-uri'
).read()[8:-2]
temp_file = f"{original_file}.tmp"
if not os.path.exists(temp_file):
shutil.copyfile(original_file, temp_file)
original_image = cv2.imread(temp_file, 1)
while True:
try:
writer_image = copy.deepcopy(original_image)
shape = writer_image.shape
font = cv2.FONT_HERSHEY_TRIPLEX
for info in METRICS:
cv2.putText(
writer_image, str(info),
# height # weight
(int(shape[1]*info.ops[0]), int(shape[0]*info.ops[1])),
font, info.metric_size, (0, 255, 0), info.metric_thickness, cv2.LINE_AA
)
cv2.imwrite(original_file, writer_image)
time.sleep(2)
except KeyboardInterrupt:
cv2.imwrite(original_file, original_image)
break
os.remove(temp_file)
if __name__ == "__main__":
print("ctrl+C to exit!")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment