GithubHelp home page GithubHelp logo

xisf's People

Contributors

jpaana avatar sergio-dr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

xisf's Issues

Deserialization fails with new xisf files after latest Pixinsight update

Hi, we have been using your xisf library in our gradient removal software GraXpert for quite some time and I want to thank you for your work! After the latest Pixinsight update which changed the way how astrometric metadata is stored in the xisf file, users report that the deserialization fails.

2024-01-16 06:55:31,778 MainProcess root INFO     Unsupported Property type F64Vector: <Element '{http://www.pixinsight.com/xisf}Property' at 0x29edb36f0>
2024-01-16 06:55:31,890 MainProcess root ERROR    An error occurred while loading your picture.
Traceback (most recent call last):
  File "graxpert/application/app.py", line 223, in on_load_image
  File "graxpert/astroimage.py", line 49, in set_from_file
  File "xisf.py", line 134, in __init__
  File "xisf.py", line 156, in _read
  File "xisf.py", line 186, in _analyze_header
  File "xisf.py", line 189, in <dictcomp>
  File "xisf.py", line 607, in _process_property
  File "ast.py", line 110, in literal_eval
  File "ast.py", line 109, in _convert
  File "ast.py", line 83, in _convert_signed_num
  File "ast.py", line 74, in _convert_num
  File "ast.py", line 71, in _raise_malformed_node
ValueError: malformed node or string on line 1: <ast.Name object at 0x288a678e0>

doesn't work well with PyInstaller

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))

Fits decompress to XISF

Because of the growing file Fits file sizes more RICE compression is being used. I will keep trying to write my own Python code using Astropy but have difficulty getting it to batch convert into .xisf and not loose the header information. Can you make a fits2xisf.py that works with compressed Fits images?

Thank you,
Daniel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.