Pixelify
This commit is contained in:
Voussoir 2014-12-15 01:59:47 -08:00
parent d21199dafa
commit 22addf32dc
2 changed files with 27 additions and 11 deletions

View file

@ -1,6 +1,10 @@
Pixelify Pixelify
======== ========
Requires PIL, which has been adapted to Python3 under the name pillow
pip install pillow
Takes an image, or a folder full of images, and produces pixelated versions of each image according to an "objective". That is, how the image would look if it was forced to fit into an *objective x objective* frame. But, the actual output file will be the same dimensions as the original, simply pixelated. Takes an image, or a folder full of images, and produces pixelated versions of each image according to an "objective". That is, how the image would look if it was forced to fit into an *objective x objective* frame. But, the actual output file will be the same dimensions as the original, simply pixelated.
Examples: Examples:

View file

@ -8,9 +8,10 @@ def boot():
objectives = input("Pixel Objective\n> ") objectives = input("Pixel Objective\n> ")
objectives = objectives.replace(' ', '') objectives = objectives.replace(' ', '')
objectives = [int(x) for x in objectives.split(',')] objectives = [int(x) for x in objectives.split(',')]
pixelify(path, objectives) outpath = input("Path to output (Blank for standard)\n> ")
pixelify(path, objectives, outpath=outpath)
def pixelify(path, objectives=[32], subfolder="pixel"): def pixelify(path, objectives=[32], subfolder="pixel", outpath=""):
if '.' in path: if '.' in path:
name = path.split('/')[-1] name = path.split('/')[-1]
path = '/'.join(path.split('/')[:-1]) path = '/'.join(path.split('/')[:-1])
@ -20,6 +21,17 @@ def pixelify(path, objectives=[32], subfolder="pixel"):
if path[-1] in ['/', '\\']: if path[-1] in ['/', '\\']:
path = path[:-1] path = path[:-1]
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)
done = False done = False
while not done: while not done:
done = True done = True
@ -32,13 +44,12 @@ def pixelify(path, objectives=[32], subfolder="pixel"):
print('Unlisted "%s": not .jpg or .png' % name) print('Unlisted "%s": not .jpg or .png' % name)
break break
newdir = path +'/' + subfolder + '/' if not os.path.exists(outpath):
if not os.path.exists(newdir): print('Creating directory: ' + outpath)
print('Creating directory: ' + newdir) os.makedirs(outpath)
os.makedirs(newdir)
for name in images: for name in images:
filepath = path + '/' + name filepath = path + name
image = Image.open(filepath) image = Image.open(filepath)
for objective in objectives: for objective in objectives:
@ -52,9 +63,10 @@ def pixelify(path, objectives=[32], subfolder="pixel"):
nimage = nimage.resize((image_width, image_height), 0) nimage = nimage.resize((image_width, image_height), 0)
parts = name.split('.') parts = name.split('.')
newpath = newdir + parts[0] + '_' + str(objective) + '.' + parts[1] newpath = outpath + parts[0] + '_' + str(objective) + '.' + parts[1]
nimage.save(newpath) nimage.save(newpath)
if __name__ == "__main__":
while True:
boot() boot()
print('\n')