Last active
June 21, 2023 22:48
-
-
Save eugrus/c133e918554023fa463bb0de0fa7b891 to your computer and use it in GitHub Desktop.
Renames images in the current dir prepending their blurriness score to the file names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import cv2 | |
def is_image_blurry(image): | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
blur_score = cv2.Laplacian(gray, cv2.CV_64F).var() | |
return blur_score | |
def rename_images_with_blur_score(folder_path): | |
for filename in os.listdir(folder_path): | |
file_path = os.path.join(folder_path, filename) | |
if os.path.isfile(file_path): | |
image = cv2.imread(file_path) | |
if image is not None: | |
blur_score = int(is_image_blurry(image)) | |
new_filename = f"{blur_score}_" + filename | |
new_file_path = os.path.join(folder_path, new_filename) | |
os.rename(file_path, new_file_path) | |
print(f" {filename} -> {new_filename}") | |
folder_path = "." | |
rename_images_with_blur_score(folder_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment