Created
December 12, 2021 13:51
-
-
Save AdriDevelopsThings/9647d7023ebf41178dc9709262ef4f9e to your computer and use it in GitHub Desktop.
Look at a file as a picture. Each block (3 bytes) of the file will be put as RGB in one pixel. The resolution of the image is 16:9.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cast file to png | |
# Copyright (C) 2021 AdriDoesThings | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU Affero General Public License as published | |
# by the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU Affero General Public License for more details. | |
# | |
# You should have received a copy of the GNU Affero General Public License | |
# along with this program. If not, see <https://www.gnu.org/licenses/>. | |
from PIL import Image | |
from sys import argv | |
from os.path import getsize | |
from math import sqrt | |
import progressbar | |
# you need: pillow and progressbar2 - install it with: pip install pillow progressbar2 | |
filesystem_path = argv[1] | |
size = getsize(filesystem_path) | |
filesystem = open(filesystem_path, "rb") | |
width = round(4 * (sqrt(size / 3)/ 3 )) | |
height = round(3 * (sqrt(size / 3) / 4)) | |
im = Image.new(mode="RGB", size = (width, height)) | |
pic = im.load() | |
with progressbar.ProgressBar(max_value=round(size / 3)) as bar: | |
i = 0 | |
for y in range(height): | |
for x in range(width): | |
value = [x for x in filesystem.read(3)] | |
while len(value) != 3: | |
value.append(255) | |
if value: | |
pic[x,y] = tuple(value) | |
else: | |
pic[x,y] = (255, 255, 255) | |
if i < round(size / 3): | |
i += 1 | |
bar.update(i) | |
im.save("image.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment