Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: saduka on June 14, 2020, 05:45:49 AM

Title: exportRaster with jpeg compression
Post by: saduka on June 14, 2020, 05:45:49 AM
Hi, I want to export a quite large orthomosaic with the jpeg compression to reduce the size.
I used the following script, but it reported an error "cant write image".

Code: [Select]
import Metashape
chunk = Metashape.app.document.chunk
d_projection = Metashape.OrthoProjection()
d_projection.crs = Metashape.CoordinateSystem("EPSG::4326")
compression = Metashape.ImageCompression()
compression.tiff_compression = Metashape.ImageCompression.TiffCompressionJPEG
compression.jpeg_quality = 20
compression.tiff_big = True
compression.tiff_compression = True

chunk.exportRaster("C:/data/test.tif",
                    format = Metashape.RasterFormatTiles,
                    image_format = Metashape.ImageFormat.ImageFormatJPEG,
    image_compression = compression,
                    save_alpha = False,
                    white_background = False,
                    save_kml = False,
                    description =  " ",
                    projection = d_projection)
Title: Re: exportRaster with jpeg compression
Post by: Alexey Pasumansky on June 14, 2020, 01:46:53 PM
Hello saduka,

Can you please provide the full output related to the exportRaster operation?
Title: Re: exportRaster with jpeg compression
Post by: saduka on July 05, 2020, 12:56:59 PM
Hi Alexey:
This is the error output. I am quite confused at this problem. The script worked perfect for a small orthomosaic, but not for a larger scale.

2020-07-05 17:48:30 Traceback (most recent call last):
2020-07-05 17:48:30   File "E:/test.py", line 19, in <module>
2020-07-05 17:48:30     projection = d_projection)
2020-07-05 17:48:30 RuntimeError: Can't create image: E:/test.tif
2020-07-05 17:48:30 Error: Can't create image: E:/test.tif
Title: Re: exportRaster with jpeg compression
Post by: saduka on July 10, 2020, 09:03:37 AM
Hello saduka,

Can you please provide the full output related to the exportRaster operation?

Actually, I want to report this as a general bug for python scripting. The script runs fine for small projects. Manual compression output through graphic interface works also fine.
However, when a project contains more than 3000 cameras, and the orthomosaic tile scale is larger than 80000x80000,  the error jumps out.
The following error report is just one case, which has been the same for all of our large aerial map compression output.

Code: [Select]
2020-07-10 13:39:10 LoadProject: path = E:/Agisoftprojects/20200526.psx
2020-07-10 13:39:10 Loading project...
2020-07-10 13:39:11 loaded project in 1.746 sec
2020-07-10 13:39:11 Finished processing in 1.755 sec (exit code 1)
2020-07-10 13:39:15 ExportRaster: image_format = JPEG, path = E:/test.tif, projection = WGS 84, save_alpha = off, white_background = off
2020-07-10 13:39:15 generating 91272 x 92998 raster in 1 x 1 tiles
2020-07-10 13:39:15 Exporting orthomosaic...
2020-07-10 13:39:15 Finished processing in 0.01 sec (exit code 0)
2020-07-10 13:39:15 Traceback (most recent call last):
2020-07-10 13:39:15   File "E:/lingang_project/test.py", line 19, in <module>
2020-07-10 13:39:15     projection = d_projection)
2020-07-10 13:39:15 RuntimeError: Can't create image: E:/test.tif
2020-07-10 13:39:15 Error: Can't create image: E:/test.tif
2020-07-10 13:40:29 Found 2 GPUs in 0.407 sec (CUDA: 0.323 sec, OpenCL: 0.084 sec)
Title: Re: exportRaster with jpeg compression
Post by: Alexey Pasumansky on July 10, 2020, 03:20:05 PM
Hello saduka,

JPEG format doesn't support images with the dimensions greater than 65535 by any size, so you cannot use JPEG export format or JPEG compression option, if the orthomosaic exceeds these limits.
Title: Re: exportRaster with jpeg compression
Post by: saduka on July 21, 2020, 04:49:33 AM
Hello saduka,

JPEG format doesn't support images with the dimensions greater than 65535 by any size, so you cannot use JPEG export format or JPEG compression option, if the orthomosaic exceeds these limits.

Hi Alexey, thanks for the answer. Thats very helpful.
But when I manually export the orthomosaic through your graphic UI,  an 8 bit depth jpeg compressed raster of  a tile larger than 65536 x 65536(90000+ x 90000+) can be exported. I am wondering how is this achieved through python?
Title: Re: exportRaster with jpeg compression
Post by: Alexey Pasumansky on August 09, 2020, 01:32:03 AM
Please provide the screenshot of the Export Orthomosaic dialog with the parameters used and the output from the Console pane corresponding to the related export operation.
Title: Re: exportRaster with jpeg compression
Post by: saduka on August 12, 2020, 11:54:20 AM
Please provide the screenshot of the Export Orthomosaic dialog with the parameters used and the output from the Console pane corresponding to the related export operation.
(https://ftp.bmp.ovh/imgs/2020/08/22a0caceac214390.png)
Code: [Select]
2020-08-12 15:55:01 LoadProject: path = E:/Agisoftprojects/20200717.psx
2020-08-12 15:55:01 Loading project...
2020-08-12 15:55:03 loaded project in 1.604 sec
2020-08-12 15:55:03 Finished processing in 1.613 sec (exit code 1)
2020-08-12 15:57:36 ExportOrthomosaic: block_height = 1080, block_width = 3840, clip_to_boundary = off, image_format = TIFF, path = E:/test.tif, projection = WGS 84, resolution_x = 1.14766e-06, resolution_y = 9.8963300000000008e-07, save_alpha = off
2020-08-12 15:57:36 Exporting orthomosaic...
2020-08-12 15:57:36 generating 90919 x 91168 raster in 1 x 1 tiles
2020-08-12 16:05:46 Finished processing in 489.236 sec (exit code 1)
>>>
Title: Re: exportRaster with jpeg compression
Post by: saduka on August 25, 2020, 07:45:44 AM
Please provide the screenshot of the Export Orthomosaic dialog with the parameters used and the output from the Console pane corresponding to the related export operation.
After comparison, it seems that the manual export skiped the last level of the tile pyramid, thus evading the 655300 pixel limitation.
Title: Re: exportRaster with jpeg compression
Post by: darkl1ght on October 30, 2020, 09:25:26 AM
Any updates on this?
Title: Re: exportRaster with jpeg compression
Post by: saduka on November 04, 2020, 10:31:20 AM
Any updates on this?
Hi:
If fine resolution is not really in consideration, coarsing the resolution is a solution.
Just check if the orthomosaic dimension is oversized for jpg compressor or not.

Code: [Select]
psize = chunk.orthomosaic.resolution
mapdim = [chunk.orthomosaic.width,  chunk.orthomosaic.height]
if max(mapdim)>65500:
                #define a new variable just in case the original resolution is needed
                psize_r = max(mapdim)/65500*psize
            else:
                psize_r = psize
chunk.exportRaster('.tif', resolution = psize)