i want to open a multilayer TIFF in Photoshop, or convert Tiff file to PSD file using python only

51 Views Asked by At

Basically i have created a tiff multilayered file in python using pillow library it is having 8 layers basically tiff is created successfully it is even getting open in normal windows image viewer and i can see all the layers but when i open that tiff file in photoshop it shows only first layer other layers are not visible in photoshop then i use online tool photopea to open that tiff file so there were all the layers and it was like what i am looking for but my requirement is to open file in photoshop with layers so i just convert that file to psd file using the same tool and it get opens in photoshop as layered.

import cv2
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import KMeans

# Load the image using OpenCV
image = cv2.imread("Testing_samples/test5.jpg", cv2.IMREAD_COLOR)

# Convert BGR to RGB
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Reshape the array for clustering
x = image_rgb.reshape(-1, 3)

# Apply KMeans clustering
kmeans = KMeans(n_clusters=4).fit(x)
segmented_image = kmeans.cluster_centers_[kmeans.labels_]
segmented_image = segmented_image.reshape(image_rgb.shape)

num_clusters = 4

# Create a directory to save the TIFF file
tiff_dir = "Testing_Tiff"
os.makedirs(tiff_dir, exist_ok=True)

# Create an empty list to store segmented layer images
layer_images = []

# Plot each segmented color cluster
for i in range(1, num_clusters + 1):
    boolean_index = kmeans.labels_ == (i - 1)

    if boolean_index.sum() > 0:
        segmented_layer = np.zeros_like(image, dtype=np.uint8)

        mask = np.zeros(image.shape[:2], dtype=bool)
        mask.flat[boolean_index] = True

        for channel in range(image.shape[-1]):
            segmented_layer[:, :, channel][mask] = kmeans.cluster_centers_[i - 1][channel]

        # Add transparency channel
        alpha_channel = np.zeros_like(image[:, :, 0], dtype=np.uint8)
        alpha_channel[mask] = 255  # Set pixels in the segment to fully opaque
        segmented_layer = np.dstack([segmented_layer, alpha_channel])

        # Convert the segmented layer to a PIL Image
        pil_layer = Image.fromarray(segmented_layer, 'RGBA')

        # Append the segmented layer image to the list
        layer_images.append(pil_layer)

# Save the list of layer images as a single TIFF file with layers
layered_tiff_path = os.path.join(tiff_dir, "test_4_16_layers.tiff")
layer_images[0].save(layered_tiff_path, save_all=True, append_images=layer_images[1:], format="TIFF")

print(f"Segmented layers TIFF file saved: {layered_tiff_path}")

so from the code you can see i have created one Tiff file using python code now i want to convert this file into PSD file using python because when i open this Tiff layered file in photoshop then only the top layer is getting open rest of them are not showing but in normal windows viewer i can see the layers so i hope now you got my point

Thank you.

0

There are 0 best solutions below