kovidgoyal/kitty · error

Failed to encode image data to %s with error: %w

Error message

Failed to encode image data to %s with error: %w

What it means

During a clipboard download commit, after decoding the received image and writing it with write_image, any accumulated error is wrapped as a failure to encode image data to the requested MIME type. It indicates the clipboard data could not be converted/written in the target format (e.g. PNG vs JPEG mismatch or an encoder error).

Source

Thrown at kittens/clipboard/read.go:121

		}
	}()
	return images.Encode(output, img, self.mime_type)
}

func (self *Output) commit() {
	if self.err != nil {
		return
	}
	if self.image_needs_conversion {
		self.dest.Seek(0, io.SeekStart)
		img, _, err := image.Decode(self.dest)
		self.dest.Close()
		os.Remove(self.dest.Name())
		if err == nil {
			err = self.write_image(img)
		}
		if err != nil {
			self.err = fmt.Errorf("Failed to encode image data to %s with error: %w", self.mime_type, err)
		}
	} else {
		self.dest.Close()
		if !self.is_stream {
			f, err := os.OpenFile(self.arg, os.O_CREATE|os.O_RDONLY, 0666)
			if err == nil {
				fi, err := f.Stat()
				if err == nil {
					self.dest.Chmod(fi.Mode().Perm())
				}
				f.Close()
				os.Remove(f.Name())
			}
			self.err = os.Rename(self.dest.Name(), self.arg)
			if self.err != nil {
				os.Remove(self.dest.Name())
				self.err = fmt.Errorf("Failed to rename temporary file used for downloading to destination: %s with error: %w", self.arg, self.err)
			}

View on GitHub (pinned to 6d5d0c4406)

Solutions

  1. Request the exact MIME type the source provides: add `--mime image/png` (or the advertised type) to avoid re-encoding
  2. Retry the copy on the sender side so a fresh, untruncated payload is transferred
  3. Update kitty to the latest version to pick up clipboard image-transfer fixes
  4. If you only need the raw bytes, get the payload without forcing a different output format

Example fix

# before
kitten clipboard getfile --mime image/jpeg out.png
# after
kitten clipboard getfile --mime image/png out.png
Defensive patterns

Strategy: fallback

Try / catch

err=$(kitten clipboard getfile --mime image/png out.png 2>&1) || { echo "$err" | grep -q 'encode' && kitten clipboard getfile --mime image/jpeg out.jpg; }

Prevention

When it happens

Trigger: `kitten clipboard getfile image.png` where the terminal supplies image data in a different format and re-encoding into the target MIME type fails; corrupted or truncated image payloads received over the OSC channel.

Common situations: Remote clipboard clients that advertise one image MIME type but send another; version mismatches between kitty and the client on image transfer; large images truncated by terminal buffer limits.

Related errors


AI-assisted analysis of kovidgoyal/kitty@6d5d0c4406 (2026-08-27). Data as JSON: /api/errors/7f6ab8525995fe16. Report an issue: GitHub.