剪贴板中的 linux 图像 [英] linux image from clipboard
问题描述
我想访问 linux 剪贴板中的图形,将其保存为文件.我在 Python/Tkinter 程序中这样做,所以我问了它(http://stackoverflow.com/questions/6817600/save-the-image-in-the-clipboatd-in-python-tkinter)但在内部(在 python 中)没有希望.
I'd like to access the graphics in the linux clipboard, to save it as a file. I'm doing this in a Python/Tkinter program, so I asked about it (http://stackoverflow.com/questions/6817600/save-the-image-in-the-clipboatd-in-python-tkinter) but internally (in python) there's no hope.
相反,我可以接受使用外部实用程序来执行此操作 - 但我找不到.
Instead, I can accept to use an external utility to do it - yet I cannot find one.
您是否知道有任何基于终端的实用程序能够获取剪贴板内容并将其另存为图像文件?
Do you know of any terminal-based utility able to take the clipboard content and save it as an image file?
推荐答案
我找不到任何工具来做这件事,所以我写了这个小的 Python 脚本.它需要pygtk.
I couldn't find any tool to do it, so I wrote this small Python script. It requires 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"
这篇关于剪贴板中的 linux 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!