I want to make a python script that grabs the newest file from a given directory (always going to be an image file) and copy that image to my clipboard. How can I achieve this?
Asked
Active
Viewed 60 times
1 Answers
0
Your question is already answered here -
I am just putting it altogether.
import glob
import os
from cStringIO import StringIO
import win32clipboard
from PIL import Image
def send_to_clipboard(clip_type, data):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(clip_type, data)
win32clipboard.CloseClipboard()
list_of_files = glob.glob('/path/to/folder/*')
newest_file = max(list_of_files, key=os.path.getctime)
image = Image.open(newest_file)
output = StringIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:] # BMP file has a 14-byte header
output.close()
send_to_clipboard(win32clipboard.CF_DIB, data)

Tabaene Haque
- 576
- 2
- 10
-
What if I'm on Windows? They don't store clipboard in a folder, so is copying it to my clipboard still achievable? Basically I just want it to grab the image and copy it so I can paste it somewhere – inthanwill12 Aug 30 '20 at 14:31