Python 3.x 创建32位图像并另存为tif

Python 3.x 创建32位图像并另存为tif,python-3.x,matplotlib,python-imaging-library,tiff,Python 3.x,Matplotlib,Python Imaging Library,Tiff,这是一个非常基本的问题,但我似乎没有找到一个很好的解决办法。我想创建一个尺寸为244 X 244的黑色(全零)32位图像,并将其另存为tif。我尝试了一些模块,如PIL,但我得到的只是一个单通道RGB图像。有什么建议吗?有链接吗? 感谢您的帮助,如果问题太简单,请道歉 希望这将有助于: #!/usr/local/bin/python3 import numpy as np from PIL import Image # Numpy array containing 244x244 solid

这是一个非常基本的问题,但我似乎没有找到一个很好的解决办法。我想创建一个尺寸为244 X 244的黑色(全零)32位图像,并将其另存为tif。我尝试了一些模块,如PIL,但我得到的只是一个单通道RGB图像。有什么建议吗?有链接吗?
感谢您的帮助,如果问题太简单,请道歉

希望这将有助于:

#!/usr/local/bin/python3
import numpy as np
from PIL import Image

# Numpy array containing 244x244 solid black image
solidBlackImage=np.zeros([244,244,3],dtype=np.uint8)

img=Image.fromarray(solidBlackImage,mode="RGB")
img.save("result.tif")
我得到的图像可以用ImageMagick进行如下检查,并被视为24位图像:

identify -verbose result.tif | more
输出

Image: result.tif
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 244x244+0+0
  Units: PixelsPerInch
  Colorspace: sRGB
  Type: Bilevel
  Base type: TrueColor
  Endianess: LSB
  Depth: 8/1-bit
  Channel depth:
    Red: 1-bit
    Green: 1-bit
    Blue: 1-bit
    ...
    ...
TIFF Directory at offset 0x8 (8)
  Image Width: 244 Image Length: 244
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Samples/Pixel: 3
  Rows/Strip: 244
  Planar Configuration: single image plane
TIFF Directory at offset 0x3ee (1006)
  Image Width: 244 Image Length: 244
  Resolution: 10, 10 pixels/cm
  Bits/Sample: 8
  Sample Format: unsigned integer
  Compression Scheme: LZW
  Photometric Interpretation: RGB color
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 3
  Rows/Strip: 128
  Planar Configuration: single image plane
  Predictor: horizontal differencing 2 (0x2)
或者,您可以使用
tiffinfo
验证:

tiffinfo result.tif 
tiffinfo result.tif 
输出

Image: result.tif
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 244x244+0+0
  Units: PixelsPerInch
  Colorspace: sRGB
  Type: Bilevel
  Base type: TrueColor
  Endianess: LSB
  Depth: 8/1-bit
  Channel depth:
    Red: 1-bit
    Green: 1-bit
    Blue: 1-bit
    ...
    ...
TIFF Directory at offset 0x8 (8)
  Image Width: 244 Image Length: 244
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Samples/Pixel: 3
  Rows/Strip: 244
  Planar Configuration: single image plane
TIFF Directory at offset 0x3ee (1006)
  Image Width: 244 Image Length: 244
  Resolution: 10, 10 pixels/cm
  Bits/Sample: 8
  Sample Format: unsigned integer
  Compression Scheme: LZW
  Photometric Interpretation: RGB color
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 3
  Rows/Strip: 128
  Planar Configuration: single image plane
  Predictor: horizontal differencing 2 (0x2)

另一个选项可能是
pyvips
,如下所示,其中我还可以指定LZW压缩:

#!/usr/local/bin/python3
import numpy as np
import pyvips

width,height,bands=244,244,3

# Numpy array containing 244x244 solid black image
solidBlackImage=np.zeros([height,width,bands],dtype=np.uint8)

# Convert numpy to vips image and save with LZW compression
vi = pyvips.Image.new_from_memory(solidBlackImage.ravel(), width, height, bands,'uchar')
vi.write_to_file('result.tif',compression='lzw')
其结果是:

输出

Image: result.tif
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 244x244+0+0
  Units: PixelsPerInch
  Colorspace: sRGB
  Type: Bilevel
  Base type: TrueColor
  Endianess: LSB
  Depth: 8/1-bit
  Channel depth:
    Red: 1-bit
    Green: 1-bit
    Blue: 1-bit
    ...
    ...
TIFF Directory at offset 0x8 (8)
  Image Width: 244 Image Length: 244
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: RGB color
  Samples/Pixel: 3
  Rows/Strip: 244
  Planar Configuration: single image plane
TIFF Directory at offset 0x3ee (1006)
  Image Width: 244 Image Length: 244
  Resolution: 10, 10 pixels/cm
  Bits/Sample: 8
  Sample Format: unsigned integer
  Compression Scheme: LZW
  Photometric Interpretation: RGB color
  Orientation: row 0 top, col 0 lhs
  Samples/Pixel: 3
  Rows/Strip: 128
  Planar Configuration: single image plane
  Predictor: horizontal differencing 2 (0x2)

“单通道RGB图像”是什么意思?是单通道图像还是RGB图像?它不能同时是单通道和3通道的RGB数据。请按照StackOverflow的要求显示最小、完整和可验证的代码-这样可以更容易地测试和帮助您。谢谢。如果你想要RGB图像,它可能不是32位的。它可能是24位的,有3个通道,每个通道8位。32位图像通常为RGBA,具有4个通道,即RGB+alpha。