<s> '''<STR_LIT>''' <EOL> import logging <EOL> import os <EOL> import re <EOL> import sys <EOL> import traceback <EOL> import matplotlib <EOL> import numpy as np <EOL> import scipy . io . matlab . mio <EOL> logger = logging . getLogger ( __name__ ) <EOL> import cellprofiler . cpmodule as cpm <EOL> import cellprofiler . measurements as cpmeas <EOL> import cellprofiler . settings as cps <EOL> from cellprofiler . settings import YES , NO <EOL> import cellprofiler . preferences as cpp <EOL> from cellprofiler . gui . help import USING_METADATA_TAGS_REF , USING_METADATA_HELP_REF <EOL> from cellprofiler . preferences import standardize_default_folder_names , DEFAULT_INPUT_FOLDER_NAME , DEFAULT_OUTPUT_FOLDER_NAME , ABSOLUTE_FOLDER_NAME , DEFAULT_INPUT_SUBFOLDER_NAME , DEFAULT_OUTPUT_SUBFOLDER_NAME , IO_FOLDER_CHOICE_HELP_TEXT , IO_WITH_METADATA_HELP_TEXT , get_default_image_directory <EOL> import os . path <EOL> from cellprofiler . modules . loadimages import C_FILE_NAME , C_PATH_NAME , C_URL <EOL> from cellprofiler . modules . loadimages import C_OBJECTS_FILE_NAME , C_OBJECTS_PATH_NAME , C_OBJECTS_URL <EOL> from cellprofiler . modules . loadimages import pathname2url <EOL> from centrosome . cpmorphology import distance_color_labels <EOL> from cellprofiler . utilities . version import get_version <EOL> from bioformats . formatwriter import write_image <EOL> import bioformats . omexml as ome <EOL> IF_IMAGE = "<STR_LIT>" <EOL> IF_MASK = "<STR_LIT>" <EOL> IF_CROPPING = "<STR_LIT>" <EOL> IF_FIGURE = "<STR_LIT>" <EOL> IF_MOVIE = "<STR_LIT>" <EOL> IF_OBJECTS = "<STR_LIT>" <EOL> IF_ALL = [ IF_IMAGE , IF_MASK , IF_CROPPING , IF_MOVIE , IF_OBJECTS ] <EOL> OLD_BIT_DEPTH_8 = "<STR_LIT>" <EOL> OLD_BIT_DEPTH_16 = "<STR_LIT>" <EOL> BIT_DEPTH_8 = "<STR_LIT>" <EOL> BIT_DEPTH_16 = "<STR_LIT>" <EOL> BIT_DEPTH_FLOAT = "<STR_LIT>" <EOL> FN_FROM_IMAGE = "<STR_LIT>" <EOL> FN_SEQUENTIAL = "<STR_LIT>" <EOL> FN_SINGLE_NAME = "<STR_LIT>" <EOL> SINGLE_NAME_TEXT = "<STR_LIT>" <EOL> FN_WITH_METADATA = "<STR_LIT>" <EOL> FN_IMAGE_FILENAME_WITH_METADATA = "<STR_LIT>" <EOL> METADATA_NAME_TEXT = """<STR_LIT>""" <EOL> SEQUENTIAL_NUMBER_TEXT = "<STR_LIT>" <EOL> FF_BMP = "<STR_LIT>" <EOL> FF_JPG = "<STR_LIT>" <EOL> FF_JPEG = "<STR_LIT>" <EOL> FF_PBM = "<STR_LIT>" <EOL> FF_PCX = "<STR_LIT>" <EOL> FF_PGM = "<STR_LIT>" <EOL> FF_PNG = "<STR_LIT>" <EOL> FF_PNM = "<STR_LIT>" <EOL> FF_PPM = "<STR_LIT>" <EOL> FF_RAS = "<STR_LIT>" <EOL> FF_TIF = "<STR_LIT>" <EOL> FF_TIFF = "<STR_LIT>" <EOL> FF_XWD = "<STR_LIT>" <EOL> FF_AVI = "<STR_LIT>" <EOL> FF_MAT = "<STR_LIT>" <EOL> FF_MOV = "<STR_LIT>" <EOL> FF_SUPPORTING_16_BIT = [ FF_TIF , FF_TIFF ] <EOL> PC_WITH_IMAGE = "<STR_LIT>" <EOL> OLD_PC_WITH_IMAGE_VALUES = [ "<STR_LIT>" ] <EOL> PC_CUSTOM = "<STR_LIT>" <EOL> PC_WITH_METADATA = "<STR_LIT>" <EOL> WS_EVERY_CYCLE = "<STR_LIT>" <EOL> WS_FIRST_CYCLE = "<STR_LIT>" <EOL> WS_LAST_CYCLE = "<STR_LIT>" <EOL> CM_GRAY = "<STR_LIT>" <EOL> GC_GRAYSCALE = "<STR_LIT>" <EOL> GC_COLOR = "<STR_LIT>" <EOL> '''<STR_LIT>''' <EOL> OFFSET_DIRECTORY_PATH = <NUM_LIT:11> <EOL> '''<STR_LIT>''' <EOL> OFFSET_BIT_DEPTH_V11 = <NUM_LIT:12> <EOL> class SaveImages ( cpm . CPModule ) : <EOL> module_name = "<STR_LIT>" <EOL> variable_revision_number = <NUM_LIT:11> <EOL> category = "<STR_LIT>" <EOL> def create_settings ( self ) : <EOL> self . save_image_or_figure = cps . Choice ( <EOL> "<STR_LIT>" , <EOL> IF_ALL , <EOL> IF_IMAGE , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . image_name = cps . ImageNameSubscriber ( <EOL> "<STR_LIT>" , cps . NONE , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . objects_name = cps . ObjectNameSubscriber ( <EOL> "<STR_LIT>" , cps . NONE , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . figure_name = cps . FigureSubscriber ( <EOL> "<STR_LIT>" , cps . NONE , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . file_name_method = cps . Choice ( <EOL> "<STR_LIT>" , <EOL> [ FN_FROM_IMAGE , FN_SEQUENTIAL , <EOL> FN_SINGLE_NAME ] , <EOL> FN_FROM_IMAGE , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . file_image_name = cps . FileImageNameSubscriber ( <EOL> "<STR_LIT>" , <EOL> cps . NONE , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . single_file_name = cps . Text ( <EOL> SINGLE_NAME_TEXT , "<STR_LIT>" , <EOL> metadata = True , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . number_of_digits = cps . Integer ( <EOL> "<STR_LIT>" , <NUM_LIT:4> , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . wants_file_name_suffix = cps . Binary ( <EOL> "<STR_LIT>" , False , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . file_name_suffix = cps . Text ( <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , metadata = True , doc = """<STR_LIT>""" ) <EOL> self . file_format = cps . Choice ( <EOL> "<STR_LIT>" , <EOL> [ FF_BMP , FF_JPG , FF_JPEG , FF_PNG , FF_TIF , FF_TIFF , FF_MAT ] , <EOL> value = FF_TIF , doc = """<STR_LIT>""" ) <EOL> self . movie_format = cps . Choice ( <EOL> "<STR_LIT>" , <EOL> [ FF_AVI , FF_TIF , FF_MOV ] , <EOL> value = FF_AVI , doc = """<STR_LIT>""" ) <EOL> self . pathname = SaveImagesDirectoryPath ( <EOL> "<STR_LIT>" , self . file_image_name , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . bit_depth = cps . Choice ( <EOL> "<STR_LIT>" , <EOL> [ BIT_DEPTH_8 , BIT_DEPTH_16 , BIT_DEPTH_FLOAT ] , doc = """<STR_LIT>""" % <EOL> globals ( ) ) <EOL> self . overwrite = cps . Binary ( <EOL> "<STR_LIT>" , False , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . when_to_save = cps . Choice ( <EOL> "<STR_LIT>" , <EOL> [ WS_EVERY_CYCLE , WS_FIRST_CYCLE , WS_LAST_CYCLE ] , <EOL> WS_EVERY_CYCLE , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . rescale = cps . Binary ( <EOL> "<STR_LIT>" , False , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . gray_or_color = cps . Choice ( <EOL> "<STR_LIT>" , <EOL> [ GC_GRAYSCALE , GC_COLOR ] , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . colormap = cps . Colormap ( <EOL> '<STR_LIT>' , <EOL> value = CM_GRAY , doc = """<STR_LIT>""" ) <EOL> self . update_file_names = cps . Binary ( <EOL> "<STR_LIT>" , False , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . create_subdirectories = cps . Binary ( <EOL> "<STR_LIT>" , False , doc = """<STR_LIT>""" % globals ( ) ) <EOL> self . root_dir = cps . DirectoryPath ( <EOL> "<STR_LIT>" , doc = """<STR_LIT>""" % dict ( sep = os . path . sep ) ) <EOL> def settings ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . save_image_or_figure , self . image_name , <EOL> self . objects_name , self . figure_name , <EOL> self . file_name_method , self . file_image_name , <EOL> self . single_file_name , self . number_of_digits , <EOL> self . wants_file_name_suffix , <EOL> self . file_name_suffix , self . file_format , <EOL> self . pathname , self . bit_depth , <EOL> self . overwrite , self . when_to_save , <EOL> self . rescale , self . gray_or_color , self . colormap , <EOL> self . update_file_names , self . create_subdirectories , <EOL> self . root_dir , self . movie_format ] <EOL> def visible_settings ( self ) : <EOL> """<STR_LIT>""" <EOL> result = [ self . save_image_or_figure ] <EOL> if self . save_image_or_figure == IF_FIGURE : <EOL> result . append ( self . figure_name ) <EOL> elif self . save_image_or_figure == IF_OBJECTS : <EOL> result . append ( self . objects_name ) <EOL> else : <EOL> result . append ( self . image_name ) <EOL> result . append ( self . file_name_method ) <EOL> if self . file_name_method == FN_FROM_IMAGE : <EOL> result += [ self . file_image_name , self . wants_file_name_suffix ] <EOL> if self . wants_file_name_suffix : <EOL> result . append ( self . file_name_suffix ) <EOL> elif self . file_name_method == FN_SEQUENTIAL : <EOL> self . single_file_name . text = SEQUENTIAL_NUMBER_TEXT <EOL> result . append ( self . single_file_name ) <EOL> result . append ( self . number_of_digits ) <EOL> elif self . file_name_method == FN_SINGLE_NAME : <EOL> self . single_file_name . text = SINGLE_NAME_TEXT <EOL> result . append ( self . single_file_name ) <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" % self . file_name_method ) <EOL> if self . save_image_or_figure == IF_MOVIE : <EOL> result . append ( self . movie_format ) <EOL> else : <EOL> result . append ( self . file_format ) <EOL> supports_16_bit = ( self . file_format in FF_SUPPORTING_16_BIT and <EOL> self . save_image_or_figure == IF_IMAGE ) <EOL> if supports_16_bit : <EOL> result . append ( self . bit_depth ) <EOL> result . append ( self . pathname ) <EOL> result . append ( self . overwrite ) <EOL> if self . save_image_or_figure != IF_MOVIE : <EOL> result . append ( self . when_to_save ) <EOL> if ( self . save_image_or_figure == IF_IMAGE and <EOL> self . file_format != FF_MAT ) : <EOL> result . append ( self . rescale ) <EOL> if self . get_bit_depth ( ) == "<STR_LIT>" : <EOL> result . append ( self . colormap ) <EOL> elif self . save_image_or_figure == IF_OBJECTS : <EOL> result . append ( self . gray_or_color ) <EOL> if self . gray_or_color == GC_COLOR : <EOL> result . append ( self . colormap ) <EOL> result . append ( self . update_file_names ) <EOL> if self . file_name_method == FN_FROM_IMAGE : <EOL> result . append ( self . create_subdirectories ) <EOL> if self . create_subdirectories : <EOL> result . append ( self . root_dir ) <EOL> return result <EOL> @ property <EOL> def module_key ( self ) : <EOL> return "<STR_LIT>" % ( self . module_name , self . module_num ) <EOL> def prepare_group ( self , workspace , grouping , image_numbers ) : <EOL> d = self . get_dictionary ( workspace . image_set_list ) <EOL> if self . save_image_or_figure == IF_MOVIE : <EOL> d [ '<STR_LIT>' ] = len ( image_numbers ) <EOL> d [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> return True <EOL> def prepare_to_create_batch ( self , workspace , fn_alter_path ) : <EOL> self . pathname . alter_for_create_batch_files ( fn_alter_path ) <EOL> if self . create_subdirectories : <EOL> self . root_dir . alter_for_create_batch_files ( fn_alter_path ) <EOL> def run ( self , workspace ) : <EOL> """<STR_LIT>""" <EOL> if self . save_image_or_figure . value in ( IF_IMAGE , IF_MASK , IF_CROPPING ) : <EOL> should_save = self . run_image ( workspace ) <EOL> elif self . save_image_or_figure == IF_MOVIE : <EOL> should_save = self . run_movie ( workspace ) <EOL> elif self . save_image_or_figure == IF_OBJECTS : <EOL> should_save = self . run_objects ( workspace ) <EOL> else : <EOL> raise NotImplementedError ( ( "<STR_LIT>" % <EOL> self . save_image_or_figure ) ) <EOL> workspace . display_data . filename = self . get_filename ( <EOL> workspace , make_dirs = False , check_overwrite = False ) <EOL> def is_aggregation_module ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . save_image_or_figure == IF_MOVIE or self . when_to_save == WS_LAST_CYCLE <EOL> def display ( self , workspace , figure ) : <EOL> if self . show_window : <EOL> if self . save_image_or_figure == IF_MOVIE : <EOL> return <EOL> figure . set_subplots ( ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> outcome = ( "<STR_LIT>" if workspace . display_data . wrote_image <EOL> else "<STR_LIT>" ) <EOL> figure . subplot_table ( <NUM_LIT:0> , <NUM_LIT:0> , [ [ outcome % <EOL> workspace . display_data . filename ] ] ) <EOL> def run_image ( self , workspace ) : <EOL> """<STR_LIT>""" <EOL> if self . when_to_save == WS_FIRST_CYCLE : <EOL> d = self . get_dictionary ( workspace . image_set_list ) <EOL> if workspace . measurements [ cpmeas . IMAGE , cpmeas . GROUP_INDEX ] > <NUM_LIT:1> : <EOL> workspace . display_data . wrote_image = False <EOL> self . save_filename_measurements ( workspace ) <EOL> return <EOL> d [ "<STR_LIT>" ] = False <EOL> elif self . when_to_save == WS_LAST_CYCLE : <EOL> workspace . display_data . wrote_image = False <EOL> self . save_filename_measurements ( workspace ) <EOL> return <EOL> self . save_image ( workspace ) <EOL> return True <EOL> def run_movie ( self , workspace ) : <EOL> out_file = self . get_filename ( workspace , check_overwrite = False ) <EOL> d = self . get_dictionary ( workspace . image_set_list ) <EOL> if d [ "<STR_LIT>" ] == <NUM_LIT:0> and os . path . exists ( out_file ) : <EOL> if not self . check_overwrite ( out_file , workspace ) : <EOL> d [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> return <EOL> else : <EOL> os . remove ( out_file ) <EOL> elif d [ "<STR_LIT>" ] == "<STR_LIT>" : <EOL> return <EOL> image = workspace . image_set . get_image ( self . image_name . value ) <EOL> pixels = image . pixel_data <EOL> pixels = pixels * <NUM_LIT:255> <EOL> frames = d [ '<STR_LIT>' ] <EOL> current_frame = d [ "<STR_LIT>" ] <EOL> d [ "<STR_LIT>" ] += <NUM_LIT:1> <EOL> self . do_save_image ( workspace , out_file , pixels , ome . PT_UINT8 , <EOL> t = current_frame , size_t = frames ) <EOL> def run_objects ( self , workspace ) : <EOL> if self . when_to_save == WS_FIRST_CYCLE : <EOL> if workspace . measurements [ cpmeas . IMAGE , cpmeas . GROUP_INDEX ] > <NUM_LIT:1> : <EOL> workspace . display_data . wrote_image = False <EOL> self . save_filename_measurements ( workspace ) <EOL> return <EOL> elif self . when_to_save == WS_LAST_CYCLE : <EOL> workspace . display_data . wrote_image = False <EOL> self . save_filename_measurements ( workspace ) <EOL> return <EOL> self . save_objects ( workspace ) <EOL> def save_objects ( self , workspace ) : <EOL> objects_name = self . objects_name . value <EOL> objects = workspace . object_set . get_objects ( objects_name ) <EOL> filename = self . get_filename ( workspace ) <EOL> if filename is None : <EOL> return <EOL> labels = [ l for l , c in objects . get_labels ( ) ] <EOL> if self . get_file_format ( ) == FF_MAT : <EOL> pixels = objects . segmented <EOL> scipy . io . matlab . mio . savemat ( filename , { "<STR_LIT>" : pixels } , format = '<STR_LIT:5>' ) <EOL> elif self . gray_or_color == GC_GRAYSCALE : <EOL> if objects . count > <NUM_LIT:255> : <EOL> pixel_type = ome . PT_UINT16 <EOL> else : <EOL> pixel_type = ome . PT_UINT8 <EOL> for i , l in enumerate ( labels ) : <EOL> self . do_save_image ( <EOL> workspace , filename , l , pixel_type , t = i , size_t = len ( labels ) ) <EOL> else : <EOL> if self . colormap == cps . DEFAULT : <EOL> colormap = cpp . get_default_colormap ( ) <EOL> else : <EOL> colormap = self . colormap . value <EOL> cm = matplotlib . cm . get_cmap ( colormap ) <EOL> cpixels = np . zeros ( ( labels [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] , labels [ <NUM_LIT:0> ] . shape [ <NUM_LIT:1> ] , <NUM_LIT:3> ) ) <EOL> counts = np . zeros ( labels [ <NUM_LIT:0> ] . shape , int ) <EOL> mapper = matplotlib . cm . ScalarMappable ( cmap = cm ) <EOL> for pixels in labels : <EOL> cpixels [ pixels != <NUM_LIT:0> , : ] += mapper . to_rgba ( distance_color_labels ( pixels ) , <EOL> bytes = True ) [ pixels != <NUM_LIT:0> , : <NUM_LIT:3> ] <EOL> counts [ pixels != <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> counts [ counts == <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> cpixels = cpixels / counts [ : , : , np . newaxis ] <EOL> self . do_save_image ( workspace , filename , cpixels , ome . PT_UINT8 ) <EOL> self . save_filename_measurements ( workspace ) <EOL> if self . show_window : <EOL> workspace . display_data . wrote_image = True <EOL> def post_group ( self , workspace , * args ) : <EOL> if ( self . when_to_save == WS_LAST_CYCLE and <EOL> self . save_image_or_figure != IF_MOVIE ) : <EOL> if self . save_image_or_figure == IF_OBJECTS : <EOL> self . save_objects ( workspace ) <EOL> else : <EOL> self . save_image ( workspace ) <EOL> def do_save_image ( self , workspace , filename , pixels , pixel_type , <EOL> c = <NUM_LIT:0> , z = <NUM_LIT:0> , t = <NUM_LIT:0> , <EOL> size_c = <NUM_LIT:1> , size_z = <NUM_LIT:1> , size_t = <NUM_LIT:1> , <EOL> channel_names = None ) : <EOL> '''<STR_LIT>''' <EOL> write_image ( filename , pixels , pixel_type , <EOL> c = c , z = z , t = t , <EOL> size_c = size_c , size_z = size_z , size_t = size_t , <EOL> channel_names = channel_names ) <EOL> def save_image ( self , workspace ) : <EOL> if self . show_window : <EOL> workspace . display_data . wrote_image = False <EOL> image = workspace . image_set . get_image ( self . image_name . value ) <EOL> if self . save_image_or_figure == IF_IMAGE : <EOL> pixels = image . pixel_data <EOL> u16hack = ( self . get_bit_depth ( ) == BIT_DEPTH_16 and <EOL> pixels . dtype . kind in ( '<STR_LIT:u>' , '<STR_LIT:i>' ) ) <EOL> if self . file_format != FF_MAT : <EOL> if self . rescale . value : <EOL> pixels = pixels . copy ( ) <EOL> if pixels . ndim == <NUM_LIT:3> : <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> img_min = np . min ( pixels [ : , : , i ] ) <EOL> img_max = np . max ( pixels [ : , : , i ] ) <EOL> if img_max > img_min : <EOL> pixels [ : , : , i ] = ( pixels [ : , : , i ] - img_min ) / ( img_max - img_min ) <EOL> else : <EOL> img_min = np . min ( pixels ) <EOL> img_max = np . max ( pixels ) <EOL> if img_max > img_min : <EOL> pixels = ( pixels - img_min ) / ( img_max - img_min ) <EOL> elif not ( u16hack or self . get_bit_depth ( ) == BIT_DEPTH_FLOAT ) : <EOL> if np . max ( pixels ) > <NUM_LIT:1> or np . min ( pixels ) < <NUM_LIT:0> : <EOL> sys . stderr . write ( <EOL> "<STR_LIT>" % <EOL> self . image_name . value ) <EOL> pixels = pixels . copy ( ) <EOL> pixels [ pixels < <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> pixels [ pixels > <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> if pixels . ndim == <NUM_LIT:2> and self . colormap != CM_GRAY and self . get_bit_depth ( ) == BIT_DEPTH_8 : <EOL> if self . colormap == cps . DEFAULT : <EOL> colormap = cpp . get_default_colormap ( ) <EOL> else : <EOL> colormap = self . colormap . value <EOL> cm = matplotlib . cm . get_cmap ( colormap ) <EOL> mapper = matplotlib . cm . ScalarMappable ( cmap = cm ) <EOL> pixels = mapper . to_rgba ( pixels , bytes = True ) <EOL> pixel_type = ome . PT_UINT8 <EOL> elif self . get_bit_depth ( ) == BIT_DEPTH_8 : <EOL> pixels = ( pixels * <NUM_LIT:255> ) . astype ( np . uint8 ) <EOL> pixel_type = ome . PT_UINT8 <EOL> elif self . get_bit_depth ( ) == BIT_DEPTH_FLOAT : <EOL> pixel_type = ome . PT_FLOAT <EOL> else : <EOL> if not u16hack : <EOL> pixels = ( pixels * <NUM_LIT> ) <EOL> pixel_type = ome . PT_UINT16 <EOL> elif self . save_image_or_figure == IF_MASK : <EOL> pixels = image . mask . astype ( np . uint8 ) * <NUM_LIT:255> <EOL> pixel_type = ome . PT_UINT8 <EOL> elif self . save_image_or_figure == IF_CROPPING : <EOL> pixels = image . crop_mask . astype ( np . uint8 ) * <NUM_LIT:255> <EOL> pixel_type = ome . PT_UINT8 <EOL> filename = self . get_filename ( workspace ) <EOL> if filename is None : <EOL> return <EOL> if self . get_file_format ( ) == FF_MAT : <EOL> scipy . io . matlab . mio . savemat ( filename , { "<STR_LIT>" : pixels } , format = '<STR_LIT:5>' ) <EOL> elif self . get_file_format ( ) == FF_BMP : <EOL> save_bmp ( filename , pixels ) <EOL> else : <EOL> self . do_save_image ( workspace , filename , pixels , pixel_type ) <EOL> if self . show_window : <EOL> workspace . display_data . wrote_image = True <EOL> if self . when_to_save != WS_LAST_CYCLE : <EOL> self . save_filename_measurements ( workspace ) <EOL> def check_overwrite ( self , filename , workspace ) : <EOL> '''<STR_LIT>''' <EOL> if not self . overwrite . value and os . path . isfile ( filename ) : <EOL> try : <EOL> return workspace . interaction_request ( self , workspace . measurements . image_set_number , filename ) == "<STR_LIT>" <EOL> except workspace . NoInteractionException : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % ( <EOL> filename ) ) <EOL> return True <EOL> def handle_interaction ( self , image_set_number , filename ) : <EOL> '''<STR_LIT>''' <EOL> import wx <EOL> dlg = wx . MessageDialog ( wx . GetApp ( ) . TopWindow , <EOL> "<STR_LIT>" % ( self . module_name , self . module_num , image_set_number , filename ) , <EOL> "<STR_LIT>" , wx . YES_NO | wx . ICON_QUESTION ) <EOL> result = dlg . ShowModal ( ) == wx . ID_YES <EOL> return "<STR_LIT>" if result else "<STR_LIT>" <EOL> def save_filename_measurements ( self , workspace ) : <EOL> if self . update_file_names . value : <EOL> filename = self . get_filename ( workspace , make_dirs = False , <EOL> check_overwrite = False ) <EOL> pn , fn = os . path . split ( filename ) <EOL> url = pathname2url ( filename ) <EOL> workspace . measurements . add_measurement ( cpmeas . IMAGE , <EOL> self . file_name_feature , <EOL> fn , <EOL> can_overwrite = True ) <EOL> workspace . measurements . add_measurement ( cpmeas . IMAGE , <EOL> self . path_name_feature , <EOL> pn , <EOL> can_overwrite = True ) <EOL> workspace . measurements . add_measurement ( cpmeas . IMAGE , <EOL> self . url_feature , <EOL> url , <EOL> can_overwrite = True ) <EOL> @ property <EOL> def file_name_feature ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . save_image_or_figure == IF_OBJECTS : <EOL> return '<STR_LIT:_>' . join ( ( C_OBJECTS_FILE_NAME , self . objects_name . value ) ) <EOL> return '<STR_LIT:_>' . join ( ( C_FILE_NAME , self . image_name . value ) ) <EOL> @ property <EOL> def path_name_feature ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . save_image_or_figure == IF_OBJECTS : <EOL> return '<STR_LIT:_>' . join ( ( C_OBJECTS_PATH_NAME , self . objects_name . value ) ) <EOL> return '<STR_LIT:_>' . join ( ( C_PATH_NAME , self . image_name . value ) ) <EOL> @ property <EOL> def url_feature ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . save_image_or_figure == IF_OBJECTS : <EOL> return '<STR_LIT:_>' . join ( ( C_OBJECTS_URL , self . objects_name . value ) ) <EOL> return '<STR_LIT:_>' . join ( ( C_URL , self . image_name . value ) ) <EOL> @ property <EOL> def source_file_name_feature ( self ) : <EOL> '''<STR_LIT>''' <EOL> return '<STR_LIT:_>' . join ( ( C_FILE_NAME , self . file_image_name . value ) ) <EOL> def source_path ( self , workspace ) : <EOL> '''<STR_LIT>''' <EOL> if self . file_name_method . value == FN_FROM_IMAGE : <EOL> path_feature = '<STR_LIT>' % ( C_PATH_NAME , self . file_image_name . value ) <EOL> assert workspace . measurements . has_feature ( cpmeas . IMAGE , path_feature ) , "<STR_LIT>" % self . file_image_name . value <EOL> return workspace . measurements . get_current_image_measurement ( path_feature ) <EOL> cur_image = workspace . image_set . get_image ( self . image_name . value ) <EOL> while cur_image . path_name is None : <EOL> cur_image = cur_image . parent_image <EOL> assert cur_image is not None , "<STR_LIT>" <EOL> return cur_image . path_name <EOL> def get_measurement_columns ( self , pipeline ) : <EOL> if self . update_file_names . value : <EOL> return [ ( cpmeas . IMAGE , <EOL> self . file_name_feature , <EOL> cpmeas . COLTYPE_VARCHAR_FILE_NAME ) , <EOL> ( cpmeas . IMAGE , <EOL> self . path_name_feature , <EOL> cpmeas . COLTYPE_VARCHAR_PATH_NAME ) ] <EOL> else : <EOL> return [ ] <EOL> def get_filename ( self , workspace , make_dirs = True , check_overwrite = True ) : <EOL> "<STR_LIT>" <EOL> measurements = workspace . measurements <EOL> if self . file_name_method == FN_SINGLE_NAME : <EOL> filename = self . single_file_name . value <EOL> filename = workspace . measurements . apply_metadata ( filename ) <EOL> elif self . file_name_method == FN_SEQUENTIAL : <EOL> filename = self . single_file_name . value <EOL> filename = workspace . measurements . apply_metadata ( filename ) <EOL> n_image_sets = workspace . measurements . image_set_count <EOL> ndigits = int ( np . ceil ( np . log10 ( n_image_sets + <NUM_LIT:1> ) ) ) <EOL> ndigits = max ( ( ndigits , self . number_of_digits . value ) ) <EOL> padded_num_string = str ( measurements . image_set_number ) . zfill ( ndigits ) <EOL> filename = '<STR_LIT>' % ( filename , padded_num_string ) <EOL> else : <EOL> file_name_feature = self . source_file_name_feature <EOL> filename = measurements . get_current_measurement ( '<STR_LIT>' , <EOL> file_name_feature ) <EOL> filename = os . path . splitext ( filename ) [ <NUM_LIT:0> ] <EOL> if self . wants_file_name_suffix : <EOL> suffix = self . file_name_suffix . value <EOL> suffix = workspace . measurements . apply_metadata ( suffix ) <EOL> filename += suffix <EOL> filename = "<STR_LIT>" % ( filename , self . get_file_format ( ) ) <EOL> pathname = self . pathname . get_absolute_path ( measurements ) <EOL> if self . create_subdirectories : <EOL> image_path = self . source_path ( workspace ) <EOL> subdir = os . path . relpath ( image_path , self . root_dir . get_absolute_path ( ) ) <EOL> pathname = os . path . join ( pathname , subdir ) <EOL> if len ( pathname ) and not os . path . isdir ( pathname ) and make_dirs : <EOL> try : <EOL> os . makedirs ( pathname ) <EOL> except : <EOL> if not os . path . isdir ( pathname ) : <EOL> raise <EOL> result = os . path . join ( pathname , filename ) <EOL> if check_overwrite and not self . check_overwrite ( result , workspace ) : <EOL> return <EOL> if check_overwrite and os . path . isfile ( result ) : <EOL> try : <EOL> os . remove ( result ) <EOL> except : <EOL> import bioformats <EOL> bioformats . clear_image_reader_cache ( ) <EOL> os . remove ( result ) <EOL> return result <EOL> def get_file_format ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . save_image_or_figure == IF_MOVIE : <EOL> return self . movie_format . value <EOL> return self . file_format . value <EOL> def get_bit_depth ( self ) : <EOL> if ( self . save_image_or_figure == IF_IMAGE and <EOL> self . get_file_format ( ) in FF_SUPPORTING_16_BIT ) : <EOL> return self . bit_depth . value <EOL> else : <EOL> return BIT_DEPTH_8 <EOL> def upgrade_settings ( self , setting_values , variable_revision_number , <EOL> module_name , from_matlab ) : <EOL> """<STR_LIT>""" <EOL> PC_DEFAULT = "<STR_LIT>" <EOL> if from_matlab and variable_revision_number == <NUM_LIT:12> : <EOL> variable_revision_number = <NUM_LIT> <EOL> if from_matlab and variable_revision_number == <NUM_LIT> : <EOL> new_setting_values = list ( setting_values ) <EOL> for i in [ <NUM_LIT:3> , <NUM_LIT:12> ] : <EOL> if setting_values [ i ] == '<STR_LIT:\\>' : <EOL> new_setting_values [ i ] == cps . DO_NOT_USE <EOL> variable_revision_number = <NUM_LIT> <EOL> if from_matlab and variable_revision_number == <NUM_LIT> : <EOL> new_setting_values = [ ] <EOL> if setting_values [ <NUM_LIT:0> ] . isdigit ( ) : <EOL> new_setting_values . extend ( [ IF_FIGURE , setting_values [ <NUM_LIT:1> ] ] ) <EOL> elif setting_values [ <NUM_LIT:3> ] == '<STR_LIT>' : <EOL> new_setting_values . extend ( [ IF_MOVIE , setting_values [ <NUM_LIT:0> ] ] ) <EOL> elif setting_values [ <NUM_LIT:0> ] . startswith ( "<STR_LIT>" ) : <EOL> new_setting_values . extend ( [ IF_CROPPING , <EOL> setting_values [ <NUM_LIT:0> ] [ len ( "<STR_LIT>" ) : ] ] ) <EOL> elif setting_values [ <NUM_LIT:0> ] . startswith ( "<STR_LIT>" ) : <EOL> new_setting_values . extend ( [ IF_MASK , <EOL> setting_values [ <NUM_LIT:0> ] [ len ( "<STR_LIT>" ) : ] ] ) <EOL> else : <EOL> new_setting_values . extend ( [ IF_IMAGE , setting_values [ <NUM_LIT:0> ] ] ) <EOL> new_setting_values . append ( new_setting_values [ <NUM_LIT:1> ] ) <EOL> if setting_values [ <NUM_LIT:1> ] == '<STR_LIT:N>' : <EOL> new_setting_values . extend ( [ FN_SEQUENTIAL , "<STR_LIT:None>" , "<STR_LIT:None>" ] ) <EOL> elif setting_values [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == '<STR_LIT:=>' : <EOL> new_setting_values . extend ( [ FN_SINGLE_NAME , setting_values [ <NUM_LIT:1> ] [ <NUM_LIT:1> : ] , <EOL> setting_values [ <NUM_LIT:1> ] [ <NUM_LIT:1> : ] ] ) <EOL> else : <EOL> if len ( cpmeas . find_metadata_tokens ( setting_values [ <NUM_LIT:1> ] ) ) : <EOL> new_setting_values . extend ( [ FN_WITH_METADATA , setting_values [ <NUM_LIT:1> ] , <EOL> setting_values [ <NUM_LIT:1> ] ] ) <EOL> else : <EOL> new_setting_values . extend ( [ FN_FROM_IMAGE , setting_values [ <NUM_LIT:1> ] , <EOL> setting_values [ <NUM_LIT:1> ] ] ) <EOL> new_setting_values . extend ( setting_values [ <NUM_LIT:2> : <NUM_LIT:4> ] ) <EOL> if setting_values [ <NUM_LIT:4> ] == '<STR_LIT:.>' : <EOL> new_setting_values . extend ( [ PC_DEFAULT , "<STR_LIT:None>" ] ) <EOL> elif setting_values [ <NUM_LIT:4> ] == '<STR_LIT:&>' : <EOL> new_setting_values . extend ( [ PC_WITH_IMAGE , "<STR_LIT:None>" ] ) <EOL> else : <EOL> if len ( cpmeas . find_metadata_tokens ( setting_values [ <NUM_LIT:1> ] ) ) : <EOL> new_setting_values . extend ( [ PC_WITH_METADATA , <EOL> setting_values [ <NUM_LIT:4> ] ] ) <EOL> else : <EOL> new_setting_values . extend ( [ PC_CUSTOM , setting_values [ <NUM_LIT:4> ] ] ) <EOL> new_setting_values . extend ( setting_values [ <NUM_LIT:5> : <NUM_LIT:11> ] ) <EOL> new_setting_values . extend ( setting_values [ <NUM_LIT:12> : - <NUM_LIT:1> ] ) <EOL> setting_values = new_setting_values <EOL> from_matlab = False <EOL> variable_revision_number = <NUM_LIT:1> <EOL> if not from_matlab and variable_revision_number == <NUM_LIT:1> : <EOL> if setting_values [ <NUM_LIT:11> ] == cps . YES : <EOL> setting_values [ <NUM_LIT:11> ] = cps . NO <EOL> else : <EOL> setting_values [ <NUM_LIT:11> ] = cps . YES <EOL> variable_revision_number = <NUM_LIT:2> <EOL> if ( not from_matlab ) and variable_revision_number == <NUM_LIT:2> : <EOL> if setting_values [ <NUM_LIT:8> ] . startswith ( "<STR_LIT>" ) : <EOL> setting_values = ( setting_values [ : <NUM_LIT:8> ] + <EOL> [ PC_DEFAULT ] + setting_values [ <NUM_LIT:9> : ] ) <EOL> elif setting_values [ <NUM_LIT:8> ] . startswith ( "<STR_LIT>" ) : <EOL> setting_values = ( setting_values [ : <NUM_LIT:8> ] + <EOL> [ PC_WITH_IMAGE ] + setting_values [ <NUM_LIT:9> : ] ) <EOL> variable_revision_number = <NUM_LIT:3> <EOL> if ( not from_matlab ) and variable_revision_number == <NUM_LIT:3> : <EOL> if setting_values [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> setting_values [ <NUM_LIT:0> ] = IF_FIGURE <EOL> setting_values = standardize_default_folder_names ( setting_values , <NUM_LIT:8> ) <EOL> variable_revision_number = <NUM_LIT:4> <EOL> if ( not from_matlab ) and variable_revision_number == <NUM_LIT:4> : <EOL> save_image_or_figure , image_name , figure_name , file_name_method , file_image_name , single_file_name , file_name_suffix , file_format , pathname_choice , pathname , bit_depth , overwrite , when_to_save , when_to_save_movie , rescale , colormap , update_file_names , create_subdirectories = setting_values <EOL> pathname = SaveImagesDirectoryPath . static_join_string ( <EOL> pathname_choice , pathname ) <EOL> setting_values = [ <EOL> save_image_or_figure , image_name , figure_name , <EOL> file_name_method , file_image_name , single_file_name , <EOL> file_name_suffix != cps . DO_NOT_USE , <EOL> file_name_suffix , file_format , <EOL> pathname , bit_depth , overwrite , when_to_save , <EOL> rescale , colormap , update_file_names , create_subdirectories ] <EOL> variable_revision_number = <NUM_LIT:5> <EOL> if ( not from_matlab ) and variable_revision_number == <NUM_LIT:5> : <EOL> setting_values = list ( setting_values ) <EOL> file_name_method = setting_values [ <NUM_LIT:3> ] <EOL> single_file_name = setting_values [ <NUM_LIT:5> ] <EOL> wants_file_suffix = setting_values [ <NUM_LIT:6> ] <EOL> file_name_suffix = setting_values [ <NUM_LIT:7> ] <EOL> if file_name_method == FN_IMAGE_FILENAME_WITH_METADATA : <EOL> file_name_suffix = single_file_name <EOL> wants_file_suffix = cps . YES <EOL> file_name_method = FN_FROM_IMAGE <EOL> elif file_name_method == FN_WITH_METADATA : <EOL> file_name_method = FN_SINGLE_NAME <EOL> setting_values [ <NUM_LIT:3> ] = file_name_method <EOL> setting_values [ <NUM_LIT:6> ] = wants_file_suffix <EOL> setting_values [ <NUM_LIT:7> ] = file_name_suffix <EOL> variable_revision_number = <NUM_LIT:6> <EOL> if ( not from_matlab ) and ( variable_revision_number == <NUM_LIT:6> ) : <EOL> setting_values = ( <EOL> setting_values [ : <NUM_LIT:2> ] + [ "<STR_LIT:None>" ] + setting_values [ <NUM_LIT:2> : <NUM_LIT> ] + <EOL> [ GC_GRAYSCALE ] + setting_values [ <NUM_LIT> : ] ) <EOL> variable_revision_number = <NUM_LIT:7> <EOL> if ( not from_matlab ) and ( variable_revision_number == <NUM_LIT:7> ) : <EOL> setting_values = setting_values + [ DEFAULT_INPUT_FOLDER_NAME ] <EOL> variable_revision_number = <NUM_LIT:8> <EOL> if ( not from_matlab ) and ( variable_revision_number == <NUM_LIT:8> ) : <EOL> if setting_values [ <NUM_LIT:9> ] == FF_TIF : <EOL> setting_values = setting_values [ : <NUM_LIT:9> ] + [ FF_TIFF ] + setting_values [ <NUM_LIT:10> : ] <EOL> variable_revision_number = <NUM_LIT:9> <EOL> if ( not from_matlab ) and ( variable_revision_number == <NUM_LIT:9> ) : <EOL> setting_values = setting_values [ : <NUM_LIT:7> ] + [ "<STR_LIT:4>" ] + setting_values [ <NUM_LIT:7> : ] <EOL> variable_revision_number = <NUM_LIT:10> <EOL> if ( not from_matlab ) and ( variable_revision_number == <NUM_LIT:10> ) : <EOL> setting_values = setting_values + [ FF_AVI ] <EOL> variable_revision_number = <NUM_LIT:11> <EOL> if variable_revision_number == <NUM_LIT:11> : <EOL> bit_depth = setting_values [ OFFSET_BIT_DEPTH_V11 ] <EOL> bit_depth = { <EOL> OLD_BIT_DEPTH_8 : BIT_DEPTH_8 , <EOL> OLD_BIT_DEPTH_16 : BIT_DEPTH_16 } . get ( bit_depth , bit_depth ) <EOL> setting_values = setting_values [ : OFFSET_BIT_DEPTH_V11 ] + [ bit_depth ] + setting_values [ OFFSET_BIT_DEPTH_V11 + <NUM_LIT:1> : ] <EOL> setting_values [ OFFSET_DIRECTORY_PATH ] = SaveImagesDirectoryPath . upgrade_setting ( setting_values [ OFFSET_DIRECTORY_PATH ] ) <EOL> return setting_values , variable_revision_number , from_matlab <EOL> def validate_module ( self , pipeline ) : <EOL> if ( self . save_image_or_figure in ( IF_IMAGE , IF_MASK , IF_CROPPING ) and <EOL> self . when_to_save in ( WS_FIRST_CYCLE , WS_EVERY_CYCLE ) ) : <EOL> for setting in cps . get_name_providers ( pipeline , <EOL> self . image_name ) : <EOL> if setting . provided_attributes . get ( cps . AVAILABLE_ON_LAST_ATTRIBUTE ) : <EOL> raise cps . ValidationError ( "<STR_LIT>" % <EOL> self . image_name . value , <EOL> self . when_to_save ) <EOL> if self . file_name_method == FN_SINGLE_NAME or ( self . file_name_method == FN_FROM_IMAGE and self . wants_file_name_suffix . value ) : <EOL> text_str = self . single_file_name . value if self . file_name_method == FN_SINGLE_NAME else self . file_name_suffix . value <EOL> undefined_tags = pipeline . get_undefined_metadata_tags ( text_str ) <EOL> if len ( undefined_tags ) > <NUM_LIT:0> : <EOL> raise cps . ValidationError ( <EOL> "<STR_LIT>" % <EOL> undefined_tags [ <NUM_LIT:0> ] , <EOL> self . single_file_name if self . file_name_method == FN_SINGLE_NAME else self . file_name_suffix ) <EOL> class SaveImagesDirectoryPath ( cps . DirectoryPath ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , text , file_image_name , doc ) : <EOL> '''<STR_LIT>''' <EOL> super ( SaveImagesDirectoryPath , self ) . __init__ ( <EOL> text , dir_choices = [ <EOL> cps . DEFAULT_OUTPUT_FOLDER_NAME , cps . DEFAULT_INPUT_FOLDER_NAME , <EOL> PC_WITH_IMAGE , cps . ABSOLUTE_FOLDER_NAME , <EOL> cps . DEFAULT_OUTPUT_SUBFOLDER_NAME , <EOL> cps . DEFAULT_INPUT_SUBFOLDER_NAME ] , doc = doc ) <EOL> self . file_image_name = file_image_name <EOL> def get_absolute_path ( self , measurements = None , image_set_index = None ) : <EOL> if self . dir_choice == PC_WITH_IMAGE : <EOL> path_name_feature = "<STR_LIT>" % self . file_image_name . value <EOL> return measurements . get_current_image_measurement ( path_name_feature ) <EOL> return super ( SaveImagesDirectoryPath , self ) . get_absolute_path ( <EOL> measurements , image_set_index ) <EOL> def test_valid ( self , pipeline ) : <EOL> if self . dir_choice not in self . dir_choices : <EOL> raise cps . ValidationError ( "<STR_LIT>" % <EOL> self . dir_choice , self ) <EOL> @ staticmethod <EOL> def upgrade_setting ( value ) : <EOL> '''<STR_LIT>''' <EOL> dir_choice , custom_path = cps . DirectoryPath . split_string ( value ) <EOL> if dir_choice in OLD_PC_WITH_IMAGE_VALUES : <EOL> dir_choice = PC_WITH_IMAGE <EOL> elif dir_choice in ( PC_CUSTOM , PC_WITH_METADATA ) : <EOL> if custom_path . startswith ( '<STR_LIT:.>' ) : <EOL> dir_choice = cps . DEFAULT_OUTPUT_SUBFOLDER_NAME <EOL> elif custom_path . startswith ( '<STR_LIT:&>' ) : <EOL> dir_choice = cps . DEFAULT_INPUT_SUBFOLDER_NAME <EOL> custom_path = '<STR_LIT:.>' + custom_path [ <NUM_LIT:1> : ] <EOL> else : <EOL> dir_choice = cps . ABSOLUTE_FOLDER_NAME <EOL> else : <EOL> return cps . DirectoryPath . upgrade_setting ( value ) <EOL> return cps . DirectoryPath . static_join_string ( dir_choice , custom_path ) <EOL> def save_bmp ( path , img ) : <EOL> '''<STR_LIT>''' <EOL> BITMAPINFOHEADER_SIZE = <NUM_LIT> <EOL> img = img . astype ( np . uint8 ) <EOL> w = img . shape [ <NUM_LIT:1> ] <EOL> h = img . shape [ <NUM_LIT:0> ] <EOL> if img . ndim == <NUM_LIT:3> : <EOL> rgb = True <EOL> raster_length = ( w * <NUM_LIT:3> + <NUM_LIT:3> ) & ~ <NUM_LIT:3> <EOL> tmp = np . zeros ( ( h , raster_length ) , np . uint8 ) <EOL> tmp [ : , <NUM_LIT:2> : ( w * <NUM_LIT:3> ) : <NUM_LIT:3> ] = img [ : , : , <NUM_LIT:0> ] <EOL> tmp [ : , <NUM_LIT:1> : ( w * <NUM_LIT:3> ) : <NUM_LIT:3> ] = img [ : , : , <NUM_LIT:1> ] <EOL> tmp [ : , <NUM_LIT:0> : ( w * <NUM_LIT:3> ) : <NUM_LIT:3> ] = img [ : , : , <NUM_LIT:2> ] <EOL> img = tmp <EOL> else : <EOL> rgb = False <EOL> if w % <NUM_LIT:4> != <NUM_LIT:0> : <EOL> raster_length = ( w + <NUM_LIT:3> ) & ~ <NUM_LIT:3> <EOL> tmp = np . zeros ( ( h , raster_length ) , np . uint8 ) <EOL> tmp [ : , : w ] = img <EOL> img = tmp <EOL> bmp = np . ascontiguousarray ( np . flipud ( img ) ) . data <EOL> with open ( path , "<STR_LIT:wb>" ) as fd : <EOL> def write2 ( value ) : <EOL> '''<STR_LIT>''' <EOL> fd . write ( np . array ( [ value ] , "<STR_LIT>" ) . data [ : <NUM_LIT:2> ] ) <EOL> def write4 ( value ) : <EOL> '''<STR_LIT>''' <EOL> fd . write ( np . array ( [ value ] , "<STR_LIT>" ) . data [ : <NUM_LIT:4> ] ) <EOL> fd . write ( "<STR_LIT>" ) <EOL> length = <NUM_LIT> <EOL> length += BITMAPINFOHEADER_SIZE <EOL> if not rgb : <EOL> length += <NUM_LIT:4> * <NUM_LIT> <EOL> hdr_length = length <EOL> length += len ( bmp ) <EOL> write4 ( length ) <EOL> write4 ( <NUM_LIT:0> ) <EOL> write4 ( hdr_length ) <EOL> write4 ( BITMAPINFOHEADER_SIZE ) <EOL> write4 ( w ) <EOL> write4 ( h ) <EOL> write2 ( <NUM_LIT:1> ) <EOL> write2 ( <NUM_LIT> if rgb else <NUM_LIT:8> ) <EOL> write4 ( <NUM_LIT:0> ) <EOL> write4 ( len ( bmp ) ) <EOL> write4 ( <NUM_LIT> ) <EOL> write4 ( <NUM_LIT> ) <EOL> write4 ( <NUM_LIT:0> if rgb else <NUM_LIT> ) <EOL> write4 ( <NUM_LIT:0> ) <EOL> if not rgb : <EOL> color_table = np . column_stack ( <EOL> [ np . arange ( <NUM_LIT> ) ] * <NUM_LIT:3> + <EOL> [ np . zeros ( <NUM_LIT> , np . uint32 ) ] ) . astype ( np . uint8 ) <EOL> fd . write ( np . ascontiguousarray ( color_table , np . uint8 ) . data ) <EOL> fd . write ( bmp ) </s>
