Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #217: The MGs ran out of gas.


sci / sci.crypt / Re: [file2png] Convert (encrypted) binary data to .png images and back

SubjectAuthor
* [file2png] Convert (encrypted) binary data to .png images and backStefan Claas
`* Re: [file2png] Convert (encrypted) binary data to .png images and backRich
 `* Re: [file2png] Convert (encrypted) binary data to .png images and backStefan Claas
  `- Re: [file2png] Convert (encrypted) binary data to .png images and backRich

1
Subject: [file2png] Convert (encrypted) binary data to .png images and back
From: Stefan Claas
Newsgroups: sci.crypt
Organization: To protect and to server
Date: Sun, 29 Dec 2024 03:05 UTC
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: pollux@tilde.club (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: <vkqeam$2odai$1@paganini.bofh.team>
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.
View all headers

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('<Q', file_size)
x, y = 0, 0

for i in range(0, 8, 3):
r = size_buf[i]
g = size_buf[i+1] if i+1 < 8 else 0
b = size_buf[i+2] if i+2 < 8 else 0
pixels[x, y] = (r, g, b, 255)
x += 1
if x >= 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('<Q', size_buf)[0]
data = bytearray(file_size)

data_index = 0
start_pixel = (8 + 2) // 3
for y in range(start_pixel // width, height):
start_x = 0 if y != start_pixel // width else start_pixel % width
for x in range(start_x, width):
if data_index >= 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

Subject: Re: [file2png] Convert (encrypted) binary data to .png images and back
From: Rich
Newsgroups: sci.crypt
Organization: A noiseless patient Spider
Date: Mon, 30 Dec 2024 20:35 UTC
References: 1
Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: sci.crypt
Subject: Re: [file2png] Convert (encrypted) binary data to .png images and back
Date: Mon, 30 Dec 2024 20:35:58 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <vkv07d$1pi60$1@dont-email.me>
References: <vkqeam$2odai$1@paganini.bofh.team>
Injection-Date: Mon, 30 Dec 2024 21:35:58 +0100 (CET)
Injection-Info: dont-email.me; posting-host="1830077b5b0fbda0c8b476fd9564eff1";
logging-data="1886400"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+DJWtFk4bA2QLDUX/rkfRu"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Cancel-Lock: sha1:HPV+tzRYjGnjUfu9NgYK/ob0svs=
View all headers

Stefan Claas <pollux@tilde.club> wrote:
> 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!

PNG compression is lossless, so it should not matter if the PNG is
compressed, you should get back the same bytes that went in.

Now, if instead you mean "rescale" (reduce/increase size) or other
transformations (RGBI to RGB or RGB to indexed) then those
transformations may alter the content such that you do not get back
what you put in.

Subject: Re: [file2png] Convert (encrypted) binary data to .png images and back
From: Stefan Claas
Newsgroups: sci.crypt
Organization: To protect and to server
Date: Tue, 31 Dec 2024 12:44 UTC
References: 1 2
Path: news.eternal-september.org!eternal-september.org!feeder3.eternal-september.org!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: pollux@tilde.club (Stefan Claas)
Newsgroups: sci.crypt
Subject: Re: [file2png] Convert (encrypted) binary data to .png images and back
Date: Tue, 31 Dec 2024 13:44:36 +0100
Organization: To protect and to server
Message-ID: <vl0ovn$3cemr$1@paganini.bofh.team>
References: <vkqeam$2odai$1@paganini.bofh.team> <vkv07d$1pi60$1@dont-email.me>
Mime-Version: 1.0
Injection-Date: Tue, 31 Dec 2024 12:44:39 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="3554011"; posting-host="6lqOwJyxRLkv94k9QBgAmg.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:liHEc++D95v98NKKAWJX9lQ6CsM=
X-Ed25519-Sig: 2a68242b24ef60c91b44b827e2be045207becbcc6b4d63b04d3c82d17238280d
57fcf68c08e11234f07d220469a4fca8d54c730419697e33543bb5489da52405
X-Date: It's Tue Sep 11445 01:44:36 PM CET 1993, the September that never ends.
X-Notice: Filtered by postfilter v. 0.9.3
X-Ed25519-Pub: c0ffee5a36e581eb10f60b2831b3cdb955d2e7ef680dd282a8d43ad8b84b357a
View all headers

Rich wrote:
> Stefan Claas <pollux@tilde.club> wrote:
> > 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!
>
> PNG compression is lossless, so it should not matter if the PNG is
> compressed, you should get back the same bytes that went in.
>
> Now, if instead you mean "rescale" (reduce/increase size) or other
> transformations (RGBI to RGB or RGB to indexed) then those
> transformations may alter the content such that you do not get back
> what you put in.

I remember taht facebook/Meta did something with .png in the past and
it did not work, but it was not rescaling etc.

--
Happy New Year 2025
Stefan

Subject: Re: [file2png] Convert (encrypted) binary data to .png images and back
From: Rich
Newsgroups: sci.crypt
Organization: A noiseless patient Spider
Date: Tue, 31 Dec 2024 23:15 UTC
References: 1 2 3
Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: sci.crypt
Subject: Re: [file2png] Convert (encrypted) binary data to .png images and back
Date: Tue, 31 Dec 2024 23:15:00 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <vl1ttk$2ef4n$1@dont-email.me>
References: <vkqeam$2odai$1@paganini.bofh.team> <vkv07d$1pi60$1@dont-email.me> <vl0ovn$3cemr$1@paganini.bofh.team>
Injection-Date: Wed, 01 Jan 2025 00:15:01 +0100 (CET)
Injection-Info: dont-email.me; posting-host="8c10bf53f7fd2c88970ece96e6e64bf0";
logging-data="2571415"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Y0mWmFO4JFqji4QVCmUu2"
User-Agent: tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Cancel-Lock: sha1:12S0Uxeyw72TItWdefGGR7jOTCE=
View all headers

Stefan Claas <pollux@tilde.club> wrote:
> Rich wrote:
>> Stefan Claas <pollux@tilde.club> wrote:
>> > 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!
>>
>> PNG compression is lossless, so it should not matter if the PNG is
>> compressed, you should get back the same bytes that went in.
>>
>> Now, if instead you mean "rescale" (reduce/increase size) or other
>> transformations (RGBI to RGB or RGB to indexed) then those
>> transformations may alter the content such that you do not get back
>> what you put in.
>
> I remember taht facebook/Meta did something with .png in the past and
> it did not work, but it was not rescaling etc.

There's only one compression algorithm in the PNG standard, zlib, which
itself is lossless.

But there are a lot of "transformations" that could be done on a PNG
that would result in modification of the actual image bytes inside.
Given the potential cost savings to a company with the scale of
facebook/meta from even a few percentage of saved space on uploaded
PNG's from "optimizing" them, it would not be surprising to learn they
were doing something to them that changed the stored bytes.

1

rocksolid light 0.9.8
clearnet tor