《Python程序设计》习题与答案

glShadeModel(GL_SMOOTH) glEnable(GL_POINT_SMOOTH) glEnable(GL_LINE_SMOOTH)

glEnable(GL_POLYGON_SMOOTH) glMatrixMode(GL_PROJECTION)

glHint(GL_POINT_SMOOTH_HINT,GL_NICEST) glHint(GL_LINE_SMOOTH_HINT,GL_NICEST)

glHint(GL_POLYGON_SMOOTH_HINT,GL_FASTEST) glLoadIdentity()

gluPerspective(45.0, float(width)/float(height), 0.1, 100.0) glMatrixMode(GL_MODELVIEW) def MainLoop(self): glutMainLoop()

if __name__ == '__main__': w = MyPyOpenGLTest() w.MainLoop()

15.2 编写程序,读取两幅大小一样的图片,然后将两幅图像的内容叠加到一幅图像,结果图像中每个像素值为原两幅图像对应位置像素值的平均值。

答:

from PIL import Image

im1 = Image.open('d:\\\\pic1.bmp') im2 = Image.open('d:\\\\pic2.bmp') size = im1.size

for i in range(size[0]): for j in range(size[1]):

color1 = im1.getpixel((i,j)) color2 = im2.getpixel((i,j)) r = (color1[0]+color2[0])//2 g = (color1[1]+color2[1])//2 b = (color1[2]+color2[2])//2 im1.putpixel((i,j),(r,g,b))

im1.save('d:\\\\pic3.bmp') im1.close() im2.close()

15.3 编写程序,读取一幅图像的内容,将其按象限分为4等份,然后1、3象限内容交换,2、4象限内容交换,生成一幅新图像。

答:

from PIL import Image

im = Image.open('d:\\\\pic1.bmp') im2 = Image.open('d:\\\\pic1.bmp') size = im.size

box1 = (0, size[1]/2, size[0]/2, size[1]) region1 = im.crop(box1)

box2 = (0, 0, size[0]/2, size[1]/2) region2 = im.crop(box2)

box3 = (size[0]/2, 0, size[0], size[1]/2) region3 = im.crop(box3)

box4 = (size[0]/2, size[1]/2, size[0], size[1]) region4 = im.crop(box4)

im2.paste(region1, box3) im2.paste(region3, box1) im2.paste(region2, box4) im2.paste(region4, box2)

im2.save('d:\\\\pic4.bmp') im.close() im2.close()

15.4 结合GUI编程知识,编写一个程序,创建一个窗口并在上面放置两个按钮,分别为“开始播放”和“暂停播放”,将本章15.3节中的音乐播放程序进行封装。

答:

import wx import os

import pygame import random import time import threading

class wxGUI(wx.App): def OnInit(self):

frame = wx.Frame(parent=None, title='MP3Player', size=(250,150), pos=(350,350)) panel = wx.Panel(frame, -1)

self.buttonOK = wx.Button(panel, -1, 'Play', pos=(30,60))

self.Bind(wx.EVT_BUTTON, self.OnButtonOK, self.buttonOK)

self.buttonOK.Enabled = True

self.buttonCancel = wx.Button(panel, -1, 'Stop', pos=(120,60))

self.Bind(wx.EVT_BUTTON, self.OnButtonCancel, self.buttonCancel) self.buttonCancel.Enabled = False

frame.Show() return True def OnExit(self): try:

self.playing = False

pygame.mixer.music.stop() finally: pass

def play(self):

folder = r'h:\\music'

musics = [folder+'\\\\'+music for music in os.listdir(folder) if music.endswith('.mp3')] total = len(musics) pygame.mixer.init() while self.playing:

if not pygame.mixer.music.get_busy(): nextMusic = random.choice(musics) pygame.mixer.music.load(nextMusic) pygame.mixer.music.play(1) print 'playing....',nextMusic else:

time.sleep(1)

def OnButtonOK(self, event): self.playing = True

# create a new thread to play music t = threading.Thread(target=self.play) t.start()

self.buttonOK.Enabled = False

self.buttonCancel.Enabled = True

def OnButtonCancel(self, event): self.playing = False

pygame.mixer.music.stop() self.buttonOK.Enabled = True self.buttonCancel.Enabled = False

app = wxGUI()

app.MainLoop()

15.5 运行本章15.4中的代码并查看运行结果。 答:略。

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4