libvips / pyvips

python binding for libvips using cffi
MIT License
640 stars 49 forks source link

Save image to tif #289

Open SikangSHU opened 2 years ago

SikangSHU commented 2 years ago

Dear Professor: I consider to save images to tif, but there is an error occured as follows. I don't know how to save. I would greatly appreciate it if you could help me. This is my code:

img = pyvips.Image.new_from_file('E:\\testpicture_jupyter\\ometif_def6.tif', access='sequential')
patch_size = 512
n_across = img.width // patch_size
n_down = img.height // patch_size
x_max = n_across - 1
y_max = n_down - 1
print(x_max, y_max)

for y in range(0, n_down):
    print("row {} ...".format(y))
    for x in range(0, n_across):
        patch = img.crop(x * patch_size, y * patch_size,
                         patch_size, patch_size)
        image = pyvips.Image.arrayjoin(patch, across=img.width)
image.tiffsave("huge.tif")
 And the error occurred is:
(wsi2.py:1468): GLib-GObject-WARNING **: 19:19:47.663: value "34102784" of type 'gint' is invalid or out of range for property 'width' of  type 'gint'
SikangSHU commented 2 years ago

Hello! I need to input the number of Colour from the command line. How can I use sys.argv and not change the format of the file? When I write the program as follows, Colour can't be editted.

gray1_1D = gray1[0]
filename = f"CD8.tif[tile,pyramid,subifd]"
print(f"writing {filename} ...")
col1 = int(sys.argv[1])
print(col1)
CD8 = (255 - gray1_1D * 255).cast("int").colourspace("grey16")
CD8.set_type(pyvips.GValue.gstr_type, "image-description",
f"""
<OME xmlns="http://www.openmicroscopy.org/Schemas/OME/2016-06"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    Creator="OME Bio-Formats 6.7.0"
    xsi:schemaLocation="http://www.openmicroscopy.org/Schemas/OME/2016-06
        http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd">
    <Image ID="Image:0">
        <!-- Minimum required fields about image dimensions -->
        <Pixels DimensionOrder="XYCZT"
                ID="Pixels:0"
                Interleaved="false"
                SizeC="1"
                SizeT="1"
                SizeX="66607"
                SizeY="53298"
                SizeZ="1"
                Type="uint16">
        <Channel Color=col1 ID="Channel:0:0" Name="Channel 1" SamplesPerPixel="1"><LightPath/></Channel><TiffData FirstC="0" FirstT="0" FirstZ="0" IFD="0" PlaneCount="1">
        </TiffData>
        </Pixels>
    </Image>
</OME>
""")
CD8.tiffsave('E:\\CD8.tif', tile=True, pyramid=True, subifd=True)
(learn) C:\Users>python E:/Learning/3c_pyvips.py -16718848

Oh, sorry. I missed a space. I haved solved the problem. Thank you!

gray1_1D = gray1[0]
filename = f"CD8.tif[tile,pyramid,subifd]"
print(f"writing {filename} ...")
print(sys.argv[1])
colour1 = sys.argv[1]
CD8 = (255 - gray1_1D * 255).cast("int").colourspace("grey16")
CD8.set_type(pyvips.GValue.gstr_type, "image-description",
f"""
<OME xmlns="http://www.openmicroscopy.org/Schemas/OME/2016-06"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    Creator="OME Bio-Formats 6.7.0"
    xsi:schemaLocation="http://www.openmicroscopy.org/Schemas/OME/2016-06
        http://www.openmicroscopy.org/Schemas/OME/2016-06/ome.xsd">
    <Image ID="Image:0">
        <!-- Minimum required fields about image dimensions -->
        <Pixels DimensionOrder="XYCZT"
                ID="Pixels:0"
                Interleaved="false"
                SizeC="1"
                SizeT="1"
                SizeX="66607"
                SizeY="53298"
                SizeZ="1"
                Type="uint16">
        <Channel Color="-"""+str(colour1)+"""" ID="Channel:0:0" Name="Channel 1" SamplesPerPixel="1"><LightPath/></Channel><TiffData FirstC="0" FirstT="0" FirstZ="0" IFD="0" PlaneCount="1">
        </TiffData>
        </Pixels>
    </Image>
</OME>
""")
CD8.tiffsave('E:\\CD82.tif', tile=True, pyramid=True, subifd=True)