Skip to content

Instantly share code, notes, and snippets.

@FoamyGuy
Created March 18, 2022 19:38
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 FoamyGuy/61d5f9ba96559186dc9c518917088610 to your computer and use it in GitHub Desktop.
Save FoamyGuy/61d5f9ba96559186dc9c518917088610 to your computer and use it in GitHub Desktop.
# 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