Created
May 27, 2018 04:36
-
-
Save texasdave2/fe26e701d742533f79f94de93a3339e3 to your computer and use it in GitHub Desktop.
Thumbnail creator
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Sat May 26 09:10:40 2018 | |
@author: David O'Dell | |
""" | |
# Simple script that resizes all images in a directory to 100 x 100 thumbnails" | |
# script needs to be placed inside target directory | |
# import libraries | |
import cv2 | |
import sys | |
import glob | |
# to make this more robust and find other image file types one would want to | |
# iterate through a list of common graphics extensions | |
def get_file_list(): | |
extension_list = ["*.jpg", "*.jpeg", "*.gif", "*.bmp", "*.png", "*.tiff", "*.tif"] | |
file_list = [] | |
for i in extension_list: | |
file_list.extend(glob.glob(i)) | |
get_input(file_list) # send the file list to the function that gets user input | |
# Since the directory could be filled with thousands of images, we should probably | |
# ask the user how they want to proceed with the job | |
def get_input(file_list): | |
user_in = input("List files - L Convert files - C Quit - any other key: ") | |
if user_in == "L": | |
print("\r\n" + str(len(file_list)) + " files found that match commmon graphic extensions are:\r\n") | |
for file in file_list: | |
print(file) | |
get_input(file_list) | |
elif user_in == "C": | |
convert(file_list) | |
print("\r\n" + str(len(file_list)) + " files successfully converted.") | |
sys.exit() | |
else: | |
print("Bye!") | |
sys.exit() | |
# define a function that can resize an image to 100 x 100 then write to a new filename | |
def convert(file_list): | |
# cv2.imread can read in different modes, 0 grey, 1 color, -1 alpha transparency | |
for i in file_list: | |
file_name = str(i) | |
img = cv2.imread(file_name, 1) | |
resized_image = cv2.resize(img, (100, 100)) | |
new_file_name = "RESIZED_" + file_name | |
cv2.imwrite(new_file_name, resized_image) | |
print(new_file_name) | |
# call the initial function | |
get_file_list() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment