analitics

Pages

Showing posts with label resize. Show all posts
Showing posts with label resize. Show all posts

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, January 30, 2010

How to resize images .

Sometimes it is necessary to resize the images. The PIL module is used for image processing.The glob module takes a wildcard and returns the full path of all files and directories matching the wildcard.
Here are two scripts that I made.
The first is a simple example using a resize after some dimensions.
In this case we used size 300x300.

from PIL import Image
import glob, os
size_file = 300,300
for f in glob.glob("*.png"):
file, ext = os.path.splitext(f)
img = Image.open(f)
img.thumbnail(size_file, Image.ANTIALIAS)
img.save("thumb_" + file, "JPEG")

In the second case I tried to do a resize with proportion preservation.

import glob
import PIL
from PIL import Image
for f in glob.glob("*.jpg"):
img = Image.open(f)
dim_percent=(100/float(img.size[0]))
dim_size=int((float(img.size[1])*float(dim_percent)))
img = img.resize((100,dim_size),PIL.Image.ANTIALIAS)
if f[0:2] != "trumb_":
img.save("thumb_" + f, "JPEG")

In both cases we use a renaming of files by adding the name of "thumb_".
Ambele scripturi pot fi modificate asa cum vreti.
Aceste scripturi demonstreaza cum sa folosim celor doua module "PIL" si "globe".