#πŸ”’ Function to Copy a .GIF directly to Clipboard

49 messages Β· Page 1 of 1 (latest)

green mirage
#

Hi, looking to create a function to copy .gif files from a given filepath directly to the clipboard so that I can paste them anywhere such as into Discord.

From my research online I have not found a clear way to do this, only methodology for storing images not GIFs.

In Firefox based browsers, you can directly right click on a .GIF and copy to clipboard, and even if you hit Windows + V, you can see the GIF autoplaying, and when you paste it anywhere such as Discord its the actuall full .GIF, so I am assuming this is a possibility with Python.

I am new to python by the way. I am learning with tkinter. Thank you

warm ravenBOT
#

@green mirage

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rocky cosmos
#

i know that you wrote that you are trying to learn tkinter specifically, but i'm not sure that you can do what you are asking using specifically tkinter

green mirage
#

It can use anything

#

I am just wanting a function that I can use with my tkinter code, just specified that because outside of tkinter I dont really know anything

rocky cosmos
green mirage
#

Yes! This is what I am trying to use currently at the moment. I am struggling with the gif aspect

rocky cosmos
green mirage
#

I found this online prior to making this post:

def send_to_clipboard(clip_type, filepath):
    image = Image.open(filepath)

    output = BytesIO()
    image.convert("RGB").save(output, "BMP")
    data = output.getvalue()[14:]
    output.close()

    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(clip_type, data)
    win32clipboard.CloseClipboard()```

What would you recommend for clip_type?
rocky cosmos
#

maybe go the other way around and read the clipboard with a python program after you know you have a gif in your clipboard to find out what should go there

#

kind of a little bit of reverse engineering

green mirage
#

Oh that is smart! Let me see if I am capable of pulling this off

green mirage
#
TypeError: Specified clipboard format is not available```
#

Thats from me doing

    win32clipboard.OpenClipboard()
    data_debug = win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()
    print(str(data_debug))```
#

When there is a gif on my clipboard

rocky cosmos
#

hold on and i'll look at the docs and also try to implement something short

green mirage
#

Okay thank you very much

rocky cosmos
#

i do hope that you continue to try yourself during that time and not just put it off in hope that i will solve it
because i'm not sure that i'll be successful

green mirage
#

Oh i am! I have actually been at this for like 6 hours lol

rocky cosmos
green mirage
#

I can not move on from something until its solved or until everything that I can possible conceive of conceiving of trying is tried

#

omg

#

i got it

#

its so simple

#
def send_to_clipboard(filepath):
    command = f'powershell Set-Clipboard -LiteralPath \'{filepath}\''
    os.system(command)
    print('{filepath}')```
#

this works

#

i can paste it into discord and everything

rocky cosmos
#

oh, using an external system command through powershell

#

well, it's kind of a hack, but whatever works i guess

#

i'll continue to look for a way to use the actual API

#

because with that solution you need to have a file on the file system first

green mirage
#

Ok now i gotta figure out how to get all the gifs in a directory then list them in a grid and have them autoplay and have a search function by filename lol

green mirage
#

There will never be a case where that isnt true

#

Im trying to make a gif browser like discord but with local files so its 1) obscenely faster loading and 2) lets me search MY gifs and not a library of random aah shi

rocky cosmos
#

oh, i see πŸ‘

green mirage
#

Ty for your help

rocky cosmos
#

but now you got me intrigued and i will have a hard time letting this go without a API based solution πŸ˜…

green mirage
#

If you find it let me know I am curious also. I sent you a friend request

#

How do I mark this thread as solved or get rid of it

rocky cosmos
green mirage
#

ye thats fine

#

just shout it to the stars ill hear it

warm ravenBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.