在 BigTiff 中用白色背景替换透明度
Replace transparency with whitebackground in BigTiff
我正在使用 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时,我只实现了黑色背景。
感谢您的帮助。
如果您检查 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
而且有效。
我正在使用 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时,我只实现了黑色背景。
感谢您的帮助。
如果您检查 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
而且有效。