37

I've looked around and read the docs, and found no way or solution, so I ask here. Is there any packages available to use Python to convert a JPG image to a PNG image?

Levon
  • 138,105
  • 33
  • 200
  • 191
user1417933
  • 805
  • 2
  • 11
  • 14
  • There appear to be python bindings to ImageMagick: http://www.imagemagick.org/download/python/. I haven't used them, but I've used ImageMagick, and it will do what you want. – Gort the Robot May 25 '12 at 17:50

6 Answers6

57

You could always use the Python Image Library (PIL) for this purpose. There might be other packages/libraries too, but I've used this before to convert between formats.

This works with Python 2.7 under Windows (Python Imaging Library 1.1.7 for Python 2.7), I'm using it with 2.7.1 and 2.7.2

from PIL import Image

im = Image.open('Foto.jpg')
im.save('Foto.png')

Note your original question didn't mention the version of Python or the OS you are using. That may make a difference of course :)

user5722540
  • 590
  • 8
  • 24
Levon
  • 138,105
  • 33
  • 200
  • 191
  • Well, it says Python 2.7 is required, and I have 2.7.3 so it won't let me install. Am I missing something? – user1417933 May 25 '12 at 17:55
  • 1
    @user1417933 if you are windows you need a flavour of PIL (32 or 64) compatible with your Python. – greggo May 25 '12 at 18:05
  • When i open the resultant image in IrfanView I get alert saying it's JPG image in png extension. Is result originally png? Python 2.7 on 64 bit windows machine – Mahendran Aug 04 '15 at 06:12
  • 1
    PIL is dead. Use Pillow now. See here to install: https://stackoverflow.com/a/20061019/4561887 – Gabriel Staples Aug 26 '18 at 03:31
  • 1
    Note, however, that these instructions still work fine, as Pillow is a fork from PIL, and in this case the import and commands are identical. – Gabriel Staples Aug 26 '18 at 03:50
13

Python Image Library: http://www.pythonware.com/products/pil/

From: http://effbot.org/imagingbook/image.htm

import Image
im = Image.open("file.png")
im.save("file.jpg", "JPEG")

save

im.save(outfile, options...)

im.save(outfile, format, options...)

Saves the image under the given filename. If format is omitted, the format is determined from the filename extension, if possible. This method returns None.

Keyword options can be used to provide additional instructions to the writer. If a writer doesn't recognise an option, it is silently ignored. The available options are described later in this handbook.

You can use a file object instead of a filename. In this case, you must always specify the format. The file object must implement the seek, tell, and write methods, and be opened in binary mode.

If the save fails, for some reason, the method will raise an exception (usually an IOError exception). If this happens, the method may have created the file, and may have written data to it. It's up to your application to remove incomplete files, if necessary.

Jonathan Root
  • 535
  • 2
  • 14
  • 31
Jan Vladimir Mostert
  • 12,380
  • 15
  • 80
  • 137
11

As I searched for a quick converter of files in a single directory, I wanted to share this short snippet that converts any file in the current directory into .png or whatever target you specify.

from PIL import Image
from os import listdir
from os.path import splitext

target_directory = '.'
target = '.png'

for file in listdir(target_directory):
    filename, extension = splitext(file)
    try:
        if extension not in ['.py', target]:
            im = Image.open(filename + extension)
            im.save(filename + target)
    except OSError:
        print('Cannot convert %s' % file)
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Semnodime
  • 1,872
  • 1
  • 15
  • 24
4
from glob import glob                                                           
import cv2 
pngs = glob('./*.png')

for j in pngs:
    img = cv2.imread(j)
    cv2.imwrite(j[:-3] + 'jpg', img)

this url: https://gist.github.com/qingswu/1a58c9d66dfc0a6aaac45528bbe01b82

Striped
  • 2,544
  • 3
  • 25
  • 31
isaac
  • 131
  • 1
  • 4
1
import cv2

image =cv2.imread("test_image.jpg", 1)

cv2.imwrite("test_image.png", image)
dejanualex
  • 3,872
  • 6
  • 22
  • 37
0

I don't use python myself, but try looking into: http://www.pythonware.com/products/pil/

import Image
im = Image.open("infile.png")
im.save("outfile.jpg")

(taken from http://mail.python.org/pipermail/python-list/2001-April/700256.html )

StuckAtWork
  • 1,613
  • 7
  • 23
  • 37