1. 程式人生 > 其它 >20212305 2022-2022-2 《PYTHON程式設計》實驗報告

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並不透徹,經過一個寒假,更是十分生疏。經過這次實驗,找回了一點感覺,也發現原來上學期自學的內容十分淺薄。