How to create and load TIFF images from klayout

Hello Mathias,

I have tried to create an image file with the TIFF format and open from the Klayout GUI by Edit > Add Image, but it fails with the message "No data loaded for that image". The TIFF image is created by the PILLOW module as shown below.

def save_sdf_as_tiff(sdf_array, grid_bbox_dbu, dbu, resolution, output_dir="."):
"""Saves the high-precision SDF data as a floating-point TIFF."""
if sdf_array is None: return
center_x_um = grid_bbox_dbu.center().x * dbu
center_y_um = grid_bbox_dbu.center().y * dbu
base_filename = f"sdf_data_center_{center_x_um:.3f}{center_y_um:.3f}_pixelsize{resolution:.3f}"
tiff_filepath = os.path.join(output_dir, f"{base_filename}.tif")
print(f"-> Saving high-precision SDF data to '{tiff_filepath}'...")
img = Image.fromarray(sdf_array.astype(np.float32))
img.save(tiff_filepath, compression='tiff_uncompressed')
print("-> Data TIFF file saved.")

where "Image" is from PIL. I have tried to load the save the image by another image viewer, then it succeeded. Is there a Caveat that I have missed in creating and loading the TIFF image?

Comments

  • Personally I use .png and TIFF is about as fat
    as an image file can be. But I only do interactive.
    That all seems orderly and works fine (though I
    could ask for "persistent background image" to
    not have to re-attach every time).

    What I do not know, is whether klayout can "create"
    a TIFF image, again I only export to .png. "Create"
    from what?

    Need a bit more insight than "failed". Determining
    -why- "No data loaded for that image" to start.
    No file, bad file, file invoked by relative path with
    no searchpath entry that's appropriate, etc.

  • TIFF is not installed with Qt by default.

    In my case (Ubuntu 24.04), I had to install this package:

    sudo apt install qt5-image-formats-plugins
    

    After this, I was able to use TIFF images.

    Matthias

  • edited July 28

    @Matthias : Thanks for your instruction. With the qt plug-in installed, I was able to load a TIFF image but realized that it is limited to a low-bit images. (8-bit was ok but not with a 32/64 bit). With that have, I have determined to save the image for a visualization as the png format while the high-precision image for an engineering is saved as TIFF format, separately.

    @dick_freebird : I was working for generating the signed distance function (SDF) for a given geometry. Because the SDF images will be used for an ML training, it requires high precision and some metadata such as origin coordinates and pixel size, which drives me to consider TIFF format. I was looking for a way of loading the high-precision TIFF image directly from the Klayout not saving the preview images separately.

    Thanks,
    Moongyu

Sign In or Register to comment.