Skip to content

Instantly share code, notes, and snippets.

@pythonsuezo
Last active May 21, 2018 04:45
Show Gist options
  • Select an option

  • Save pythonsuezo/99841c1a151965717fc391e6d7fac3e4 to your computer and use it in GitHub Desktop.

Select an option

Save pythonsuezo/99841c1a151965717fc391e6d7fac3e4 to your computer and use it in GitHub Desktop.
クリップ画像保存器その3のコード2
import clipframe # wxFormBuilderのソース
import wx
import os, sys, re
import datetime
import configparser
from PIL import ImageGrab, Image
"""----------------------------------------------
クリップ画像保存器
グローバル定数
path : 実行ファイルのディレクトリ
today : 今日の日付
now : 今の時間
INI : 設定ファイルの場所
設定ファイルに最後に保存したディレクトリの情報を記載する
----------------------------------------------"""
path = os.path.dirname( sys.argv[0] )
INI = path + "/INI.conf"
conf = configparser.SafeConfigParser()
now = datetime.datetime.now()
today = now.strftime( "%Y/%m/%d" ) # 日付 YYYY/MM/DD
now = now.strftime( "%H:%M:%S" ) # 時刻 HH:MM:SS
class Mainframe( clipframe.MyFrame1 ):
def __init__( self, parent ):
clipframe.MyFrame1.__init__( self, parent )
def Dirset( self, event ):
dir = self.m_dirPicker1.GetPath()
print(dir)
self.m_textCtrl2.SetValue(dir)
return
#-----------------------------------------ここから
def Opendir( self, event ):
dir = self.m_textCtrl2.GetValue() # テキストコントロールから保存先を取得する
if os.path.exists( dir ): # ディレクトリが存在するか?
os.popen("explorer %s" % dir) # あるなら親フォルダを開く
else:
os.popen("explorer %s" % path) # ないなら実行ファイルのフォルダを開く
return
def Clipsave( self, event ):
im = ImageGrab.grabclipboard() # クリップボードの画像を取得
dir = self.m_textCtrl2.GetValue()
manual_save = dir + "/manual" # 手動保存のディレクトリ
file_name = "pic_"
if im == None: # クリップボード内に画像が無ければ何もしない
return print("クリップボードに画像がありません")
if os.path.exists( dir ): # ディレクトリが存在するか?
if not os.path.exists(manual_save): # 手動保存のディレクトリが存在するか?
os.mkdir(manual_save) # 無いなら作る
else:
return print("存在しないディレクトリ:",dir)
# 保存先のフォルダとファイル名を投げるとファイル名+1.pngを返す関数を作った
new_file = self.New_file(manual_save, file_name)
im.save( manual_save + new_file ) # ファイルを保存
print(manual_save + new_file) # 保存先
return
def Monitoring_set( self, event ):
m_set = self.m_toggleBtn2.GetValue() # トグルスイッチの状態を取得
print(m_set)
def New_file( self, dir, file_name ):
# ファイル名のリストを受け取って最高値+1のファイル名を返す
# 同名ファイルのナンバリング最高値を求める
dir_list = os.listdir(dir) # フォルダの中身をリスト化
if len(dir_list) == 0: # ディレクトリ内にファイルがない場合は00000
return "/" + file_name + "00000" + ".png"
max_num = max([s for s in dir_list if s.startswith(file_name)]).split(".")[0]
max_num = re.sub(file_name, r"", max_num) # ファイル名を削除
new_file = "{0:05d}".format(int(max_num)+1) # ファイル名に+1して5ケタでゼロサプレスする
return "/" + file_name + new_file + ".png"
#-----------------------------------------ここまで追記
app = wx.App( False )
frame = Mainframe( None )
frame.Show( True )
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment