Last active
August 13, 2024 03:37
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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