TransWikia.com

Convert multiple bsq files to jpg images in python

Geographic Information Systems Asked by Elena Ioannidou on March 22, 2021

I am trying to convert a big number of bsq files to JPEG images. I found the way to do it only for one file but I cannot figure out how to put it in a loop in order to run repetitively all the files that are in a folder. Here is what I already did because I have already found it online.

import gdal

img_bsq  =image.bsq
img_jpg  =image.jpg

gdal.Translate(img_jpg, img_bsq, format='JPEG', width=1024, height=0, 
scaleParams=[[]])

2 Answers

Not sure how to do this in Python but a simply batch or bash script that calls GDAL_TRANSLATE for each bsb will do the trick.

set /p bsbfolder=folder where NOAA RNC are located:

for /r "%bsbfolder%" %%f in (*.kap) do gdal_translate -of JPEG -co WORLDFILE=YES -co QUALITY 75 %%f %%~nf.jpg

Answered by Geospatial Engineer on March 22, 2021

Here is how you iterate over all the files in a directory using Python. Note the importance of using the os module to manipulate file paths.

import gdal
import glob, os

in_ws = '/path/to/your/in_workspace'
out_ws = '/path/to/your/out_workspace'

# List bsq files in workspace
imgs = glob.glob(os.path.join(in_ws, "*.bsq"))

for img in imgs:
    # Define the output path
    out_jpg = os.path.join(out_ws, os.path.basename(img).split(".")[0] + ".jpg")
    
    # Convert to JPEG
    gdal.Translate(out_jpg, img, format='JPEG', width=1024, height=0, 
    scaleParams=[[]])

Answered by Aaron on March 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP