20212305 2022-2022-2 《PYTHON程序设计》实验报告



课程:《Python程序设计》
班级: 2123
姓名:杨贯宇
学号:20212305

实验教师:王志强

实验日期:2022年3月22日

实验内容:

  • 1.熟悉Python开发环境;

    2.练习Python运行、调试技能;

    3.编写程序,练习变量和类型、字符串、对象、缩进和注释等;

    4.掌握git技能

1.实验内容

  • (1)熟悉Python开发环境;

    (2)练习Python运行、调试技能;

    (3)编写程序,练习变量和类型、字符串、对象、缩进和注释等;

    (4)掌握git技能

2. 实验过程及结果

成功安装并运行pycharm,以后统一简称为pc。用Windows的cmd运行Python,成功输出Hello world

 

 

 

运用IDLE运行简单的代码

 

 

 从网络上找到一些有趣的python代码进行实验

复制代码
 1 import os
 2 import freetype
 3 import numpy as np
 4 from PIL import Image
 5 
 6 FONT_FILE = r'C:\Windows\Fonts\STXINGKA.TTF'
 7 BG_FILE = r'D:\temp\bg.png'
 8 
 9 
10 def text2image(word, font_file, size=128, color=(0, 0, 0)):
11     """使用指定字库将单个汉字转为图像
12 
13     word        - 单个汉字字符串
14     font_file   - 矢量字库文件名
15     size        - 字号,默认128
16     color       - 颜色,默认黑色
17     """
18 
19     face = freetype.Face(font_file)
20     face.set_char_size(size * size)
21 
22     face.load_char(word)
23     btm_obj = face.glyph.bitmap
24     w, h = btm_obj.width, btm_obj.rows
25     pixels = np.array(btm_obj.buffer, dtype=np.uint8).reshape(h, w)
26 
27     dx = int(face.glyph.metrics.horiBearingX / 64)
28     if dx > 0:
29         patch = np.zeros((pixels.shape[0], dx), dtype=np.uint8)
30         pixels = np.hstack((patch, pixels))
31 
32     r = np.ones(pixels.shape) * color[0] * 255
33     g = np.ones(pixels.shape) * color[1] * 255
34     b = np.ones(pixels.shape) * color[2] * 255
35     im = np.dstack((r, g, b, pixels)).astype(np.uint8)
36 
37     return Image.fromarray(im)
38 
39 
40 def write_couplets(text, horv='V', quality='L', out_file=None, bg=BG_FILE):
41     """写春联
42 
43     text        - 春联字符串
44     bg          - 背景图片路径
45     horv        - H-横排,V-竖排
46     quality     - 单字分辨率,H-640像素,L-320像素
47     out_file    - 输出文件名
48     """
49 
50     size, tsize = (320, 128) if quality == 'L' else (640, 180)
51     ow, oh = (size, size * len(text)) if horv == 'V' else (size * len(text), size)
52     im_out = Image.new('RGBA', (ow, oh), '#f0f0f0')
53     im_bg = Image.open(BG_FILE)
54     if size < 640:
55         im_bg = im_bg.resize((size, size))
56 
57     for i, w in enumerate(text):
58         im_w = text2image(w, FONT_FILE, size=tsize, color=(0, 0, 0))
59         w, h = im_w.size
60         dw, dh = (size - w) // 2, (size - h) // 2
61 
62         if horv == 'V':
63             im_out.paste(im_bg, (0, i * size))
64             im_out.paste(im_w, (dw, i * size + dh), mask=im_w)
65         else:
66             im_out.paste(im_bg, (i * size, 0))
67             im_out.paste(im_w, (i * size + dw, dh), mask=im_w)
68 
69     im_out.save('%s.png' % text)
70     os.startfile('%s.png' % text)
71 
72 
73 if __name__ == '__main__':
74     write_couplets('天降祥瑞洪福至', horv='V', quality='H')
75     write_couplets('地升好运富贵来', horv='V', quality='H')
76     write_couplets('人世平安', horv='H', quality='H')
复制代码

运行结果

 

 

 在pc上编写了简单的代码,练习变量和类型、字符串、对象、缩进和注释

复制代码
 1 message = 'hello world!'  # 创建变量并赋值
 2 print(message)  # 打印变量
 3 print(message.title())  # 打印变量并首字母大写
 4 print(message.upper())  # 打印变量并全部大写
 5 
 6 message1 = message.upper()  # 赋值新变量message1
 7 print(message1.lower())  # 打印变量并全部小写
 8 
 9 first_name = 'guanyu'
10 last_name = 'yang'
11 print(f"Hello everyone, my name is {last_name.title()} {first_name.title()}.You can call me {last_name.title()}.")
复制代码

运行结果

 

 

3. 实验过程中遇到的问题和解决过程

  • 问题1:git的使用
  • 问题1解决方案:查找云班课的资料,请教同学
  • 问题2:pc缺少库,导致从网上找到的代码无法运行
  • 问题2解决方案:在知乎和博客上请教众位大神,看到他们的回复和帖子,学到了很多

其他(感悟、思考等)

上学期自学的python并不透彻,经过一个寒假,更是十分生疏。经过这次实验,找回了一点感觉,也发现原来上学期自学的内容十分浅薄。

posted @ 2022-03-22 21:24  20212305杨贯宇  阅读(56)  评论(0编辑  收藏  举报