sergio-dr / xisf

XISF (Extensible Image Serialization Format) Encoder/Decoder
GNU General Public License v3.0
14 stars 4 forks source link

doesn't work well with PyInstaller #5

Open Pleiode opened 6 months ago

Pleiode commented 6 months ago

When I went compile my python script to a dir with an exe : pyinstaller --onedir converter.py

Script does'nt work. I think it compiles dependencies incorrectly ?

And I don't see the xisf folder in the _internal folder in the folder compiled by pyinstaller --onedir converter.py.

Here my script (a script convert fit, tif, cr2 and xisf to .PNG)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
import asyncio
import numpy as np
from astropy.io import fits
from PIL import Image
import imageio
from xisf import XISF 

async def normalize_data(data, low_percentile=5, high_percentile=99):
    """Normalise les données d'image pour le traitement."""

    def normalize_sync(data):
        if len(data.shape) == 3 and data.shape[0] == 3:  # Supposons RGB
            data_rgb = []
            for i in range(3):  # Normalise chaque canal de couleur
                channel = data[i]
                percentile_low, percentile_high = np.percentile(
                    channel, [low_percentile, high_percentile]
                )
                data_clipped = np.clip(channel, percentile_low, percentile_high)
                data_min = data_clipped.min()
                data_max = data_clipped.max()
                normalized_channel = (
                    (data_clipped - data_min) / (data_max - data_min) * 255
                ).astype(np.uint8)
                data_rgb.append(normalized_channel)
            return np.stack(data_rgb, axis=-1)
        elif len(data.shape) > 2:
            data = data.mean(axis=0)
        percentile_low, percentile_high = np.percentile(
            data, [low_percentile, high_percentile]
        )
        data_clipped = np.clip(data, percentile_low, percentile_high)
        data_min = data_clipped.min()
        data_max = data_clipped.max()
        return ((data_clipped - data_min) / (data_max - data_min) * 255).astype(
            np.uint8
        )

    return await asyncio.to_thread(normalize_sync, data)

def save_image_as_png(image, output_path):
    """Enregistre l'image au format PNG."""
    image.save(output_path, "PNG")

def save_thumbnail(image, thumbnail_path, max_size=150):
    """Enregistre un aperçu de l'image (thumbnail) au format PNG avec une taille maximale de 200px."""
    image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
    image.save(thumbnail_path, "PNG")

async def convert_image(input_path, output_path, thumbnail_path):
    """Convertit les images FITS, RAW, TIFF, et maintenant XISF en PNG, et crée des miniatures."""
    if not os.path.exists(input_path):
        print(f"Le fichier {input_path} n'existe pas.")
        return
    try:
        if input_path.lower().endswith((".xisf")):
            xisf_image = XISF(input_path)
            data = xisf_image.read_image(0)  # Lit la première image; ajustez selon le besoin

            # Pas de normalisation pour XISF dans cet exemple pour éviter la modification des données originales
            # Mais si vous décidez de normaliser, assurez-vous que les données sont en uint8 avant de continuer
            if data.dtype != np.uint8:
                data = (255 * (data - data.min()) / (data.max() - data.min())).astype(np.uint8)

            # S'assurer que les données sont correctes pour la conversion en image PIL
            if data.ndim == 3 and data.shape[2] == 3:  # Assumer RGB pour 3 canaux
                image = Image.fromarray(data, 'RGB')
            else:
                # Pour les images en niveaux de gris ou avec des dimensions inattendues
                # On s'assure de convertir correctement les données
                if data.ndim == 3:  # Si on a un tableau 3D inattendu, prendre le premier canal
                    data = data[:, :, 0]
                image = Image.fromarray(data, 'L')

        elif input_path.lower().endswith((".fit", ".fits")):
            data = fits.getdata(input_path, memmap=False)
            data = await normalize_data(data)
            if data.ndim == 3 and data.shape[-1] == 3:  # RGB
                image = Image.fromarray(data)
            else:
                image = Image.fromarray(data, "L")
        elif input_path.lower().endswith((".tif", ".tiff", ".png", ".jpg", ".jpeg")):
            image = Image.open(input_path)
        else:
            # Essaie de traiter comme un fichier RAW
            try:
                data = imageio.imread(input_path)
                data = await normalize_data(data)
                image = Image.fromarray(data)
            except ValueError as e:
                print(f"Erreur lors de la lecture du fichier RAW: {e}")
                return
        save_image_as_png(image, output_path)
        save_thumbnail(image, thumbnail_path)
    except Exception as e:
        print(f"Erreur lors de la lecture du fichier: {e}")
        return
    finally:
        if "image" in locals():
            image.close()
    print(output_path)
    print(thumbnail_path)

async def process_files(file_paths, output_paths, thumbnail_paths):
    """Traite les fichiers d'entrée et produit les fichiers de sortie."""
    tasks = []
    for input_path, output_path, thumbnail_path in zip(
        file_paths, output_paths, thumbnail_paths
    ):
        task = asyncio.create_task(
            convert_image(input_path, output_path, thumbnail_path)
        )
        tasks.append(task)
    await asyncio.gather(*tasks)

if __name__ == "__main__":
    if len(sys.argv) < 4 or len(sys.argv) % 3 != 1:
        print(
            "Usage: script.py <path_to_file> <output_path> <thumbnail_path> [<path_to_file> <output_path> <thumbnail_path> ...]"
        )
        sys.exit(1)
    file_paths = sys.argv[1::3]
    output_paths = sys.argv[2::3]
    thumbnail_paths = sys.argv[3::3]
    asyncio.run(process_files(file_paths, output_paths, thumbnail_paths))
sergio-dr commented 6 months ago

Hi Pleiode, I don't have any experience with pyinstaller. It seems that pyinstaller --onefile --copy-metadata xisf convert.py does the trick for me (on Windows 11), it builds the exe file and it runs without errors. (But the executable file is huge, at least in my case. Can't assist you further on that)