Created
April 21, 2021 20:29
-
-
Save mniami/2c27f723947508a854b2dc42b326a05a to your computer and use it in GitHub Desktop.
Convert image color format
This file contains hidden or 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
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