云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 《Python程序设计》习题与答案

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

  • 62 次阅读
  • 3 次下载
  • 2025/5/3 23:14:57

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中的代码并查看运行结果。 答:略。

搜索更多关于: 《Python程序设计》习题与答案 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

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, f

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com