1

Following is the code I'm using to compress set of .tif images to .jpg. My objective is to iterate through each .tif image and create a new set of compressed jpg images.

from osgeo import gdal
import os
for filename in os.listdir(os.getcwd()):
    #print(filename.split("."))
    if(filename.split(".")[1] == "tif"):
        print(filename.split(".")[0])
        gdal_translate \
          -co COMPRESS=JPEG \
          -co TILED=YES \
          filename.split(".")[0].tif  filename.split(".")[0].jpg

Though I don't see an error in this code. I followed the technique mentioned in this blog post., I'm getting the following error when I try to execute the above code. The error message is:

  File "test.py", line 8
    -co COMPRESS=JPEG \
               ^
SyntaxError: invalid syntax
        

What seems to be the issue here?

1
  • 2
    gdal_translate is a commandline executable, not a python module. Either run it from a command prompt/shell terminal or use the gdal.VectorTranslate python method.
    – user2856
    Commented May 31, 2021 at 23:56

2 Answers 2

2

It looks like you're attempting to save as JPEG files. In which case GDAL would use the JPEG raster driver. The JPEG raster driver does not have a COMPRESS= creation option. See the driver's documentation at https://gdal.org/drivers/raster/jpeg.html#raster-jpeg and scroll down to see the list of creation options.

In fact, setting a compression option to JPEG for a JPEG file wouldn't make sense, anyhow, as JPEGs are always JPEG compressed. It's just a matter of how much they are compressed (versus the quality retained - JPEG compression is lossy). You could use the QUALITY= creation option for JPEGs to control the quality versus compression.

Alternatively, you could save the files as TIFF files (using the GDAL GTIFF driver), and then you could use the COMPRESS=JPEG creation option which would use JPEG compression within a TIFF file. But the output files would be TIFFs, not JPEGs.

2
  • thanks for your comment @Son of a Beach ;) . I'll look more into this. My objective was to compress .tif to jpg for rendering purposes and maintain native .tif separately for download purposes. I found a technique to do so. thanks for your comment.
    – 0xMinCha
    Commented May 31, 2021 at 22:45
  • Using JPEG for rendering is not a good idea. The whole JPEG image must be decompressed each time even if you need just a small part from the image. Use JPEG compressed and tiled TIFF instead -of GTiff -co tiled=yes -co compress=jpeg.
    – user30184
    Commented Jun 1, 2021 at 6:19
0

Apparently, this short shell script does what I want. The script is inspired from the answer that appeared in this post.

#!/bin/sh

for f in *.tif; do
     #gdal_translate -co COMPRESS=JPEG -co TILED=YES "$f" "${f%.*}.jpg"
     gdal_translate  "$f" "jpg/${f%.*}.jpg"
done
1
  • I wonder what you get as a result. Could you add gdalinfo from a resulting file?
    – user30184
    Commented Jun 1, 2021 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.