Hi, I’m trying to do the equivalent of Raster->Data Conversion->Band Select from a Python script using jpy to bridge the Java Api with Python.
The closest that I got was to use one of the snappy examples to create a rgb_image. The thing is that I need to create a new product with 4 bands and the method ProductUtils.createImageInfo
only accepts 3 bands (the RGB). Is there another method to do almost the same but with more bands?
import os
import sys
# Imports Snap's module
sys.path.append('C:/Users/DEV/.snap/snap-python')
import snappy
from snappy import jpy, Band, ProductIO, ProductUtils, ProgressMonitor
jpy = snappy.jpy
# More Java type definitions required for image generation
Color = jpy.get_type('java.awt.Color')
ColorPoint = jpy.get_type('org.esa.snap.core.datamodel.ColorPaletteDef$Point')
ColorPaletteDef = jpy.get_type('org.esa.snap.core.datamodel.ColorPaletteDef')
ImageInfo = jpy.get_type('org.esa.snap.core.datamodel.ImageInfo')
ImageLegend = jpy.get_type('org.esa.snap.core.datamodel.ImageLegend')
ImageManager = jpy.get_type('org.esa.snap.core.image.ImageManager')
JAI = jpy.get_type('javax.media.jai.JAI')
RenderedImage = jpy.get_type('java.awt.image.RenderedImage')
GPF = jpy.get_type('org.esa.snap.core.gpf.GPF')
# Disable JAI native MediaLib extensions
System = jpy.get_type('java.lang.System')
System.setProperty('com.sun.media.jai.disableMediaLib', 'true')
def write_rgb_image(bands, filename, format):
image_info = ProductUtils.createImageInfo(bands, True, ProgressMonitor.NULL)
im = ImageManager.getInstance().createColoredBandImage(bands, image_info, 0)
JAI.create("filestore", im, filename, format)
file = 'D:/Union_bandas/IMAGE.SAFE/MTD_MSIL1C.xml'
product = ProductIO.readProduct(file)
image_format = 'tiff'
blue = product.getBand('B2')
green = product.getBand('B3')
red = product.getBand('B4')
infrared = product.getBand('B8')
write_rgb_image([red, green, blue, infrared], 'snappy_write_image_rgb.tiff', image_format)