These scripts can change the format of images from PNG to JPG and JPG to PNG
Requirements:
Required Modules
pip install pillow //PIL==1.1.6
Code language: JavaScript (javascript)
Run the Script:
- Dynamic Change Copy the scriptÂ
convertDynamic.py
 into the directory where images are (PNG and/or JPG). And run:
$ python convertDynamic.py
- This will convert all JPG images to PNG and PNG images to JPG in the present directory tree recursively (i.e. will change format in images inside sub-directories too.)
- JPG to PNG (single image)
- Copy the JPG image to the directory whereÂ
JPGtoPNG.py
 exists - Replace file nameÂ
naruto_first.jpg
 insideÂJPGtoPNG.py
 (line 3) to input file name (JPG). - Replace file nameÂ
naruto.png
 insideÂJPGtoPNG.py
(line 4) to output file name (PNG). - Run following command:Â
$ python JPGtoPNG.py
- Copy the JPG image to the directory whereÂ
- PNG to JPG (single image)
- Copy the PNG image in directory whereÂ
PNGtoJPG.py
 exists - Replace file nameÂ
naruto_first.png
 insideÂPNGtoJPG.py
 (line 3) to input file name (PNG). - Replace file nameÂ
naruto.jpg
 insideÂPNGtoJPG.py
 (line 4) to output file name (JPG). - Run following command:Â
$ python PNGtoJPG.py
- Copy the PNG image in directory whereÂ
Source Code:
convertDynamic.py
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()
Code language: PHP (php)
PNGtoJPG.py
from PIL import Image
im = Image.open("naruto_first.png").convert("RGB")
im.save("naruto.jpg", "jpeg")
Code language: JavaScript (javascript)
JPGtoPNG.py
from PIL import Image
im = Image.open("naruto_first.jpg").convert("RGB")
im.save("naruto.png", "png")
Code language: JavaScript (javascript)