else/Pixelify/pixelify.py

91 lines
2.2 KiB
Python
Raw Normal View History

2014-12-15 09:23:15 +00:00
from PIL import Image
import os
import sys
2014-12-16 09:23:31 +00:00
import urllib.request
2014-12-15 09:23:15 +00:00
def boot():
path = input("Path to image or directory\n> ")
if path == '':
path = os.getcwd()
objectives = input("Pixel Objective\n> ")
objectives = objectives.replace(' ', '')
objectives = [int(x) for x in objectives.split(',')]
2014-12-15 09:59:47 +00:00
outpath = input("Path to output (Blank for standard)\n> ")
start(path, objectives, outpath=outpath)
2014-12-15 09:23:15 +00:00
def start(path, objectives=[32], subfolder="pixel", outpath=""):
2014-12-15 09:23:15 +00:00
if '.' in path:
2014-12-15 11:02:01 +00:00
path = path.replace('\\', '/')
2014-12-15 09:23:15 +00:00
name = path.split('/')[-1]
path = '/'.join(path.split('/')[:-1])
images = [name]
else:
images = os.listdir(path)
if path[-1] in ['/', '\\']:
path = path[:-1]
2014-12-15 11:02:01 +00:00
print(path)
2014-12-15 09:59:47 +00:00
if outpath == "":
outpath = path + '/' + subfolder + '/'
elif ':' not in outpath:
outpath = path + '/' + outpath + '/'
if outpath[-1] not in ['/', '\\']:
outpath += '/'
path += '/'
print('from:', path)
print(' to:',outpath)
2014-12-15 09:23:15 +00:00
done = False
while not done:
done = True
for name in images:
ext = name.lower()[-4:]
if ext != '.png' and ext != '.jpg':
done = False
images.remove(name)
if name != subfolder:
print('Unlisted "%s": not .jpg or .png' % name)
break
2016-05-10 08:00:29 +00:00
os.makedirs(outpath, exist_ok=True)
2014-12-15 09:23:15 +00:00
for name in images:
2014-12-15 09:59:47 +00:00
filepath = path + name
2014-12-15 09:23:15 +00:00
image = Image.open(filepath)
for objective in objectives:
nimage = pixelify(image, name, objective)
2014-12-15 09:23:15 +00:00
parts = name.split('.')
2014-12-15 09:59:47 +00:00
newpath = outpath + parts[0] + '_' + str(objective) + '.' + parts[1]
2014-12-16 09:23:31 +00:00
nimage.save(newpath, quality=100)
def pixelify(image, name, objective):
print("Working: " + name, objective)
image = image.copy()
image_width = image.size[0]
image_height = image.size[1]
ratio = objective / max([image_width, image_height])
new_width = image_width * ratio
new_height = image_height * ratio
image = image.resize((round(new_width), round(new_height)), 1)
image = image.resize((image_width, image_height), 0)
return image
2014-12-15 09:23:15 +00:00
2014-12-15 09:59:47 +00:00
if __name__ == "__main__":
if len(sys.argv) > 1:
print(sys.argv)
path = sys.argv[1]
objectives = [32]
outpath = ""
try:
objectives = [int(x) for x in sys.argv[2].split(',')]
outpath = sys.argv[3]
except IndexError:
pass
start(path, objectives, outpath=outpath)
else:
while True:
boot()
print('\n')