Skip to content

Instantly share code, notes, and snippets.

@joetsoi
Last active August 11, 2017 01:56
Show Gist options
  • Save joetsoi/f58b152e0a16efe4e965873db3b4e752 to your computer and use it in GitHub Desktop.
Save joetsoi/f58b152e0a16efe4e965873db3b4e752 to your computer and use it in GitHub Desktop.
imgsorter
import os
import sys
import glob
import pygame
scale_factor = 4
def load_image(file_name):
return pygame.image.load(file_name)
def display_image(image):
surface = load_image(image)
image_rect = surface.get_rect()
surface = pygame.transform.scale(
surface,
(image_rect.width * scale_factor,
image_rect.height * scale_factor),
)
image_rect = surface.get_rect()
screen.fill((0, 0, 0))
screen.blit(surface, image_rect)
pygame.display.flip()
directory_map = {
pygame.K_1: '1',
pygame.K_2: '2',
pygame.K_3: '3',
}
def move_file(file_path, image, output_directory):
original_path = os.path.join(file_path, image)
new_path = os.path.join(
file_path,
output_directory,
image
)
print('{} -> {}'.format(original_path, new_path))
os.rename(original_path, new_path)
def wait_and_handle_image(file_path, image, prev_image, prev_output_directory):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_u and prev_image:
# undo
original_path = os.path.join(file_path,
prev_output_directory,
prev_image)
new_path = os.path.join(file_path, prev_image)
print('{} -> {}'.format(original_path, new_path))
os.rename(original_path, new_path)
return None, None, -1
try:
output_directory = directory_map[event.key]
move_file(file_path, image, output_directory)
return image, output_directory, 1
except KeyError:
pass
else:
continue
break
pygame.time.wait(10)
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((320 * scale_factor, 320 * scale_factor))
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)))
print(file_path)
images = glob.glob('*.tif')
i = 0
prev_image = None
prev_output_directory = None
while i < len(images):
image = images[i]
display_image(image)
prev_image, prev_output_directory, count = wait_and_handle_image(
file_path,
image,
prev_image,
prev_output_directory
)
i += count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment