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
========
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.
Examples:

View file

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