I am trying to convert image from jpg to png but not been able to save it in other directory.
Asked
Active
Viewed 6,057 times
-1
-
Try this https://stackoverflow.com/questions/43258461/convert-png-to-jpeg-using-pillow-in-python/43258955 – Jai Prak Jan 10 '18 at 06:54
-
1I see no code. No wonder it's not able. – Jongware Jan 10 '18 at 09:29
2 Answers
2
from PIL import Image
import glob, os
directory = "/your_path/"
for infile in glob.glob("*.JPG"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
rgb_im = im.convert('RGB')
rgb_im.save(directory + file + ".png", "PNG")
for infile in glob.glob("*.jpg"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
rgb_im = im.convert('RGB')
rgb_im.save(directory + file + ".png", "PNG")
for infile in glob.glob("*.JPEG"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
rgb_im = im.convert('RGB')
rgb_im.save(directory + file + ".png", "PNG")
for infile in glob.glob("*.jpeg"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
rgb_im = im.convert('RGB')
rgb_im.save(directory + file + ".png", "PNG")
you can use this code to save your file in other directory. This code saves the file with the same name in "your_path" directory

user169703
- 66
- 1
- 5
1
You can use PIL(Python Image Library). There might be other packages/libraries too out there. Currently, it supports Python 1.5.2 and newer, including 2.5 and 2.6.
Here is an example for using it
import Image
im = Image.open('/current/directory/PhotoName.jpg')
im.save('/other/directory/PhotoName.png')

HendraWD
- 2,984
- 2
- 31
- 45