Last active
March 29, 2023 22:50
-
-
Save alexrecuenco/d38fcc233387e08033ec3ce464c27a53 to your computer and use it in GitHub Desktop.
Conver image files
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
# Requires `pip install Pillow` | |
# RUN | |
# python convert.py **/*.bmp | |
# It will overwrite any .jpg file | |
from PIL import Image | |
from pathlib import Path | |
import sys | |
import os | |
import logging | |
logger = logging.getLogger("convert") | |
def convert(file, from_ext=".bmp", to_ext=".jpg"): | |
path = Path(file) | |
logger.debug(path) | |
if path.suffix != from_ext: | |
logger.warn( | |
"Incorrect path for '%s', we are only converting {%s}", | |
file, | |
from_ext, | |
) | |
return | |
with Image.open(path) as i: | |
new_path = path.with_suffix(to_ext) | |
i.save(new_path) | |
logger.info(f"saved '{path}' to '{new_path}'") | |
def main(): | |
LOGLEVEL = os.environ.get("LOGLEVEL", "INFO").upper() | |
logging.basicConfig(level=LOGLEVEL) | |
program, *files = sys.argv | |
if not files: | |
logger.warn("Please call as convert.py (files)") | |
exit(1) | |
for img in files: | |
convert(img, from_ext=".bmp", to_ext=".png") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment