def append_png_assertion(self, badge): """ Append the assertion to a PNG file """ badge.signed = _signature chunks = list() png = Reader(bytes=badge.source.image) for chunk in png.chunks(): chunks.append(chunk) itxt_data = b'openbadges' + pack( 'BBBBB', 0, 0, 0, 0, 0) + badge.get_assertion().encode('utf-8') itxt = ('iTXt', itxt_data) chunks.insert(len(chunks) - 1, itxt) text_data = 'Comment Signed with OpenBadgesLib %s' % __version__ text = ('tEXt', text_data.encode('utf-8')) chunks.insert(len(chunks) - 1, text) for tag, data in chunks: badge.signed = badge.signed + pack("!I", len(data)) tag = tag.encode('iso8859-1') badge.signed = badge.signed + tag badge.signed = badge.signed + data checksum = crc32(tag) checksum = crc32(data, checksum) checksum &= 2**32 - 1 badge.signed = badge.signed + pack("!I", checksum)
def extract_png_assertion(file_data): png = Reader(bytes=file_data) for tag, data in png.chunks(): if tag == 'iTXt': fmt_len = len(data) - 15 # 15=len('openbadges'+pack('BBBBB')) fmt = '<10s5B%ds' % fmt_len return Assertion.decode(unpack(fmt, data)[6])
def has_png_assertion(self, badge): png = Reader(bytes=badge.image) for tag, data in png.chunks(): if tag == 'iTXt': if data.startswith(b'openbadges'): return True return False
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')