Path: ...!feeds.phibee-telecom.net!2.eu.feeder.erje.net!feeder.erje.net!newsfeed.bofh.team!paganini.bofh.team!not-for-mail From: Stefan Claas Newsgroups: sci.crypt Subject: [file2png] Convert (encrypted) binary data to .png images and back Date: Sun, 29 Dec 2024 04:05:58 +0100 Organization: To protect and to server Message-ID: Mime-Version: 1.0 Injection-Date: Sun, 29 Dec 2024 03:05:58 -0000 (UTC) Injection-Info: paganini.bofh.team; logging-data="2897234"; posting-host="fWMvsfsAKClk+dLC0zX7xw.user.paganini.bofh.team"; mail-complaints-to="usenet@bofh.team"; posting-account="9dIQLXBM7WM9KzA+yjdR4A"; User-Agent: flnews/1.3.0pre29 (for GNU/Linux) Cancel-Lock: sha1:F2UVCf7KdHH5U42edmTSbqdoHBg= X-Ed25519-Sig: adbb35f5904ebe5cabeabcc6b977813c3ace3bff427b75fee785e88515ebafca d53ce6cca43705350086d42f8c3637c17f892d9a424a884628b13af9f718970c X-Ed25519-Pub: c0ffee5a36e581eb10f60b2831b3cdb955d2e7ef680dd282a8d43ad8b84b357a X-Notice: Filtered by postfilter v. 0.9.3 X-Date: It's Sun Sep 11443 04:05:58 AM CET 1993, the September that never ends. Bytes: 4743 Lines: 118 Hi all, here is a Python3 implementation of my file2png program, available here: https://github.com/706f6c6c7578/file2png Hope you find it useful, when for example uploading such images to platforms like X, which does not compress .png images, as I have tested! import argparse import math import os import struct from PIL import Image import sys def encode_file(input_stream, output_stream): data = input_stream.read() file_size = len(data) pixel_count = (file_size + 8) // 3 dimension = math.ceil(math.sqrt(pixel_count)) img = Image.new('RGBA', (dimension, dimension), (0, 0, 0, 255)) pixels = img.load() size_buf = struct.pack('= dimension: x = 0 y += 1 for i in range(0, len(data), 3): r = data[i] g = data[i+1] if i+1 < len(data) else 0 b = data[i+2] if i+2 < len(data) else 0 pixels[x, y] = (r, g, b, 255) x += 1 if x >= dimension: x = 0 y += 1 img.save(output_stream, format='PNG') def decode_file(input_stream, output_stream): img = Image.open(input_stream) pixels = img.load() width, height = img.size size_buf = bytearray(8) pixel_index = 0 for i in range(0, 8, 3): x = pixel_index % width y = pixel_index // width r, g, b, _ = pixels[x, y] size_buf[i] = r if i+1 < 8: size_buf[i+1] = g if i+2 < 8: size_buf[i+2] = b pixel_index += 1 file_size = struct.unpack('= file_size: break r, g, b, _ = pixels[x, y] data[data_index] = r if data_index + 1 < file_size: data[data_index+1] = g if data_index + 2 < file_size: data[data_index+2] = b data_index += 3 output_stream.write(data[:file_size]) def main(): parser = argparse.ArgumentParser(description="file2png - Convert any file to PNG and back") parser.add_argument("-d", action="store_true", help="decode PNG to file") parser.add_argument("input", nargs="?", default="-", help="Input file (default: stdin)") parser.add_argument("output", nargs="?", default="-", help="Output file (default: stdout)") args = parser.parse_args() input_stream = sys.stdin.buffer if args.input == "-" else open(args.input, "rb") output_stream = sys.stdout.buffer if args.output == "-" else open(args.output, "wb") try: if args.d: decode_file(input_stream, output_stream) else: encode_file(input_stream, output_stream) except Exception as e: print(f"Error: {e}", file=sys.stderr) sys.exit(1) finally: if input_stream is not sys.stdin.buffer: input_stream.close() if output_stream is not sys.stdout.buffer: output_stream.close() if __name__ == "__main__": main() -- Regards Stefan