analitics

Pages

Monday, April 30, 2012

Resize screenshot with PIL python module .

The script that I've created is made ​​to shrink images. Some screenshots are large and should be resized to be used later on the Internet. It is a simple example that uses PIL module. This script reads the image name that I want to resize and filename that will be saved image. I use python PIL functions how to create a new image.
"""
This python script read the name of image and will create a new image with the given width and height.

$ python imgresz.py 
filename input image:test.png
test.png
filename output image:test-out.jpg
->width:500
->height:400
"""
import os 
import sys
from PIL import Image 
from PIL import ImageDraw
filnaminp=raw_input("filename input image:")
filnamout=raw_input("filename output image:")
w=input("->width:")
h=input("->height:")
imgi=Image.open(str(filnaminp))
imgo=imgi.resize((w,h),Image.BILINEAR)
imgo.save(str(filnamout))

Saturday, April 7, 2012

When to use '__main__' ?

When your script is run it as a command to the Python interpreter: python your_script.py all of the code that is at indentation level 0 gets executed and functions and classes that are defined but none of their code gets ran. If will then read :
if __name__ == '__main__'
so it will execute the block standalone. In other words, when you use the __main__ this means the module is being run standalone
if __name__ == '__main__':
 print '... is being run by itself'
else:
 print '... is being run directly'