Skip to content

Instantly share code, notes, and snippets.

@mniami
Created April 21, 2021 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mniami/2c27f723947508a854b2dc42b326a05a to your computer and use it in GitHub Desktop.
Save mniami/2c27f723947508a854b2dc42b326a05a to your computer and use it in GitHub Desktop.
Convert image color format
import os
from PIL import Image
import click
@click.command()
@click.argument("path-to-image")
@click.argument("color-format")
def convert(path_to_image: str, color_format: str):
image = Image.open(path_to_image)
file_name, _ = os.path.splitext(path_to_image)
output_path = os.path.join(os.path.dirname(path_to_image), f"{file_name}_{color_format}.jpg")
if image.format == "PNG":
image = image.convert(color_format)
image.save(output_path, "JPEG")
return
if image.format == color_format:
raise ValueError("No need to convert")
image = image.convert(color_format)
image.save(output_path)
if __name__ == "__main__":
convert()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment