Page 1 of 1

Avatar Resizing Script

Posted: Thu Jan 31, 2013 1:49 am
by Scott
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.

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()
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.

Re: Avatar Resizing Script

Posted: Thu Jan 31, 2013 4:01 pm
by AnthonyOstrich
Does it work with animated gifs?

Re: Avatar Resizing Script

Posted: Thu Jan 31, 2013 4:28 pm
by Scott
AnthonyOstrich wrote:Does it work with animated gifs?
This edited code will allow resize .gifs but not animated .gifs. Animated .gifs will just have their first frame resized.

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

		#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()
I'm not sure I could do that with Python but I may look into it.

Re: Avatar Resizing Script

Posted: Thu Jan 31, 2013 5:30 pm
by AnthonyOstrich
Oh, well. It's still pretty cool.

On a semi-related note, you're new avatar is... interesting. Does it have some kind of meaning that's going over my head?

On a slightly less related note, I have a couple of miscellaneous projects I'd like to share. Do you think you could create a forum for me in the Game Development section? :D

Re: Avatar Resizing Script

Posted: Thu Jan 31, 2013 6:02 pm
by Scott
AnthonyOstrich wrote:On a semi-related note, you're new avatar is... interesting. Does it have some kind of meaning that's going over my head?
Just a sad man doing sad things

Image
AnthonyOstrich wrote:On a slightly less related note, I have a couple of miscellaneous projects I'd like to share. Do you think you could create a forum for me in the Game Development section? :D
Sure thing :u:

Re: Avatar Resizing Script

Posted: Thu Jan 31, 2013 6:17 pm
by AnthonyOstrich
:) Thanks. I'll post something there as soon as I get back to my house.