2011-07-27 34 views
10

我想訪問linux剪貼板中的圖形,將其保存爲文件。 我在Python/Tkinter程序中這樣做,所以我問了它(http://stackoverflow.com/questions/6817600/save-the-image-in-the-clipboatd-in-python-tkinter),但內部(在Python中)沒有希望。來自剪貼板的linux映像

相反,我可以接受使用外部工具來做到這一點 - 但我找不到一個。

你知道任何基於終端的工具能夠將剪貼板內容保存爲圖像文件嗎?

+0

也許這有助於http://forums.debian.net/viewtopic.php?f=6&t=63433 – lhf

+0

謝謝,但我希望在一些更一般的東西,能夠採取剪貼板內容,而不是一種快照效用。我仍然無法相信這樣的效用不存在,儘管我在某處讀到X11剪貼板管理有點混亂...... – alessandro

回答

11

我找不到任何工具來做它,所以我寫了這個小Python腳本。它需要pygtk。

#!/usr/bin/python 
""" 
Save image from clipboard to file 
""" 

import sys 
import glob 
from optparse import OptionParser 

def def_file(): 
    """ 
    Return default file name 
    """ 
    files = glob.glob("img???.png") 
    if len(files) < 1: 
     return 'img001.png' 
    maxf = 0 
    for f in files: 
     try: 
      n = int(f[3:6]) 
      maxf = max(n, maxf) 
     except ValueError: 
      pass 
    return 'img{:03d}.png'.format(maxf+1) 


usage = """%prog [option] [filename] 
Save image from clipboard to file in PNG format.""" 

op = OptionParser(usage=usage) 
op.add_option("-o", "--open", action="store_true", dest="open", default=False, 
     help="Open saved file with default program (using xdg-open)") 
(options, args) = op.parse_args() 

if len(args) > 1: 
    parser.error("Only one argument expected") 
    sys.exit(1) 
elif len(args) == 1: 
    fname = args[0] 
else: 
    fname = def_file() 

import gtk 
clipboard = gtk.clipboard_get() 
image = clipboard.wait_for_image() 
if image is not None: 
    image.save(fname, "png") 
    print "PNG image saved to file", fname 
    if options.open: 
     import subprocess 
     subprocess.call(["xdg-open", fname]) 
else: 
    print "No image in clipboard found" 
+0

我剛剛注意到有人終於解決了我的問題,謝謝 - 很好的工作! – alessandro

+0

感謝您的腳本!我希望你不要介意我爲它創建了一個[public Github Gist](https://gist.github.com/orschiro/8b9a1ddadb696c2b4587#file-image-clipboard-to-file-py),供分發貢獻者輕鬆使用打包它。 – orschiro

3

複製:

xclip -selection clipboard in.png 

短:

xclip -se c in.png 

粘貼:

xclip -selection clipboard -target image/png -out > out.png 

較短的版本:

xclip -se c -t image/png -o > out.png 

從這個Unix的Linux的&問題:

https://unix.stackexchange.com/questions/145131/copy-image-from-clipboard-to-file

您還可以使用image/tiffimage/jpeg

+4

它在我的計算機上顯示錯誤..'錯誤:目標圖像/ png不可用' –

+0

@MerhawiFissehaye,首先運行'xclip -o -target TARGETS -selection clipboard'來知道哪些所有目標可以應用於'當前'剪貼板數據。 – rraadd88