Skip to content

Instantly share code, notes, and snippets.

@yowasou
Created January 18, 2025 09:37
Show Gist options
  • Save yowasou/72722ba3feec027552697ee2f56bb182 to your computer and use it in GitHub Desktop.
Save yowasou/72722ba3feec027552697ee2f56bb182 to your computer and use it in GitHub Desktop.
import os
from glob import glob
from skimage.metrics import structural_similarity as ssim
import cv2
import sys
from pathlib import Path
def calculate_ssim(image_path1, image_path2):
"""2つの画像間のSSIM(構造的類似性)を計算する"""
img1 = cv2.imread(image_path1, cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread(image_path2, cv2.IMREAD_GRAYSCALE)
# 画像サイズを統一(必要に応じてリサイズ)
img1 = cv2.resize(img1, (100, 100))
img2 = cv2.resize(img2, (100, 100))
# SSIMを計算
score, _ = ssim(img1, img2, full=True)
return score
def cluster_images(image_folder, similarity_threshold=0.8):
"""似た画像をまとめてクラスター化する"""
image_paths = glob(os.path.join(image_folder, "*"))
clusters = [] # クラスターを格納
for image_path in image_paths:
added_to_cluster = False
# 既存のクラスターに追加できるか確認
for cluster in clusters:
representative_image = cluster[0] # クラスターの代表画像
if calculate_ssim(image_path, representative_image) >= similarity_threshold:
cluster.append(image_path)
added_to_cluster = True
break
# 新しいクラスターを作成
if not added_to_cluster:
clusters.append([image_path])
return clusters
# 実行例
image_folder = sys.argv[1] # 画像フォルダのパス
similarity_threshold = 0.15 # 類似性の閾値(0.0〜1.0)
clusters = cluster_images(image_folder, similarity_threshold)
# 結果を表示
fnamehead = "renamed_"
bar = "_"
for idx, cluster in enumerate(clusters):
print(f"クラスター {idx + 1}:")
for idx2, image_path in enumerate(cluster):
# ファイルをリネーム
to_path = os.path.dirname(image_path) + "\\" + fnamehead + str(idx).zfill(4) + "_" + str(idx2).zfill(4) + Path(image_path).suffixes[0]
os.rename(image_path, to_path)
print("リネームしました:" + image_path + " to " + to_path)
@yowasou
Copy link
Author

yowasou commented Jan 18, 2025

似た画像ファイルが近しくなるようにソートする

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment