Skip to content

Instantly share code, notes, and snippets.

@Julian-Nash
Last active April 28, 2018 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Julian-Nash/814f9511a4a617ccdfb822e4f701c342 to your computer and use it in GitHub Desktop.
Save Julian-Nash/814f9511a4a617ccdfb822e4f701c342 to your computer and use it in GitHub Desktop.
Python 3. Convert bulk images from any file type to PNG with progress bar and save in new directory
#!/bin/bash
arg=$1
red='\033[1;31m'
yellow='\033[1;33m'
cyan='\033[1;36m'
purple='\033[1;35m'
nl='\n'
nc='\033[0m'
build_env(){
printf '\n'
echo -e "${purple}Resize & rename script starting...${nc}"
cp ~/tools/resize_to_png/resize_to_png.py .
printf '\n'
echo -e "${purple}copying python script to current directory...${nc}"
printf '\n'
echo -e "${purple}Building temporary environment...${nc}"
pipenv --three
pipenv install Pillow progress
pipenv shell
printf '\n'
echo -e "${purple}Environment build complete. Run 'resize run' to continue${nc}"
printf '\n'
}
resize(){
python resize_to_png.py
echo -e "${purple}Operation complete, tearing down pipenv and removing temporary files...${nc}"
pipenv --rm
rm Pipfile Pipfile.lock resize_to_png.py
exit
}
help(){
echo -e "${cyan}--------------------------------------------------------------------------${nc}"
echo -e "${cyan}| resize converts .jpg and .tif files to .png and resizes to 1920 x 1080 |${nc}"
echo -e "${cyan}--------------------------------------------------------------------------${nc}"
printf '\n'
echo -e "Commands:"
printf '\n'
echo -e "${red}build${nc} Builds a temporary local environment and installs dependencies"
echo -e "${yellow}run${nc} Executes the script and removes the temporary environment"
}
if [ $# -eq 0 ]; then
help
elif [ $arg == "build" ]; then
build_env
elif [ $arg == "run" ]; then
resize
else
echo -e "${red}Unknown argument: ${arg}${nc}"
fi
import os, sys
from PIL import Image
from progress.bar import Bar
width = 1080
height = 800
root = os.getcwd()
save_dir_name = "edits"
save_dir = root + "/" + save_dir_name
size = (width, height)
img_types = (".JPG", ".TIF")
images = [f for f in os.listdir(os.curdir) if f.endswith(img_types)]
if len(images) == 0:
print("No images found in this directory")
exit()
else:
bar = Bar('Processing', max=len(images))
print("Processing {} images".format(len(images)))
os.mkdir(save_dir)
for image in images:
(name, extension) = image.split(".")
im = Image.open(image)
im.thumbnail(size, Image.ANTIALIAS)
os.chdir(save_dir)
im.save(name + ".png")
os.chdir(root)
bar.next()
bar.finish()
print("Job finished. Processed {} images".format(len(images)))
@Julian-Nash
Copy link
Author

Julian-Nash commented Apr 28, 2018

Add resize_to_png.py in ~/tools/resize_to_png
Add resize.sh in ~/tools/resize_to_png

Add alias resize="~/tools/resize_to_png/resize.sh" in ~/.bashrc

Execute resize build followed by resize run in the directory containing the images you want to resize

Script will create temp pipenv and install dependencies -> create a new directory called edits -> resize images and convert to .png -> removes pipenv

run exit to leave the pipenv shell after the script has completed

Not perfect but gets the job done

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