Python 進階課程

講者:王譽錚

大綱

  • 套件
  • QR code
  • 圖片檔
  • 圖片轉文字

套件

安裝pip

cmd(命令提示字元)

在搜尋或是按下開始鍵 + R,輸入cmd,按下enter

pip

可對python的套件進行查詢、下載、安裝、卸載

pip list

確認電腦的pip資訊

pip --version

確認電腦已安裝的套件

pip安裝與卸載

pip uninstall 套件名

安裝

pip install 套件名

卸載

import pycon
from pycon import xmath

import

from

QR code

原理

二維碼

漢信碼

Data Matrix

QR code

Aztec

ShotCode

QuickMark

QR code的組成

1等於黑色方塊

0等於白色方塊

定位標記

校正圖塊

容錯率越高,QR code的圖形越大,因此常見的容錯率為15%

QR code的糾錯功能

QR code轉換器實作

套件安裝

pip install qrcode

打開命令提示字元(cmd),安裝 QR code的套件

import qrcode

在第一行引入QR code的套件

text = input("輸入文字或URL:")
print(text)

設一個變數存放使用者想要轉換成QRcode的文字或網址

img = qrcode.make(text)

建立變數來存放轉換好的結果

img.save("檔名.jpg")
img.show()

存檔並展示

完整程式碼

import qrcode
text = input("輸入文字或URL:")
img = qrcode.make(text)
img.save("test.png")
img.show()   

圖片檔

圖片檔的資訊

尺寸:275 x 183

 (201)

 (218)

 (248)

RGB編碼

RGB如何存在電腦

RGB  (201, 218, 248)

11001001

11011010

11111000

每一個數字佔1Bit的的儲存容量

RGBA編碼

RGBA如何存在電腦

RGBA  (201,  218,  248, 128)

11001001

11011010

11111000

每一個數字佔1Bit的的儲存容量

10000000

圖片轉文字

原理

(

轉成黑白圖像

依照黑或白轉成文字

(

語法

try-except

try-except if-else
如果try的程式區塊沒有錯誤,就繼續執行
 
如果符合if的條件,就執行if的程式區塊
否則執行except的程式區塊
 
否則執行else​的程式區塊
a = 1
try:
    print(a)
except:
    print("查無此物")
a = 1
try:
    print(b)
except:
    print("查無此物")

試一下

檔案讀取

file = open("檔名", mode="w", encoding="編碼")
file.write("要寫入的文字")
file.close()

檔案寫入

namestr = input("請輸入檔名") + ".txt"
txt = open(namestr, 'w')

txt.write('嘿大家好!')

txt.close()
print('保存完成')

試一下

end = " "

for i in range(0, 3):
    print("我是print")

我是print
我是print
我是print

for i in range(0, 3):
    print("我是print", end=" ")

我是print  我是print  我是print

for i in range(0, 3):
    print("我是print", end="\")

我是print\我是print\我是print

print("床前明月光", end = ",")
print("疑似地上霜", end = "。")

試一下

函式

def total(n):
    s = 0
    for i in range(1, n + 1):
        s = s + i
    return s

函式名叫做 total

需要給他一個參數

最後會回傳 s 給呼叫他的人

convert函式

convert函式可轉換成以下幾種顏色編碼

  • 1(黑白 1 bit)
  • L(灰階 8 bits)
  • P(8位彩色圖像 8 bits
  • CMYK(4 x 8 bits)
  • RGB ( 3 x 8 bits)
  • RGBA (4 x 8 bits)

複習一下!    split函式

"要被分割的字串" . split("以甚麼為分割點")

fox.png

img = fox.png
img.split(".")

fox

png

圖片轉文字實作

1.安裝PIL套件

pip install PIL
from PIL import Image

2.引入PIL套件裡的Image

name = input('請輸入圖片檔案名稱(圖片需置於同一層資料夾): ')
try:
    image_to_txt(name)
except:
    print('發生錯誤')
print('程式結束')

3.讓使用者可以輸入要轉換的圖片檔

def image_to_txt(imgName):
    # 打開圖片
    print("開啟圖片[{}]".format(imgName))
    try:
        img = Image.open(imgName)
    except:
        print("開啟圖片[{}]時出現錯誤".format(imgName))
        
    print("圖片資訊: 大小為{}x{}, 格式為{}, 顏色編碼為{}".format(img.size[0], img.size[1], img.format, img.mode))

4.確認圖片是否有開啟

width = img.size[0]
height = img.size[1]
zoom = 0
if width >= height:
    maxsize = width
else:
	maxsize = height
if maxsize >= 100:
    zoom = maxsize / 100
    width = int(width / zoom * 2)
    height = int(height / zoom)
    img = img.resize((width, height))
    print("圖片長或寬大於150,已縮小為{}x{}".format(img.size[0], img.size[1]))
img.save("檔名")

5.縮小圖片

img = img.convert('1')

6.將圖片轉換成黑白

namestr = imgName.split('.')[0] + '.txt'
txt = open(namestr, 'w')

txt.write('嘿大家好!')

txt.close()
print('保存完成')

7.開啟文字檔

index = 0  
print('開始進行圖片轉txt程序')
for y in range(height):
    index += 1
    print('#', end='')
    if index >= 60:
        index = 0
        print()

    for x in range(width):
        pixel = img.getpixel((x, y))
        if pixel != 0:
            txt.write('_')
        else:
            txt.write('@')
    txt.write('\n')
print('\n完成轉換程序')
print('檔案儲存為[{}]'.format(namestr))

8.將黑白圖片轉換成文字

完整程式碼

from PIL import Image

def image_to_txt(imgName):

    print("開啟圖片[{}]".format(imgName))
	try:
		img = Image.open(imgName)
    except:
		print("開啟圖片[{}]時出現錯誤".format(imgName))

    print("圖片資訊: 大小為{}x{}, 格式為{}, 顏色編碼為{}".format(img.size[0], img.size[1], img.format, img.mode))

    width = img.size[0]
    height = img.size[1]

    zoom = 0
    if width >= height:
        maxsize = width
    else:
        maxsize = height
    if maxsize >= 100:
        zoom = maxsize / 100
        
        width = int(width / zoom * 2)
        height = int(height / zoom)
        img = img.resize((width, height))
        print("圖片長或寬大於150,已縮小為{}x{}".format(img.size[0], img.size[1]))
    
    img = img.convert('1')
    
    namestr = imgName.split('.')[0] + '.txt'
    txt = open(namestr, 'w')
    index = 0  
    print('開始進行圖片轉txt程序')
    for y in range(height):

        index += 1
        print('#', end='')
        if index >= 60:
            index = 0
            print()
        
        for x in range(width):
            pixel = img.getpixel((x, y))
            if pixel != 0:
                txt.write('_')
            else:
                txt.write('@')
        txt.write('\n')
    
    print('\n完成轉換程序')
    print('檔案儲存為[{}]'.format(namestr))
    txt.close()
    print('保存完成')

name = input('請輸入圖片檔案名稱(圖片需置於同一層資料夾): ')
try:
    image_to_txt(name)
except:
    print('發生錯誤')
print('程式結束')

Thank you for listening

©SIRLA

參考資料

deck

By arashi

deck

  • 159