Example #1
0
def main(in_dir, name):
    path_match = "*.fits.fz"
    img_list = sorted(glob(os.path.join(in_dir, path_match)))
    img_list = reproject_files(img_list[0], img_list, in_dir)
    img_list = write_clean_data(img_list)
    img_list = sort_files_for_colour(img_list)
    fits_to_jpg(img_list,
                os.path.join(in_dir, name),
                width=1000,
                height=1000,
                color=True)
    return
Example #2
0
    def create_thumbnail(self, width=None, height=None):
        """
        Creates a thumbnail image of this data product (if it is a valid FITS image file) with specified width and
        height, or the original width and height if none is specified.

        :Keyword Arguments:
            * width (`int`): Desired width of the thumbnail
            * height (`int`): Desired height of the thumbnail

        :returns: Thumbnail file if created, None otherwise
        :rtype: file
        """
        if is_fits_image_file(self.data.file):
            tmpfile = tempfile.NamedTemporaryFile(suffix='.jpg')
            try:
                if not width or not height:
                    width, height = find_fits_img_size(self.data.file)
                resp = fits_to_jpg(self.data.file,
                                   tmpfile.name,
                                   width=width,
                                   height=height)
                if resp:
                    return tmpfile
            except Exception as e:
                logger.warn(f'Unable to create thumbnail for {self}: {e}')
        return
Example #3
0
 def create_thumbnail(self, width=None, height=None):
     if is_fits_image_file(self.data):
         tmpfile = tempfile.NamedTemporaryFile()
         if not width or not height:
             width, height = find_img_size(self.data.file.name)
         resp = fits_to_jpg(self.data.file.name,
                            tmpfile.name,
                            width=width,
                            height=height)
         if resp:
             return tmpfile
     return
Example #4
0
def convert_to_jpg(paths, key, **params):
    jpg_path = f'{unique_temp_path_start()}{key}'
    fits_to_jpg(paths, jpg_path, **params)
    return jpg_path
Example #5
0
    def write_timelapse(self,
                        outfile,
                        fmt=TIMELAPSE_GIF,
                        fps=10,
                        image_size=500,
                        **flags):
        """
        Write the timelapse to the given output file, which may be a path or
        file-like object
        """
        writer_kwargs = {'format': fmt, 'mode': 'I', 'fps': fps}

        # When saving to MP4 or WebM, imageio uses ffmpeg, which determines
        # output format from file extension. When using a BytesIO buffer,
        # imageio creates a temporary file with no extension, so the ffmpeg
        # call fails. We need to specify the output format explicitly instead
        # in this case
        if fmt in (TIMELAPSE_MP4, TIMELAPSE_WEBM):
            writer_kwargs['output_params'] = [
                '-f', fmt, '-crf', '30', '-lossless', '1'
            ]

            # Need to specify codec for WebM
            if fmt == TIMELAPSE_WEBM:
                writer_kwargs['codec'] = 'vp8'

            # The imageio plugin does not recognise webm as a format, so set
            # 'format' to 'mp4' in either case (this does not affect the ffmpeg
            # call)
            writer_kwargs['format'] = TIMELAPSE_MP4

        num_frames = self.input_files.count()

        with tempfile.TemporaryDirectory() as tmpdir:
            with imageio.get_writer(outfile, **writer_kwargs) as writer:
                self.log('Sorting frames')
                for i, product in enumerate(self.sorted_frames()):
                    self.log(f'Processing frame {i + 1}/{num_frames}')

                    fits_path = product.data.file
                    if flags.get('normalise_background') or flags.get('crop'):
                        fits_path.open()
                        data, header = fits.getdata(fits_path, header=True)
                        fits_path.close()
                        try:
                            if flags.get('crop'):
                                scale = self.get_settings().get(
                                    'crop_scale', 0.5)
                                data, header = crop_image(data, header, scale)
                            if flags.get('normalise_background'):
                                data = normalise_background(data, header)

                        except ValueError as ex:
                            raise AsyncError("Error in file '{}': {}".format(
                                product.data.name, ex))
                        fits_path = os.path.join(tmpdir, 'tmp.fits')
                        hdul = fits.HDUList(
                            [fits.PrimaryHDU(data, header=header)])
                        with open(fits_path, 'wb') as f:
                            hdul.writeto(f)

                    # Note: imageio supports loading FITS files, but does not
                    # choose brightness levels intelligently. Use fits_to_jpg
                    # instead to go FITS -> JPG -> timelapse
                    jpg_path = os.path.join(tmpdir, 'frame_{}.jpg'.format(i))
                    fits_to_jpg(fits_path,
                                jpg_path,
                                width=image_size,
                                height=image_size)
                    writer.append_data(imageio.imread(jpg_path))

        self.log('Finished')