Ascii
- 電腦裡只有數字
- string裡的其實也是數字
- Ascii 表
Ascii (cont)
- 65 : 'A'
- 97 : 'a'
- 48 : '0'
Method ( ord )
- 獲得一個字元的Ascii值
- e.g.
ord('A')
Method ( chr )
- 獲得一個數字所代表的字元
- e.g.
chr(65)
練習
- 輸出 1-255,所有數字的Ascii,並產生如下的表
string 延伸
- Format String
"a {} b"
.format("!=")
Format string
- '{:f}'.format(123.123)
- '{:s}'.format("QQAQQ")
- '{:d}'.format(17)
- '{:b}'.format(17)
- '{:o}'.format(17)
- '{:x}'.format(17)
Format string (cont)
'{:[char][location][num][k進位制]}'
- 代表以k進位顯示,如果不滿num位,會用char來padding,並且以location(置中、置左、置右)顯示。
- location
- e.g. '{:乂^11s}'.format("Rilak")
int的魔力
- int(string,positional notation)
- e.g.
int("101",2)
位元運算
- 簡單邏輯運算
- bitwise
- only for integer
And
& |
0 |
1 |
0 |
False |
False |
1 |
False |
True |
Or
| |
0 |
1 |
0 |
False |
True |
1 |
True |
True |
Xor
^ |
0 |
1 |
0 |
False |
True |
1 |
True |
False |
圖片格式簡介(png)
- 用PIL讀入後,轉成RGBA模式
- R(紅通道)、G(綠通道)、B(藍通道)、A(阿法通道)
- 都是0~255的數值
人類眼睛不好

差不多啦
- Least Significant Bit Encoding
- 把資料藏在倒數第一個bit(可以選擇要用幾個通道)
藏圖結果

獲得Bit Array (PNG)
| im = Image.open("test.png").convert('RGBA') |
| byte_data = np.array(list(im.getdata())) |
塞入圖片
| bit_array = "".join(['{0:08b}'.format(ord(c)) for c in data]) |
| |
| for i, x in enumerate(bit_array): |
| byte_data[i][0] = int(byte_data[i][0]) & ((1<<8) - 2) | int(x) |
| |
| QQ = np.array(byte_data).astype(np.uint8).reshape(250,250,4) |
存回圖片 (PNG)
| im = Image.fromarray(QQ,'RGBA') |
| im.save('gg.png') |
隱寫術 我藏我藏
{"metaMigratedAt":"2023-06-14T16:07:10.062Z","metaMigratedFrom":"YAML","title":"隱寫術","breaks":true,"slideOptions":"{\"theme\":\"solarized\",\"transition\":\"fade\"}","contributors":"[]"}