Trying to transform my small code to OOP :) need help...

This is the place for queries that don't fit in any of the other categories.

Trying to transform my small code to OOP :) need help...

Postby Valda » Sun Aug 03, 2014 10:57 am

Hey guys, I'm trying to transform this code to OOP, just for practice purposes...

This is the code

Code: Select all
Dir = 'C:\\Python\\'

from PIL import Image, ImageDraw
Img = Image.open(Dir+"len.jpg")
a,b = Img.size[0],Img.size[1]
Drawer = ImageDraw.Draw(Img)
Drawer.ellipse((a/2-50,b/2-50,a/2+50,b/2+50),'red')
Drawer.rectangle((a*0.25-20,b*0.25-20,a*0.25+20,b*0.25+20),'silver')
Drawer.line((a*0.75-100,b*0.75+20,a*0.75+50,b*0.75+75,a*0.75+70,b*0.75-60,a*0.75,b*0.75-30,a*0.75-30,b*0.75+15, a*0.75-100,b*0.75+20),'blue', width=5)
Img.save(Dir+'task.png')


This is my attempt:

Code: Select all
class DrawOnImage

Dir = 'C:\\Python\\'

def pic (Lena)
from PIL import Image, ImageDraw
Img = Image.open(Dir+lena)

def draw()
Drawer = ImageDraw.Draw(Img)
a = Img.size[0]
b = Img.size[1]
Drawer.ellipse((a/2-50,b/2-50,a/2+50,b/2+50),'red')
Drawer.rectangle((a*0.25-20,b*0.25-20,a*0.25+20,b*0.25+20),'silver')
Drawer.line((a*0.75-100,b*0.75+20,a*0.75+50,b*0.75+75,a*0.75+70,b*0.75-60,a*0.75,b*0.75-30,a*0.75-30,b*0.75+15, a*0.75-100,b*0.75+20),'blue', width=5)

def imgsave(save)
Img.save(Dir+save)

Lena = DrawOnImage('len.jpg')
Lena.draw()
Lena.imgsave('task.jpg')


But this time it doesn't save the new image.
Can anyone let me know what am I doing wrong?
Last edited by Yoriz on Sun Aug 03, 2014 2:28 pm, edited 1 time in total.
Reason: First post lock.
Valda
 
Posts: 2
Joined: Sun Aug 03, 2014 10:52 am

Re: Trying to transform my small code to OOP :) need help...

Postby Yoriz » Sun Aug 03, 2014 2:34 pm

Welcome to the forum, please read the new users read this link in my signature.

I take it you haven't done any tutorials on how to use classes?
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 5 guests