はじまり
チュンチュン!・・・コケコッコー!・・・
![リサちゃん](https://www.endorphinbath.com/wp-content/uploads/2021/10/02_nayutarisa_001-1.png)
はあ~、最近髪伸びてきたなあ、切ろっかな~。
![135ml](https://www.endorphinbath.com/wp-content/uploads/2021/10/01_kinkinbeer135ml_001-2.jpg)
そういえば、画像を一気に切り抜いてくれるツールを作ったのを思い出したわ!
今回はそのツールを紹介するぜ!
![リサちゃん](https://www.endorphinbath.com/wp-content/uploads/2021/10/02_nayutarisa_005.png)
うわ、出た! 展開速いパターン!
ツールの紹介
Anaconda Promptで動かしていきます。
まず、切り抜きたい、トリミングしたい画像たちが入っているフォルダを指定します。
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_01-1024x570.jpg)
画像がないフォルダを選ぶとスクリプトは何もなく終了です。
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_02-1024x130.jpg)
1度選んだ範囲で全てトリミングするか、毎回トリミングする範囲を選んでトリミングするかを選びます。今回は「1」にします。(まあ、「1」が出来ることが今回のスクリプトの真骨頂なので。)
表示されているディレクトリがWindowsなのに「/」区切りになっているのが気になりますが、まあ、一旦進んでみましょう。
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_03-1024x262.jpg)
トリミングした後のファイルを保存するフォルダの名前を入力します。今回は「トリミング後」にします。
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_04-1024x227.jpg)
入力すると、実際にトリミングする画面になります。画面サイズは決めておらず、縦長の画像なので、最初はこんな風に歪みます。(デフォルトの画面サイズは設定できるんですかねえ? まあ、使えればいいので、これで良いですが。)
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_05-997x1024.jpg)
画面も縦長にすれば、まあ見れる感じになりました。
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_06-917x1024.jpg)
そして、下の4つのスライダーを動かすと、実際にトリムする範囲を指定できます。
上限が青の線、下限がオレンジの線、左限が緑の線、右限が赤の線です。
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_07-775x1024.jpg)
範囲を選んだら、ウインドウを×ボタンで閉じます。
すると・・・・、できない!!!
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_08-1024x68.jpg)
なぜ、できないんだ~。っていう過程はとりあえず置いておきます。実はここが日本語になってるのが原因だったりします。
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_09-1024x206.jpg)
直して正常に動いたものが以下になります。「このディレクトリを見て確認してね。」的なメッセージが出てきて、トリミングした画像たちも保存されています。
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_10-1024x325.jpg)
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_11-1024x673.jpg)
ソース貼りました。
ソースはライブラリとして、以下のリポジトリのLandmasterLibraryに入っています。
特に気をつけたところ
ここでは、ソースを組むに当たって気をつけたところを記載しています。
ディレクトリをWindowsの「\」で区切る。
以下、ソースの抜粋部分です。
DirEditor.py
def DecideNowDir():
'''
nowDir : String absolutely directory default folder
nowDirPath : String absolutely path selected folder
'''
nowDir = os.path.abspath(os.path.dirname(__file__))
nowDirPath = filedialog.askdirectory(initialdir=nowDir)
# Discrimination whether Windows or Mac.
pf = platform.system()
if pf == 'Windows': # OS is Windows
nowDirPath = nowDirPath.replace('/', '\\')
print("Folder's Absolutely Path: {quotation}{folderpath}{quotation}".format(quotation='"',folderpath=nowDirPath))
return nowDirPath
該当箇所は、filedialog.askdirectory()
の部分で、これで選択されたファイルのディレクトリはなぜかWindowsでもMacでも”/”区切りとなってしまうみたいなのです。
それで、後続処理に問題が出るかどうかは調べていませんが、どうにも気持ち悪いのでWindowsで動かした時は文字の置換処理が動くようにしています。
ディレクトリに日本語が入っていると処理を行わずに強制終了させる。
以下、ソースの抜粋部分です。
ImageEditor.py
def TrimImage(trimmed_img_ext):
'''
folderList : List of file filtered with extension in the selected folder.
selectTimes : String of times to input.
extracted_dir : String absolutely path of directory has selected file.
img : Dictionary of pixels of image. (img [height] [width] [color channel])
trimmed_img : Dictionary of pixels of image.
trimmed_img_ext : String of extension of trimmed_img.
trimmed_img_name : String of filename of trimmed_img.
'''
fileList = FileListGetter.GetFileList(DirEditor.DecideNowDir(),trimmed_img_ext)
# Error Handling
if len(fileList) == 0:
print('\nImageEditor exits because of no target files.')
sys.exit(0)
# Error Handling
checkStr = re.compile('[\\a-zA-Z0-9\-\_\.\-\s\:\~\^\=]+')
if checkStr.fullmatch(fileList[0]) == None:
print('\nImageEditor exits because of the directory containing shift-jis character.')
sys.exit(0)
selectTimes = input('What times do you choosing? ("1" or "every"): ')
while selectTimes != '1' and selectTimes != 'every':
selectTimes = input('Retry. ("1" or "every"): ')
extracted_dir = DirEditor.MakeDirectory(fileList[0])
2個目の「Error Handling」の部分になります。
僕も、この記事を書くために動かして気づいたのですが、この画像切り抜きに使用しているOpenCVというモジュールは、なんと指定したディレクトリに日本語(ちゃんと言うとShift_JIS)が入っていると、画像を読み取れません・・・。
![](https://www.endorphinbath.com/wp-content/uploads/2021/11/20211101_09-1024x206.jpg)
OpenCVを作った人たちは、Shift_JISを文字だと思っていないようです・・・。日本語を文字だと思っていないようです・・・。
この不具合を回避するために、Pillowやnumpyなどのモジュールを使って処理する方法がありましたが、そこまでして日本語を使いたいわけではないので、いっそのこと、このモジュールを使う時には日本語のディレクトリは使用できないようにエラー処理を行わせるようにしました。
実装方法としては、ソース内で正規表現で指定した文字だけで表記されているディレクトリだけを使用できるようにしています。
# Error Handling
checkStr = re.compile('[\\a-zA-Z0-9\-\_\.\-\s\:\~\^\=]+')
if checkStr.fullmatch(fileList[0]) == None:
print('\nImageEditor exits because of the directory containing shift-jis character.')
sys.exit(0)
おしまい
![135ml](https://www.endorphinbath.com/wp-content/uploads/2021/10/01_kinkinbeer135ml_001-2.jpg)
まあ、今回はこんなところだ。
まさか、日本語が使えない仕様になっていたとは思わなんだ~。以前にMacで動かしたときは普通に動いていたからWindowsのせいかと思ったけど、そういや英数字しか使ってなかったような気がする・・・。
![リサちゃん](https://www.endorphinbath.com/wp-content/uploads/2021/10/02_nayutarisa_001-1.png)
そういうこともあるのね。なんかもう日本語で生活するのが嫌になるような出来事ですな・・・。
![135ml](https://www.endorphinbath.com/wp-content/uploads/2021/10/01_kinkinbeer135ml_001-2.jpg)
本当だな~。英語に親しんだ生活をしていかなければならんな。
![リサちゃん](https://www.endorphinbath.com/wp-content/uploads/2021/10/02_nayutarisa_021.png)
まじかあ。自己紹介とかどうすればいいんだろ。「那由多 リサ」ってどう言うんだろ?
![135ml](https://www.endorphinbath.com/wp-content/uploads/2021/10/01_kinkinbeer135ml_001-2.jpg)
普通に” Lisa Nayuta”でいいんじゃね?
ちなみに、1那由多は”novemdecillion”、1000那由多は”vigintillion”と英語では言うらしいよ。
![リサちゃん](https://www.endorphinbath.com/wp-content/uploads/2021/10/02_nayutarisa_026.png)
おおー、かっこいいじゃん。
これから、”I’m novemdecillion Lisa.”って言いまくるわ。
![135ml](https://www.endorphinbath.com/wp-content/uploads/2021/10/01_kinkinbeer135ml_001-2.jpg)
「私は、10の60乗人のリサです。」???
まあいいや、言いまくった結果を後で聞いてみよう。
以上になります!
このツールでトリミングした後は・・・・
一気にトリミングした複数の画像の重複を除きたい場合は、以下のツールで重複を排除してみてはいかがでしょうか?
こちらのツールもPythonで動くものになっています!
おまけ
![ペンギン](https://www.endorphinbath.com/wp-content/uploads/2021/10/03_penguinbeer135ml_001-1.jpg)
今回作ったソースは、以下のリポジトリのLandmasterLibraryに挙げています。
![リサちゃん](https://www.endorphinbath.com/wp-content/uploads/2021/10/02_nayutarisa_001-1.png)
また、この前段として、以下の記事もあります。よければ、見てみて下さい!
コメント