imgtndr/imgtndr.py

145 lines
3.3 KiB
Python

#!/usr/bin/env python3
import sys
import os
import pygame
import argparse
import shutil
class Image:
def __init__(self, path, filename):
self.image = pygame.image.load(path + filename)
self.path = path
self.filename = filename
def fit(self, surface, screen_width, screen_height):
width = self.image.get_width()
height = self.image.get_height()
delta_w = width - screen_width
delta_h = height - screen_height
scale_w = 1
if delta_w > 0:
scale_w = screen_width / width
scale_h = 1
if delta_h > 0:
scale_h = screen_height / height
scale = min(scale_w, scale_h)
return pygame.transform.scale_by(self.image, scale)
def draw(self, surface):
image = self.fit(self.image, surface.get_width(), surface.get_height())
x = (surface.get_width() - image.get_width()) * 0.5
y = surface.get_height() * 0.5 - image.get_height() * 0.5
surface.blit(image, (x, y))
def load_image(index, images, screen):
screen.fill((0, 0, 0))
images[index].draw(screen)
pygame.display.update()
def next_image(current_image, skip, images, screen):
if len(images) < 1:
screen.fill((0, 0, 0))
pygame.display.update()
return
index = (current_image + skip) % len(images)
load_image(index, images, screen)
return index
def sanitize_path(path):
if not path.endswith('/'):
path += '/'
return path
def main():
pygame.init()
parser = argparse.ArgumentParser(prog='Image Tinder', description='Swipe through your images and press M if you have a match.')
parser.add_argument('image_path', help='The folder with your images')
parser.add_argument('target_path_match', help='The folder for your matched images')
parser.add_argument('target_path_remain', help='The folder for the remaining images')
args = parser.parse_args()
path = sanitize_path(args.image_path)
path_matches = sanitize_path(args.target_path_match)
path_remain = sanitize_path(args.target_path_remain)
if not os.path.isdir(args.target_path_match):
raise Exception('Match path %s does not exist!' % args.target_path_match)
if not os.path.isdir(args.target_path_remain):
raise Exception('Remain path %s does not exist!' % args.target_path_remain)
images = []
matches = []
for file in os.listdir(args.image_path):
filepath = path + file
if not filepath.lower().endswith('.jpg') and not filepath.lower().endswith('.jpeg'):
continue
images.append(Image(path, file))
screen = pygame.display.set_mode(pygame.display.list_modes()[0], pygame.FULLSCREEN)
current_image = 0
load_image(current_image, images, screen)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.KEYDOWN:
match event.key:
case pygame.K_ESCAPE:
return
case pygame.K_LEFT:
current_image = next_image(current_image, -1, images, screen)
case pygame.K_RIGHT:
current_image = next_image(current_image, 1, images, screen)
case pygame.K_m:
if len(images) > 0:
matches.append(images[current_image])
images.pop(current_image)
current_image = next_image(current_image, 0, images, screen)
case pygame.K_RETURN:
for m in matches:
shutil.move(m.path + m.filename, path_matches + m.filename)
for i in images:
shutil.move(i.path + i.filename, path_remain + i.filename)
return
if __name__ == '__main__':
main()