meg 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import gi, os, locale, subprocess, sys
  4. from datetime import datetime
  5. from gi.repository import Gtk, GObject, GdkPixbuf, Gdk, GLib
  6. translate = {"tr":{0:"Tüm ekran.",
  7. 1:"Alan seçimi.",
  8. 2:"Ekran yakalama öncesi gecikme.",
  9. 3:"saniye.",
  10. 4:"Kaydet.",
  11. 5:"Panoya Kopyala.",
  12. 6:"0x0.st ye yükle.",
  13. 7:"Ekran Görüntüsü Al",
  14. 8:"Uygula"},
  15. "en":{0:"Entire Screen.",
  16. 1:"Select a region.",
  17. 2:"Delay before caputring.",
  18. 3:"seconds.",
  19. 4:"Save.",
  20. 5:"Copy Clipboard.",
  21. 6:"Upload 0x0.st",
  22. 7:"Take Screenshot",
  23. 8:"Apply"}}
  24. l = locale.getdefaultlocale()
  25. l = l[0].split("_")[0]
  26. locales = list(translate.keys())
  27. if l not in locales:
  28. l = "en"
  29. _ = translate[l]
  30. SS_SAVE_DIR = "/tmp"
  31. SS_WHAT = 0#0 Save 1 Copy Clipboard 2 Upload 0x0.st
  32. def get_default_file_name():
  33. name = ""
  34. now = datetime.now()
  35. current_time = now.strftime("%F_%H%M%S")
  36. return "{}.png".format(current_time)
  37. def take_screen_shot(ss_size):
  38. f_name = os.path.join(SS_SAVE_DIR,get_default_file_name())
  39. if ss_size:
  40. take = subprocess.Popen(["sh","-c",'grim -g "{}" {}'.format(ss_size,f_name)], stdout = subprocess.PIPE)
  41. else:
  42. take = subprocess.Popen(["grim",f_name], stdout = subprocess.PIPE)
  43. take.wait()
  44. get_ss = GetSS(f_name)
  45. get_ss.connect("destroy", Gtk.main_quit)
  46. get_ss.show_all()
  47. def get_ss_destroy(win):
  48. if win.win_destroy_user:
  49. Gtk.main_quit()
  50. class GetSSInfo(Gtk.Window):
  51. def __init__(self):
  52. super(GetSSInfo, self).__init__()
  53. self.win_destroy_user = True
  54. m_box = Gtk.VBox()
  55. self.add(m_box)
  56. box = Gtk.HBox()
  57. m_box.pack_start(box,1,1,5)
  58. self.all_screen_tg = Gtk.Button()
  59. self.all_screen_tg.set_label(_[0])
  60. self.all_screen_tg.connect("clicked",self.take_ss, False)
  61. box.pack_start(self.all_screen_tg,1,1,5)
  62. self.select_area_tg = Gtk.Button()
  63. self.select_area_tg.set_label(_[1])
  64. self.select_area_tg.connect("clicked",self.take_ss, True)
  65. box.pack_start(self.select_area_tg,1,1,5)
  66. box = Gtk.HBox()
  67. m_box.pack_start(box,0,0,5)
  68. box.pack_start(Gtk.Label(_[2]),0,0,5)
  69. ad = Gtk.Adjustment(0, 0, 300, 1, 0, 0)
  70. self.delay_sb = Gtk.SpinButton(adjustment=ad, climb_rate=1, digits=0)
  71. self.delay_sb.set_wrap(True)
  72. box.pack_start(self.delay_sb,0,0,5)
  73. box.pack_start(Gtk.Label(_[3]),0,0,5)
  74. def take_ss(self, widget, ss_size):
  75. SS_DELAY = self.delay_sb.get_text()
  76. self.win_destroy_user = False
  77. self.destroy()
  78. if SS_DELAY == "0":
  79. SS_DELAY = "0.5"
  80. if ss_size:
  81. ss_size = subprocess.Popen(["slurp"], stdout = subprocess.PIPE)
  82. ss_size.wait()
  83. ss_size = ss_size.communicate()[0]
  84. ss_size = ss_size.decode("utf-8","strict")
  85. ss_size = ss_size.replace("\n","")
  86. GObject.timeout_add(float(SS_DELAY)*1000, take_screen_shot, ss_size)
  87. class GetSS(Gtk.Window):
  88. def __init__(self,file_name):
  89. super(GetSS, self).__init__()
  90. box = Gtk.VBox()
  91. self.add(box)
  92. scroll = Gtk.ScrolledWindow()
  93. box.add(scroll)
  94. scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
  95. self.image_viewer = Gtk.Image()
  96. scroll.add(self.image_viewer)
  97. self.set_default_size(480,400)
  98. self.width, self.height = self.get_size()
  99. self.view_image(file_name)
  100. self.file_choser = Gtk.FileChooserButton()
  101. self.file_choser.set_action(Gtk.FileChooserAction.SELECT_FOLDER)
  102. box.pack_start(self.file_choser,0,0,5)
  103. img_dir = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_PICTURES)
  104. if img_dir == None:
  105. img_dir = os.path.expanduser("~")
  106. self.file_choser.set_current_folder(img_dir)
  107. self.save_tg = Gtk.Button()
  108. self.save_tg.set_label(_[4])
  109. self.save_tg.connect("clicked",self.save_tg_ps,file_name)
  110. box.pack_start(self.save_tg,0,0,5)
  111. self.clipboard_tg = Gtk.Button()
  112. self.clipboard_tg.set_label(_[5])
  113. self.clipboard_tg.connect("clicked",self.clipboard_tg_ps,file_name)
  114. box.pack_start(self.clipboard_tg,0,0,5)
  115. self.up_0x0_tg = Gtk.Button()
  116. self.up_0x0_tg.set_label(_[6])
  117. self.up_0x0_tg.connect("clicked",self.up_0x0_tg_ps,file_name)
  118. box.pack_start(self.up_0x0_tg,0,0,5)
  119. def save_tg_ps(self,widget,file_name):
  120. folder = self.file_choser.get_filename()
  121. subprocess.Popen(["cp",file_name,folder], stdout = subprocess.PIPE)
  122. self.destroy()
  123. def clipboard_tg_ps(self,widget,file_name):
  124. subprocess.Popen(["sh","-c","wl-copy < {}".format(file_name)], stdout = subprocess.PIPE)
  125. self.destroy()
  126. def up_0x0_tg_ps(self,widget,file_name):
  127. get = subprocess.Popen(["sh","-c","curl -F'file=@{}' https://0x0.st".format(file_name)], stdout = subprocess.PIPE)
  128. get.wait()
  129. get = get.communicate()[0]
  130. get = get.decode("utf-8","strict")
  131. subprocess.Popen(["sh","-c","wl-copy {}".format(get)], stdout = subprocess.PIPE)
  132. self.destroy()
  133. def view_image(self,file_name):
  134. active_pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(file_name,
  135. int(self.width),
  136. int(self.height),
  137. True)
  138. self.image_viewer.set_from_pixbuf(active_pixbuf)
  139. if __name__ == '__main__':
  140. args = sys.argv
  141. if not ("--fullscreen" in args or "--area" in args):
  142. get_ss_info = GetSSInfo()
  143. get_ss_info.connect("destroy", get_ss_destroy)
  144. get_ss_info.show_all()
  145. else:
  146. ss_size = False
  147. SS_DELAY = 0
  148. for a in args:
  149. if a == "-fullscreen":
  150. ss_size = False
  151. elif a == "--area":
  152. ss_size = True
  153. elif "--timer=" in a:
  154. try:
  155. SS_DELAY = int(a.replace("--timer=",""))
  156. except:
  157. pass
  158. if ss_size:
  159. ss_size = subprocess.Popen(["slurp"], stdout = subprocess.PIPE)
  160. ss_size.wait()
  161. ss_size = ss_size.communicate()[0]
  162. ss_size = ss_size.decode("utf-8","strict")
  163. ss_size = ss_size.replace("\n","")
  164. GObject.timeout_add(float(SS_DELAY)*1000, take_screen_shot, ss_size)
  165. Gtk.main()