Created
March 18, 2022 19:38
-
-
Save FoamyGuy/61d5f9ba96559186dc9c518917088610 to your computer and use it in GitHub Desktop.
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
# convert a .png image file to a .bmp image file using PIL | |
from PIL import Image | |
import os | |
for file in os.listdir("."): | |
file_in = file | |
if ".png" in file: | |
img = Image.open(file_in) | |
file_out = file.replace(".png", ".bmp") | |
print (len(img.split())) # test | |
if len(img.split()) == 4: | |
# prevent IOError: cannot write mode RGBA as BMP | |
r, g, b, a = img.split() | |
img = Image.merge("RGB", (r, g, b)) | |
img.save(file_out) | |
else: | |
img = img.convert(mode="P", palette=Image.WEB) | |
img.save(file_out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment