Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 424
user1417933 Avatar asked May 25 '12 17:05

user1417933


People also ask

Does converting JPG to PNG improve quality?

Since you are starting with a JPG, going to a PNG isn't really buying you anything. You are going from a low fidelity image to a higher fidelity image. But it can't be MORE clear than your original image.


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

like image 77
Levon Avatar answered Sep 28 '22 07:09

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.

like image 22
Jan Vladimir Mostert Avatar answered Sep 28 '22 07:09

Jan Vladimir Mostert