Skip to content

Instantly share code, notes, and snippets.

@tano297
Last active July 29, 2020 22:13
Show Gist options
  • Save tano297/15c3006ed2d5b0eaedb624279e2691c6 to your computer and use it in GitHub Desktop.
Save tano297/15c3006ed2d5b0eaedb624279e2691c6 to your computer and use it in GitHub Desktop.
Dim image from one folder and put it in another, for each new image
#!/usr/bin/env python3
import numpy as np
import cv2
import time
import os
DIM = 0.5
ROOTDIR = "/home/tano/Dropbox/earth"
SAVEDIR = "/home/tano/Dropbox/earth/dimmed"
if __name__ == "__main__":
# get initial list of file
files = os.listdir(ROOTDIR)
files = [f for f in files if os.path.isfile(os.path.join(ROOTDIR, f))]
# check every time there is a new image in the image folder, and dim it
while True:
# check images
new_files = os.listdir(ROOTDIR)
new_files = [f for f in new_files if os.path.isfile(
os.path.join(ROOTDIR, f))]
# print(len(files), len(new_files))
if len(files) < len(new_files):
# check newest image
for fname in new_files:
if not fname in files:
# open the image
img = cv2.imread(os.path.join(ROOTDIR, fname))
print(img.max())
print(img.min())
if img is not None:
print("Saving img {} in dimmed".format(fname))
# dim it
img = img.astype(np.float32) * DIM
print(img.max())
print(img.min())
img = img.astype(np.uint8)
print(img.max())
print(img.min())
# save it
cv2.imwrite(os.path.join(SAVEDIR, fname), img)
files = new_files
# sleep for a couple of seconds
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment