-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·69 lines (53 loc) · 1.86 KB
/
example.py
File metadata and controls
executable file
·69 lines (53 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
"""
範例:使用 Fast JPEG Decoder 解碼 JPEG 圖片
"""
import sys
import os
import numpy as np
# Add src/python to path for development
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src', 'python'))
try:
import fast_jpeg_decoder as fjd
except ImportError:
print("Error: fast_jpeg_decoder 模組未安裝")
print("請先執行: make develop")
sys.exit(1)
def main():
"""主函數"""
if len(sys.argv) < 2:
print("用法: python example.py <jpeg_file>")
print("\n範例:")
print(" python example.py photo.jpg")
return
filename = sys.argv[1]
print(f"正在解碼: {filename}")
try:
# 方法 1: 使用簡便函數
image = fjd.load(filename)
print(f"✓ 解碼成功!")
print(f" 圖片尺寸: {image.shape[1]} x {image.shape[0]}")
print(f" 通道數: {image.shape[2]}")
print(f" 資料類型: {image.dtype}")
print(f" 數值範圍: [{image.min()}, {image.max()}]")
# 方法 2: 使用 Decoder 類別
print("\n使用 Decoder 類別:")
decoder = fjd.JPEGDecoder()
success = decoder.decode_file(filename)
if success:
print(f"✓ 解碼成功!")
print(f" 寬度: {decoder.width}")
print(f" 高度: {decoder.height}")
print(f" 通道: {decoder.channels}")
image2 = decoder.get_image_data()
# 驗證兩種方法得到相同結果
if np.array_equal(image, image2):
print("\n✓ 兩種方法結果一致")
else:
print("\n✗ 兩種方法結果不同")
except Exception as e:
print(f"✗ 解碼失敗: {e}")
return 1
return 0
if __name__ == '__main__':
sys.exit(main())