Converting .jpg images to .png

Question:

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?

Asked By: user1417933

||

Answers:

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 🙂

Answered By: Levon

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.

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 )

Answered By: StuckAtWork

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)
Answered By: Semnodime
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

Answered By: isaac
import cv2

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

cv2.imwrite("test_image.png", image)
Answered By: Inyeni Showers
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.