-1

I am trying to convert image from jpg to png but not been able to save it in other directory.

Rohit Sharma
  • 1
  • 1
  • 2

2 Answers2

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