Skip to content

Instantly share code, notes, and snippets.

@harendra21
Created February 2, 2022 06:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save harendra21/2db0d6e72baf4a3b0467b637d1860a71 to your computer and use it in GitHub Desktop.
from PIL import Image
import sys
import os
try:
im = None
for root, dirs, files in os.walk("."):
for filename in files:
if filename.endswith('.jpg'):
im = Image.open(filename).convert("RGB")
im.save(filename.replace('jpg', 'png'), "png")
elif filename.endswith('.png'):
im = Image.open(filename).convert("RGB")
im.save(filename.replace('png', 'jpg'), "jpeg")
else:
print('dont have image to convert')
except IOError:
print('directory empty!')
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment