user1417933
user1417933

Reputation: 835

Converting .jpg images to .png

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?

Upvotes: 40

Views: 112300

Answers (6)

Inyeni Showers
Inyeni Showers

Reputation: 11

import cv2

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

cv2.imwrite("test_image.png", image)

Upvotes: 1

Semnodime
Semnodime

Reputation: 2003

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)

Upvotes: 13

isaac
isaac

Reputation: 141

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

Upvotes: 4

Levon
Levon

Reputation: 143017

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 :)

Upvotes: 64

Jan Vladimir Mostert
Jan Vladimir Mostert

Reputation: 12972

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.

Upvotes: 12

StuckAtWork
StuckAtWork

Reputation: 1633

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 )

Upvotes: 0

Related Questions