Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting tiff to jpeg in python

Tags:

Can anyone help me to read .tiff image and convert into jpeg format?

from PIL import Image im = Image.open('test.tiff') im.save('test.jpeg') 

The above code was not working.

like image 837
Anbarasan Thangapalam Avatar asked Mar 05 '15 05:03

Anbarasan Thangapalam


People also ask

How do I convert TIFF to JPEG without losing quality?

Make your TIFF images into JPGs.Choose File and select Save As. Or, choose File, then Export, and Save for Web (Legacy). Either process can be used to save CMYK, RGB, or grayscale images. Note: JPGs support only 8-bit images, so the bit depth will automatically be lowered on anything with a 16-bit image quality.

How do I convert multiple TIFF files to JPEG?

Go to File> Scripts > Image Processor. Click Select Folder to navigate to your TIFF file folder and select the output location. In the File Type, check the box before Save as JPEG. Finally, click Run to start converting multiple TIFF files to JPEG with Photoshop.


1 Answers

I have successfully solved the issue. I posted the code to read the tiff files in a folder and convert into jpeg automatically.

import os from PIL import Image  yourpath = os.getcwd() for root, dirs, files in os.walk(yourpath, topdown=False):     for name in files:         print(os.path.join(root, name))         if os.path.splitext(os.path.join(root, name))[1].lower() == ".tiff":             if os.path.isfile(os.path.splitext(os.path.join(root, name))[0] + ".jpg"):                 print "A jpeg file already exists for %s" % name             # If a jpeg is *NOT* present, create one from the tiff.             else:                 outfile = os.path.splitext(os.path.join(root, name))[0] + ".jpg"                 try:                     im = Image.open(os.path.join(root, name))                     print "Generating jpeg for %s" % name                     im.thumbnail(im.size)                     im.save(outfile, "JPEG", quality=100)                 except Exception, e:                     print e 
like image 109
Anbarasan Thangapalam Avatar answered Sep 22 '22 06:09

Anbarasan Thangapalam