def test_png_file(filename: str) -> None: """ Takes the relative filename of a PNG file and draws the quadtree on top of it :param filename: :return: """ reader = Reader(filename) pngdata = reader.read() #extract file matrix = numpy.array([[y//255 for y in x] for x in list(pngdata[2])])[:, ::3] # load into numpy array lqtld = trees.LinearQuadTree(value_matrix=matrix) lqtld.draw_all_usable_cells() lqtld.generate_debug_png('../output.png')
def to_fits(self, out_file): """ Converts a PNG to file a FITS file. :param out_file: User specified filename for the FITS """ if not self.png: raise ValueError, "AstroPNG was not initialized with a PNG image" # Reading the PNG takes two passes (for now) r = Reader(self.png) width, height, imgdata, metadata = r.read() fluxes = numpy.vstack( itertools.imap(numpy.uint16, imgdata) ) r = Reader(self.png) chunks = r.chunks() has_fits = False has_quantization = False has_nans = False # Read the custom astro chunks while True: try: chunk_name, data = chunks.next() except: break if chunk_name == 'fITS': header = self.__read_fits_header(data) has_fits = True elif chunk_name == 'qANT': zzero, zscale = self.__read_quantization_parameters(data, height) has_quantization = True elif chunk_name == 'nANS': y_nans, x_nans = self.__read_nan_locations(data) has_nans = True elif chunk_name == 'iEND': break if has_quantization: random_numbers = self.__random_number_generator(N = width * height).reshape( (height, width) ) fluxes = (fluxes - random_numbers + 0.5) * numpy.vstack(zscale) + numpy.vstack(zzero) if has_nans: if y_nans.size > 0: fluxes[y_nans, x_nans] = numpy.nan fluxes = numpy.flipud(fluxes) hdu = pyfits.PrimaryHDU(fluxes, header) hdu.verify() hdu.writeto(out_file, output_verify='ignore')
def __readPngFile(self): with open(self._s_file, "rb") as fs: _reader = Reader(file=fs) _data = _reader.read() _colorarray = [] for frame in list(_data[2]): framearray = list(frame) _colorarray.append([ framearray[x:x + 3] for x in range(0, len(framearray), 3) ]) self.__nbleds = _data[0] self.__nbframes = _data[1] self.__data = _colorarray
def generate_spectrogram_file(self, audio: AudioRecording): prefilter = settings.SPECTROGRAM_PREFILTER credit = settings.SPECTROGRAM_CREDIT title = '(unknown) ' if audio.species: title = f'{audio.genus} {audio.species} ' try: species = self.species_lookup.species_by_abbreviations( audio.genus, audio.species) except NonUniqueSpeciesLookup: species = None if species: species_ucfirst = species.species[0].upper( ) + species.species[1:] if species.common_name: title = f'{species.common_name} ({species.genus} {species_ucfirst}) ' else: title = f'{species.genus} {species_ucfirst} ' if audio.recorded_at_iso: title += date_parser.parse( audio.recorded_at_iso).strftime('%Y-%m-%d %H:%M') if not audio.id: audio.save() dest = os.path.join(settings.MEDIA_ROOT, 'processed', 'spectrograms', f'{audio.id}-{audio.identifier}.png') print(f'Plot {audio.audio_file} spectrum to {dest}') # sox "$file" -n spectrogram -o "$outfile" -t "$ident" sox_result = subprocess.run([ self.sox_executable, audio.audio_file, '-n', ] + prefilter + ['spectrogram', '-o', dest, '-c', credit, '-t', title]) sox_result.check_returncode() png_reader = Reader(filename=dest) (width, height, _, _) = png_reader.read() audio.spectrogram_image_file = dest audio.spectrogram_image_width = width audio.spectrogram_image_height = height
def test_issue_54_notinverted(dark, light, transparent): qr = segno.make('The Beatles') assert 'M4-M' == qr.designator out = io.BytesIO() scale = 5 qr.save(out, kind='png', scale=scale, dark=dark, light=light) out.seek(0) reader = PNGReader(file=out) w, h, pixels, meta = reader.read() width, height = qr.symbol_size(scale=scale) assert width == w assert height == h assert meta['greyscale'] border_row = tuple([1] * w) expected_row = tuple([1] * 2 * scale + [0] * 7 * scale + [1]) for idx, row in enumerate(pixels): if idx < 10: assert border_row == tuple(row) elif idx == 10: assert expected_row == tuple(row)[:len(expected_row)] break assert transparent == reader.transparent
from png import Reader FILENAME = "target_gradent.png" background = Actor("background") background.web_color = "#000000" with open(FILENAME, 'rb') as f: reader = Reader(file=f) file_data = reader.read() """ (686, 300, <map at 0x7f153a04aa20>, {'alpha': True, 'size': (686, 300), 'planes': 4, 'interlace': 0, 'bitdepth': 8, 'greyscale': False, 'background': (255, 255, 255)}) """ image_width, image_height = file_data[0], file_data[1] # take the three ranges (drop alpha) for the whole width #colors = [c[:3] for c in list(zip(*list(file_data[2])))[:image_width]] #colors = list(file_data[2]) raw = list(file_data[2])[0] colors = list(zip(raw[::4], raw[1::4], raw[2::4])) go_and_back_colors = colors + colors[::-1]