【问题标题】:Replace transparency with whitebackground in BigTiff在 BigTiff 中用白色背景替换透明度
【发布时间】:2019-01-03 06:35:50
【问题描述】:

我正在使用 python 将 RGBA tiff 转换为具有白色背景的 RGB tiff。

我正在使用 ImageMagick 和 GDAL 库。

我的代码是这样的:

def add_background_to_rgba_geotiff(source, destination):

convert_rgba_to_rgb_tif(source, destination)
add_metadata_to_new_geotiff_file(source, destination)


def convert_rgba_to_rgb_tif(source, destination):
    # work also with BigTiff
    command = ' '.join(['convert', quote(source),
                        '-background', 'white',
                        '-alpha', 'background',
                        '-alpha', 'off', quote(destination)])
    shell_command.execute_and_log_outputs(command, shell=True)


def add_metadata_to_new_geotiff_file(source, destination):
    RGBA_tif = gdal.Open(source, gdalconst.GA_ReadOnly)
    RGB_tif = gdal.Open(destination, gdalconst.GA_Update)

    RGB_tif.SetMetadata(RGBA_tif.GetMetadata())
    RGB_tif.SetGeoTransform(RGBA_tif.GetGeoTransform())
    RGB_tif.SetProjection(RGBA_tif.GetProjection())

    del (RGBA_tif)
    del (RGB_tif)

def execute_and_log_outputs(command, silence_errors=False, **kwargs):
    shell_process = execute_async(command,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE,
                                  **kwargs)

     (out, err) = shell_process.communicate()

当我尝试使用 tiff 运行我的代码时,一切正常,但是当我想在 BigTIFF (>4GB) 上应用它时,它会失败并出现以下错误:

TIFFWriteDirectoryTagData:超过最大 TIFF 文件大小。

有人知道如何将 BigTiff 与 ImageMagick 一起使用吗? 或者可以用GDAL做到这一点吗?在我尝试使用 GDAL 的那一刻,我只实现了黑色背景。

感谢您的帮助。

【问题讨论】:

  • 尝试像这样在输出文件的前面强制使用 BigTIFF delgate...convert input.tiff ... TIFF64:BigBoy.tif

标签: python image image-processing imagemagick gdal


【解决方案1】:

如果您检查 ImageMagick 知道的可用格式,您将看到 TIFF64:

identify -list format | grep -i tiff

      EPT  EPT       rw-   Encapsulated PostScript with TIFF preview
     EPT2  EPT       rw-   Encapsulated PostScript Level II with TIFF preview
     EPT3  EPT       rw+   Encapsulated PostScript Level III with TIFF preview
   GROUP4* TIFF      rw-   Raw CCITT Group4
     PTIF* TIFF      rw+   Pyramid encoded TIFF
     TIFF* TIFF      rw+   Tagged Image File Format (LIBTIFF, Version 4.0.9)
   TIFF64* TIFF      rw-   Tagged Image File Format (64-bit) (LIBTIFF, Version 4.0.9)

如果您尝试制作这样的 TIFF,它会失败:

convert -size 20000x50000 xc:red +noise random a.tif
convert: Maximum TIFF file size exceeded. `TIFFAppendToStrip' @ error/tiff.c/TIFFErrors/652.

因此,通过将 TIFF64 委托附加到输出文件名来强制获得这样的 BigTIFF:

convert -size 20000x50000 xc:red +noise random TIFF64:a.tif

它有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-03
    • 2012-09-07
    • 2014-08-21
    • 2013-04-03
    • 1970-01-01
    • 2011-03-31
    • 2013-12-15
    • 2018-03-25
    相关资源
    最近更新 更多