最近實驗室在做 Construst,要跑很多次 DNA 電泳,偏偏照膠系統故障,只能拍好後再用手機翻拍膠圖,我們的小型藍光照膠器拍出來不明顯而且還是橘色的,看起來很討厭,本來打算用 python 寫個轉灰階腳本,因為時在需要用太多次了,乾脆直接寫成一個小工具比較實在。
環境
1
2
3
python 3.7.3
pip 19.2
Pillow 6.1.0
首先先來圖片處理轉灰階的核心部分,呼叫 PIL 函式即可很輕易達成目標
先 import PIL 第三方函式庫,把開啟的圖片檔分割成路徑與檔名,以便後面存檔,用 PIL.Image.open
載入圖片
把輸出檔名前面冠上一個 “L_” 做為區別,呼叫 im.convert('L')
轉成灰階,若要二元化則是 im.convert('1')
再來就存檔 L.save(SaveImagePath)
,核心這樣就完成了,很簡單。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from PIL import ImageFilter
import PIL.Image
OpenImagePath = ""
SaveImagePath = ""
head , tail = os . path . split ( OpenImagePath )
SaveImagePath = head + " \\ L_" + tail
im = PIL . Image . open ( OpenImagePath )
L = im . convert ( 'L' )
if ( previewOption ):
L . show ()
L . save ( SaveImagePath )
print ( "Save in " + SaveImagePath )
print ( "OK!" )
exit ( 0 )
接下來我把要轉換的圖片放在命令列參數,加上一些檢查機制,並且在轉換圖片的部分加上 try 敘述,捕捉例外,以免找不到檔案或是其他轉換失敗導致程式崩潰
到這一步已經完成命令列版的轉換小工具了。
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
from PIL import ImageFilter
import PIL.Image
import sys , os
def showHelp ():
print ( "Usage:" )
print ( "python ConvertGelImage.py [OpenImagePath] (SaveImagePath)" )
previewOption = False
if ( len ( sys . argv ) != 2 and len ( sys . argv ) != 3 ):
print ( "Arguments error!" )
showHelp ()
exit ( 2 )
if ( sys . argv [ 1 ] == "help" or sys . argv [ 1 ] == "Help" ):
showHelp ()
OpenImagePath = ""
OpenImagePath = sys . argv [ 1 ]
print ( OpenImagePath )
SaveImagePath = ""
head , tail = os . path . split ( OpenImagePath )
if ( len ( sys . argv ) == 3 ):
SaveImagePath = sys . argv [ 2 ]
else :
SaveImagePath = head + " \\ L_" + tail
try :
im = PIL . Image . open ( OpenImagePath )
L = im . convert ( 'L' )
if ( previewOption ):
L . show ()
L . save ( SaveImagePath )
except Exception as e :
print ( str ( e ))
exit ( 1 )
print ( "Save in " + SaveImagePath )
print ( "OK!" )
exit ( 0 )
本來只打算做到命令列,結果等 PCR 反應太無聊,就把他加上 tk 的檔案開啟對話窗,這樣就可以用 GUI 選擇圖片檔案
我依然保留他 CLI 的功能,畢竟 CLI 有時還是比較好用,只是變成如果路徑參數輸入 “gui” 則會開啟檔案視窗。
完整程式碼:
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
from PIL import ImageFilter
import PIL.Image
import sys , os
from tkinter import filedialog
from tkinter import *
def showHelp ():
print ( "Usage:" )
print ( "python ConvertGelImage.py [OpenImagePath] (SaveImagePath)" )
print ( "[GUI Mode]: python ConvertGelImage.py gui" )
previewOption = False
if ( len ( sys . argv ) != 2 and len ( sys . argv ) != 3 ):
print ( "Arguments error!" )
showHelp ()
exit ( 2 )
if ( sys . argv [ 1 ] == "help" or sys . argv [ 1 ] == "Help" ):
showHelp ()
OpenImagePath = ""
if ( sys . argv [ 1 ] == "gui" ):
root = Tk ()
root . filename = filedialog . askopenfilename ( title = "Select file" , filetypes = (( "jpeg files" , "*.jpg" ),( "all files" , "*.*" )))
OpenImagePath = root . filename
else :
OpenImagePath = sys . argv [ 1 ]
print ( OpenImagePath )
SaveImagePath = ""
head , tail = os . path . split ( OpenImagePath )
if ( len ( sys . argv ) == 3 ):
SaveImagePath = sys . argv [ 2 ]
else :
SaveImagePath = head + " \\ L_" + tail
try :
im = PIL . Image . open ( OpenImagePath )
L = im . convert ( 'L' )
if ( previewOption ):
L . show ()
L . save ( SaveImagePath )
#L.filter(ImageFilter.SHARPEN).save(SaveImagePath)
except Exception as e :
print ( str ( e ))
exit ( 1 )
print ( "Save in " + SaveImagePath )
print ( "OK!" )
exit ( 0 )
執行畫面
參考資料
http://www.tastones.com/zh-tw/tutorial/tkinter/tk-file-dialogs/
文章作者
oxygen
上次更新
2019-08-13
授權條款
Copyright © 2017-2024 oxygen. All rights reserved.