Skip to content

Instantly share code, notes, and snippets.

@RayPS
Last active October 19, 2023 12:27
Show Gist options
  • Save RayPS/5f8c31de2a4ded2f0e947996c30ab1fe to your computer and use it in GitHub Desktop.
Save RayPS/5f8c31de2a4ded2f0e947996c30ab1fe to your computer and use it in GitHub Desktop.
Converting image format in clipboard - macOS Python (Copied as PNG, Paste as JPG)
from PIL import Image
import io
import pasteboard
pb = pasteboard.Pasteboard()
pb_image = pb.get_contents(pasteboard.TIFF)
if pb_image:
image = Image.open(io.BytesIO(pb_image))
if image.mode == 'RGBA':
image.load()
new_image = Image.new('RGB', image.size, (255, 255, 255))
new_image.paste(image, mask=image.split()[3])
image = new_image
data_bytes = io.BytesIO()
image.save(data_bytes, format='JPEG', quality=90)
data_bytes = data_bytes.getvalue()
pb.set_contents(data=data_bytes, type=pasteboard.TIFF)
print('Converted clipboard image to JPG.')
else:
print('No image was copied.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment