"""
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))
Is a blog about python programming language. You can see my work with python programming language, tutorials and news.
Monday, April 30, 2012
Resize screenshot with PIL python module .
Saturday, April 7, 2012
When to use '__main__' ?
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'
Sunday, February 26, 2012
The wx module - tutorial 02 - class and button function
In this tutorial I will show how to use wx using one class example.
This tutorial is based on some code from a previous tutorial.
Let's see the source code:
import wx
class my_gui_form(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Example windows wx with Python',size=(300,150))
wx.StaticText(self, -1, 'User', (10, 20))
user = wx.TextCtrl(self, -1, '', (110, 20), (160, -1))
wx.StaticText(self, -1, 'Password', (10, 50))
passwd = wx.TextCtrl(self, -1, "", (110,50),(160, -1), style=wx.TE_PASSWORD)
conn = wx.Button(self, -1,'Connect',(10,80),(260, -1))
conn.Bind(wx.EVT_BUTTON, self.onButton)
def onButton(self, event):
button = event.GetEventObject()
print "The button's label is: " + button.GetLabel()
print "The button's name is: " + button.GetName()
if __name__ == "__main__":
app1 = wx.App(False)
frame = my_gui_form()
frame.Show()
app1.MainLoop()
First , I make a class named my_gui_form.
Also I add the self on each function from this class.
I make the onButton function to working with the button Connect.
I read the event of the button with event.GetEventObject.
The link with this function is make by:
conn.Bind(wx.EVT_BUTTON, self.onButton)
Source Code Optimization:
The -1 from is can replace with wx.ID_ANY.
Thursday, February 23, 2012
The wx module - tutorial 01
Install wx python module using root account.
# yum install wxPython.i686
The most simple example is show bellow.
$python
Python 2.7.2 (default, Oct 27 2011, 01:36:46)
[GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>> app1=wx.App()
>>> frame=wx.Frame(None,-1,'Example windows wx with Python')
>>> frame.Show()
True
>>> app1.MainLoop()
>>>
Let's see the result of this python script:
Let's take a look at the source code.
import wx
This line of code allows us to use wx python module.
app1=wx.App()
This line defines our application named app1.
A frame is a window whose size and position can be changed by the user.
Let's see:
frame=wx.Frame(None,-1,'Example windows wx with Python')
frame.Show()
Finally, processing and display function in main function.
app1.MainLoop()
Bellow, we can see a more complex example, where we find : labels , two editbox and one button.
The event-processing function button has not been implemented.
import wx
app1=wx.App()
frame=wx.Frame(None,-1,'Example windows wx with Python',size=(300,150))
wx.StaticText(frame, -1, 'User', (10, 20))
user = wx.TextCtrl(frame, -1, '', (110, 20), (160, -1))
wx.StaticText(frame, -1, 'Password', (10, 50))
passwd = wx.TextCtrl(frame, -1, "", (110,50),(160, -1), style=wx.TE_PASSWORD)
conn = wx.Button(frame, -1,'Connect',(10,80),(260, -1))
frame.Show()
app1.MainLoop()
Let's see the result of this python script:
I will illustrate how this working in a future tutorial.
Saturday, November 26, 2011
Simple socket client with python
This example was presented in a previous post named Simple socket server with python.
The program is simple, the algorithm uses connection and some data processing input from the console.
I used the same test method as for the program created in C + +.
The program breaks the connection when the client enter: end connection
Here's the source code:
import socket
import sys
HOST = 'your-IP'
PORT = 5001
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error, msg:
s = None
continue
try:
s.connect(sa)
except socket.error, msg:
s.close()
s = None
continue
break
if s is None:
print 'could not open socket'
sys.exit(1)
data=''
while data<>'end connection':
data=raw_input()
s.send(data)
data = s.recv(1024)
s.close()
print 'Received', repr(data)
Sunday, November 20, 2011
Simple socket server with python
I can not say that it really is a server that does not accept multiple connections.
The script worked stable sending manually entered text.
I not checked the stability for the large data streams.
It is also normal because as you see below is just one example.
import socket
HOST = 'your-IP'
PORT = 5001
s= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST,PORT))
s.listen(1)
conn,addr=s.accept()
print 'Conectat la =',addr
while 1:
data = conn.recv(1024)
if not data :
break
conn.send(data)
print data
conn.close()
The distance between me and the server was considerable.