#################################### #################################### ## 電腦繪圖報告-Project4 ## ## 2013/12/18 ## ## 組別:第七組 ## ## 組員: ## ## 數學三 00131042 蘇育萱 ## ## 數學三 00131052 方思婷 ## #################################### #################################### from PIL import Image from PIL import ImageDraw import random #TODO1 write function colorPixel def colorPixel(position,color,myImage): x=int(position[0]) y=int(position[1]) myImage.putpixel((x,y),color) #TODO2 write function flipAxis def flipAxis(position,bounds): x=int(position[0]) y=int(position[1]) hight,width=image.size width=bounds[0] hight=bounds[1] return (x,height-1-y) #TODO3 write function checkBounds def checkBounds(position,bounds): x=int(position[0]) y=int(position[1]) hight,width=image.size if x>width : return (width,y) if x<=width : return (0,y) if y>hight : return (x,hight) if y<=hight : return (x,0) #TODO4 write drawLine function def drawLine(position,direction,color,myImage): step=10 width, height = myImage.size x1=width//2 y1=height//2 x2=width//2 y2=height//2 directions = list(direction) while(directions): direc = directions.pop(0) if direc=='N': y2=y1-step elif direc=='S': y2=y1+step elif direc=='E': x2=x1+step elif direc=='W': x2=x1-step drw = ImageDraw.Draw(myImage) drw.line((x1,y1,x2,y2),color) x1=x2 y1=y2 #TODO5 write main function def main(): list=[] #1 infilename=input("Please input file to open: ") #2 colorname=input("Please input color(red,green,blue,or random): ") #3 if colorname=='red' : color=(255,0,0) elif colorname=='green' : color=(0,255,0) elif colorname=='blue' : color=(0,0,255) elif colorname=='random': color=(random.randint(0,255),random.randint(0,255),random.randint(0,255)) else :color=(0,0,0) #4.5.7 infile = open(infilename,'r') count=1 for line in infile: if (count%3==1) : direction=line.strip('\n') if (count%3==2) : width=line.strip('\n') if (count%3==0) : hight=line.strip('\n') myImage = Image.new('RGB', (int(width),int(hight)), (255, 255, 255)) position = [0,0] drawLine(position,direction,color,myImage) a='dragon_image_'+infilename[10]+'.jpeg' print('save') myImage.save(a) count = count+1 infile.close input("Thank you come again.") myImage.show() main()