Created
January 26, 2022 15:33
-
-
Save FoamyGuy/a223fc9ebc5262de801defa296ed3fc2 to your computer and use it in GitHub Desktop.
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
# 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