Avatar Resizing Script
Posted: Thu Jan 31, 2013 1:49 am
I was uploading a new avatar and was frustrated that I had to manually resize my image to fit the requirements. So instead of spending 1 minute in paint I spent 15 writing this script.
All you need is Python 2.7 to use it. Just run "python PYTHON_PATHWAY.py IMAGE_PATHWAY.EXT NEW_WIDTH NEW_HEIGHT" and the image will be saved and resized. Nobody may use it but I thought I'd share it anyway.
EDIT:
Newest version can also be used to find the current dimensions of an image. Just run the above command but use 0 for both height and width.
Code: Select all
from sys import argv
from Tkinter import *
from PIL import Image, ImageTk, ImageFilter
class ResizeAvatar(object):
def __init__(self):
self.root = Tk()
self.root.title("Avatar Resizer")
#unpacking command line arguments
filename = argv[1]
new_width = int(argv[2])
new_height = int(argv[3])
try:
image = Image.open(str(filename))
except:
return None
if new_width == 0 or new_height == 0:
print "Original: " + str((image.size[0], image.size[1]))
else:
#resizes image to an abritrary number
#so that it will fit to most screens
#while retaining its aspect ratio
print "Original: " + str((image.size[0], image.size[1]))
print "New Width: " + str(new_width) + "\tNew Height: " + str(new_height)
if image.size[0] > new_width or image.size[1] > new_height:
width = image.size[0]
height = image.size[1]
while width > new_width or height > new_height:
width = int(width*0.99)
height = int(height*0.99)
resized = image.resize((width, height))
new_filename = ""
ext = ""
for i in range(len(filename)):
if filename[i] != '.':
new_filename += filename[i]
ext = filename[i+2]
else:
new_filename += "(2)"
break
if ext == 'g':
new_filename += ".gif"
else:
new_filename += ".jpg"
saved = resized.save(new_filename)
print "Resized: " + str((resized.size[0], resized.size[1]))
resized = ImageTk.PhotoImage(resized)
label = Label(self.root, image=resized)
label.pack()
self.root.mainloop()
ResizeAvatar()
EDIT:
Newest version can also be used to find the current dimensions of an image. Just run the above command but use 0 for both height and width.