How to put image / screenshot into computer clipboard? (Godot 4.2, Godot 4.x)

Godot Version

4.2.1

Question

My goal is to make it so when you take a screenshot, that screenshot is not only saved but also added to your clipboard. That way, you can just Ctrl+V somewhere and paste that screenshot.

Just for funsies, here’s the code I’m using to take a screenshot. You may use it if you like! I’d like to make it so that it also takes that image it just took (image.save_png(screenshot_path)) and puts it into the computer’s clipboard. That way I can just press Ctrl+V and it’ll paste that screenshot :slight_smile:

extends Node2D

@export_category("Screenshot Settings")
## If true, the screenshot will be upscaled.
## If false, the screenshot will be at its native resolution.
@export var upscale : bool = true
## Upscale by what factor?
@export var scaleFactor : int = 5

@onready var resizedWidth : int = get_viewport_rect().size.x * scaleFactor
@onready var resizedHeight : int = get_viewport_rect().size.y * scaleFactor

func _process(_delta):
	TAKE_SCREENSHOT()

func TAKE_SCREENSHOT():
	if Input.is_action_just_pressed("SCREENSHOT"):
		var date = Time.get_datetime_string_from_system().replace(".","_").replace(":","_")
		var screenshot_path = "user://"+ "screenshot_" + date + ".png"
		var image = get_viewport().get_texture().get_image()

		if upscale:
			var resizedImage = Image.new()

			resizedImage.create(resizedWidth,resizedHeight,false,Image.FORMAT_BPTC_RGBA)
			image.resize(resizedWidth,resizedHeight,Image.INTERPOLATE_NEAREST)
			resizedImage.blit_rect(image,Rect2(Vector2.ZERO,image.get_size()),Vector2.ZERO)

		image.save_png(screenshot_path)

I’ve tried putting DisplayServer.clipboard_set(image) or DisplayServer.clipboard_get_image() after image.save_png(screenshot_path), and looking the solution up online and I’m all out of ideas lol

Also, I’m trying to figure out what the default screenshot_path should be. I’d like to put screenshots in the user’s Pictures folder or maybe the folder the game’s EXE is in, but I don’t really know how to do that either. As far as I can tell, user:// doesn’t seem to lead anywhere.

(SEMI-)FIXED! No copy to clipboard, but this will work for now :slight_smile:

extends Node2D

@export_category("Screenshot Settings")
## If true, the screenshot will be upscaled.
## If false, the screenshot will be at its native resolution.
@export var upscale : bool = true
## Upscale by what factor?
@export var scaleFactor : int = 5

@onready var resizedWidth : int = get_viewport_rect().size.x * scaleFactor
@onready var resizedHeight : int = get_viewport_rect().size.y * scaleFactor

func _process(_delta):
	TAKE_SCREENSHOT()

func TAKE_SCREENSHOT():
	if Input.is_action_just_pressed("SCREENSHOT"):
		var date = Time.get_datetime_string_from_system().replace(".","_").replace(":","_")
		var screenshot_path = OS.get_system_dir(OS.SYSTEM_DIR_PICTURES) \
		+ "/" + "screenshot_" + date + ".png"
		var image = get_viewport().get_texture().get_image()

		if upscale:
			var resizedImage = Image.new()

			resizedImage.create(resizedWidth,resizedHeight,false,Image.FORMAT_BPTC_RGBA)
			image.resize(resizedWidth,resizedHeight,Image.INTERPOLATE_NEAREST)
			resizedImage.blit_rect(image,Rect2(Vector2.ZERO,image.get_size()),Vector2.ZERO)

		image.save_png(screenshot_path)

Copying an Image to the clipboard is not implemented yet.

The user:// path points to these locations depending on the OS File paths in Godot projects — Godot Engine (stable) documentation in English

To get the system’s Pictures folder you can use OS.get_system_dir()

You said it wasn’t implemented yet, do you know if there are any plans for this feature to be added in Godot 4.3 or later?

And thank you! I used this and it worked! I appreciate the help a lot. :slight_smile:
var screenshot_path = OS.get_system_dir(OS.SYSTEM_DIR_PICTURES) \ + "/" + "screenshot_" + date + ".png"

There was a PR open to set a image in the clipboard for windows and macOS/iOS Add clipboard image data get/set methods by radzo73 · Pull Request #79561 · godotengine/godot · GitHub but it got closed because a similar PR implementing only the get methods was merged. I’m not sure if someone will open a new PR with that functionality. You can follow the discussion here Improved clipboard functionality with support for multiple formats · Issue #7899 · godotengine/godot-proposals · GitHub