Jump to content

Python, File to PNG Compressor.

Poet129
Go to solution Solved by Poet129,
2 minutes ago, WereCatf said:

No need to.

Well I would to thank everybody who participated in helping me make this compressor, helped me test it, and even showed me how it isn't better in most cases. Thanks again, will definitely look for help here again in my future projects.

You can do something close to that with IrfanView

 

Open image

Image > Decrease color depth : select 2 colors (black and white) 

Save image as  PGM  (but in the options panel, check ASCII  Encoding instead of Binary Encoding:  

image.png.4e9200ceeea201744166164659c17544.png

 

Irfanview saves as grayscale even if there's only 2 colors (black and white), so it uses 0 for one color and 255 for the other color.

 

image.png.9709e2d0e2d6f32f822cf9996e92ca66.png

 

So you could simply do a "search and replace" and replace "255" with "1"  and then search and replace for " "  and replace it with nothing to remove the spaces.

 

You can do this directly in python as well.  Load the whole file in a string variable,  remove space characters, replace 255 with 1, split the string by the "\n"  (enter) character  and the longest string is your 1s and 0s.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

34 minutes ago, mariushm said:

You can do something close to that with IrfanView

 

Open image

Image > Decrease color depth : select 2 colors (black and white) 

Save image as  PGM  (but in the options panel, check ASCII  Encoding instead of Binary Encoding:  

image.png.4e9200ceeea201744166164659c17544.png

 

Irfanview saves as grayscale even if there's only 2 colors (black and white), so it uses 0 for one color and 255 for the other color.

 

image.png.9709e2d0e2d6f32f822cf9996e92ca66.png

 

So you could simply do a "search and replace" and replace "255" with "1"  and then search and replace for " "  and replace it with nothing to remove the spaces.

 

You can do this directly in python as well.  Load the whole file in a string variable,  remove space characters, replace 255 with 1, split the string by the "\n"  (enter) character  and the longest string is your 1s and 0s.

 

 

I hate to be picky but is there a cli version of this for easier automation also what about vice versa Binary to Picture?

Link to comment
Share on other sites

Link to post
Share on other sites

As you can see it's just a matter of putting 0s or 1s in the files, so you can do that straight in your script.

 

But of course any of this will still be worse than not using images at all.

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

I still haven't understood this obsession with pixels and using everything as if they were images. A number is a number and it's irrelevant what the number supposedly represents; a number of apples, a number of colour-values, a number of times a red car passed you on the road -- it's still a number.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Kilrah said:

As you can see it's just a matter of putting 0s or 1s in the files, so you can do that straight in your script.

 

But of course any of this will still be worse than not using images at all.

I know how to do the script changing 255 to 1 etc, I'm asking about the image to 255 and 0.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Poet129 said:

I know how to do the script changing 255 to 1 etc, I'm asking about the image to 255 and 0.

It's literally written on the website page how they do it, they calculate the brightness of a pixel from its rgb values then write a 1 if brightness is >=0.5 or a 0 if not. But that's because it's what's appropriate for their purpose, it probably isn't for what you want (that we still can't understand).

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

Do you actually want an image converted to 1s and 0s or you'd be fine with converting text ?

 

For text, you can do it super easy in php , and you can run php scripts from command line ... just go  php.exe your_script.php input_file.txt  output_file.txt and it works, just like with python

 

If you don't give the input and output files in the command line, it will default to  input.txt and output.txt file names.

 

this would convert text into 1s and 0s :

 

<?php 

// Default to reading input.txt in the folder where the script is.
$filename_in  =  __DIR__ . '/input.txt';
// Default to reading input.txt in the folder where the script is.
$filename_out =  __DIR__ . '/output.txt';

// See if we received input and output files as arguments in command line
// The script won't work if the paths have spaces in them, you need to do 
// this better in that case, look for "  " characters and join together arguments
//
if (isset($argv[1])==true) $filename_in = $argv[1];
if (isset($argv[2])==true) $filename_out = $argv[2];

$input = file_get_contents($filename_in);

$text = '';
for ($i=0;$i<strlen($input);$i++) {
	$character = substr($input,$i,1); // get character at position i
    $code = ord($character); // convert to ascii code
	// convert to binary,make sure it's always 8 chars and add to text buffer
    $text.= str_pad(decbin($code),8,'0',STR_PAD_LEFT); 
}
//write the text variable to file 
file_put_contents($filename_out,$text);
?>

 

and here's the script that converts the sequence of 1s and 0s back into regular text :

 

<?php 

// Default to reading input.txt in the folder where the script is.
$filename_in  =  __DIR__ . '/input.txt';
// Default to reading input.txt in the folder where the script is.
$filename_out =  __DIR__ . '/output.txt';

// See if we received input and output files as arguments in command line
// The script won't work if the paths have spaces in them, you need to do 
// this better in that case, look for "  " characters and join together arguments
//
if (isset($argv[1])==true) $filename_in = $argv[1];
if (isset($argv[2])==true) $filename_out = $argv[2];

$input = file_get_contents($filename_in);

$text = '';
$i = 0;
while ($i<strlen($input)) {
	$chunk = substr($input,$i,8); // get 8 characters from input
	$code = base_convert($chunk,2,10); // convert from base2 to base10
	$character = chr($code); 	// convert to character
	$text .= $character;	// add character to buffer 
	$i=$i+8; // move our position in the input string 
}
//write the text variable to file 
file_put_contents($filename_out,$text);
?>

 

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, WereCatf said:

I still haven't understood this obsession with pixels and using everything as if they were images. A number is a number and it's irrelevant what the number supposedly represents; a number of apples, a number of colour-values, a number of times a red car passed you on the road -- it's still a number.

Going that website you'll find these are the same however the picture is half the size.

AC.8.png

AC.8.txt

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Poet129 said:

half the size

The text file is 401 bytes and the image is 370, nowhere near half... and how was this made? That page you linked doesn't do such a text to image conversion.

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Kilrah said:

The text file is 401 bytes and the image is 370, nowhere near half... and how was this made? That page you linked doesn't do such a text ti image conversion.

It does it is the main thing in the middle of the page also the page has the vice versa which I used to make the png on my pc it 204 bytes btw. https://dcode.fr/binary-image

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, Poet129 said:

Going that website you'll find these are the same however the picture is half the size.

I have no idea what you mean, since those two files are not the same.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, WereCatf said:

I have no idea what you mean, since those two files are not the same.

If my pc had obs on it right now I would make a video. Edit: I'm installing OBS.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, Poet129 said:

the main thing in the middle of the page

The "Image from Binary Generator" on https://dcode.fr/binary-image expects a "BINARY LIST OF PIXELS (0 AND 1)" so not just random text...

 

  

Just now, Poet129 said:

If my pc had obs on it right now I would make a video.

Install it then?

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Kilrah said:

The "Image from Binary Generator" on https://dcode.fr/binary-image expects a "BINARY LIST OF PIXELS (0 AND 1)" so not just text...

The way the website uses the term "binary" is wrong to begin with. What the website actually does is it just converts black-and-white pixel-data to text-characters, not binary, so of course the resulting text-file will consume more space than the original image!

 

This is a whole boatload of misunderstanding on OP's part and horribly bad explanation on the website-designer's part.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, WereCatf said:

The way the website uses the term "binary" is wrong to begin with. What the website actually does is it just converts black-and-white pixel-data to text-characters, not binary, so of course the resulting text-file will consume more space than the original image!

 

This is a whole boatload of misunderstanding on OP's part and horribly bad explanation on the website-designer's part.

I'm sorry gave the wrong file however I'll make a video to show how I got it working.

Link to comment
Share on other sites

Link to post
Share on other sites

Dude you have to understand that some image formats (PNG, GIF) are compressed. PNG uses algorithm Deflate, like in ZIP,  GIF uses LZW which is similar.

 

Get it in your head already.

 

If that website converts a black and white image into 1s and 0s, the sequence of 1s and 0s would be just as big as the number of pixels in the image, because for every pixel there would be an 1 or 0 in the file.  

If you convert a sequence of text into image, the image file MAY be smaller because the image format chosen by the website COMPRESSES the data.

 

You also HAVE TO UNDERSTAND THE SETTINGS ON THAT PAGE

 

Also by default, the website REDUCES the width of the image when converting, so again you're losing information. you don't get a 1:1 match.

By default, the converter only looks at a picture as GRAYSCALE image, so if you give it a color picture or grayscale it treats it the same.

Also by default, it's configured to convert a pixel to 0 or 1, depending on the brightness of the pixel - if the pixel's luminosity is < 128, then it outputs 0,  if it's more than 128 it prints 1

You're LOSING INFORMATION.   You can't convert that sequence of 0s and 1s back to an image and get the same image as the original one.

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, mariushm said:

If that website converts a black and white image into 1s and 0s

But the website doesn't actually do even that. It converts the black-and-white image into text-characters, not the actual binary ones and zeroes. It is literally just a black-and-white image -> text-file converter.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

I've managed to get everything working, I will record a video after my family goes to sleep tonight.

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Poet129 said:

I've managed to get everything working, I will record a video after my family goes to sleep tonight.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, so you took a text file, converted it to a text representation of binary (i.e. a text file containing 1s and 0s), then converted that to a black an white image and finally ran that through PNG compression. Talk about convoluted. Just run the original file through something like 7zip, same result.

 

Everything a computer does is always "binary". Everything is 1s and 0s. The important thing is how you interpret those 1s and 0s. If you put data into a file, give it an ending of ".txt" then your computer will by default open it in a text editor and try to interpret the contents as text. If you rename it to ".png" the OS will open that with an image viewer, which will then try to render the image (and probably fail, because an image needs a header, which is just a pre-defined format of bits that i.e. identify the size and color depth of the image).

 

That doesn't mean it makes sense to interpret the bits of a text file as an image, because it would just be "random" colors mixed together, but on the lowest level there is no difference between a text file and an image file. Both contain bits. The only difference is how you interpret those bits.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Eigenvektor said:

Ok, so you took a text file, converted it to a text representation of binary (i.e. a text file containing 1s and 0s), then converted that to a black an white image and finally ran that through PNG compression. Talk about convoluted. Just run the original file through something like 7zip, same result.

I showed this if you watch the entire video and that the file size is bigger for a 7zip.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Poet129 said:

I showed this if you watch the entire video and that the file size is bigger for a 7zip.

7zip is a universal compression algorithm, which means the compressed file includes a header. For very small file sizes, the header will be larger than the original file, making it useless. In these cases a less generic compression algorithm that requires no header definitely has an advantage.

 

Try again with larger file sizes. Take a text file that is e.g. 1 MB or 5 MB in size and then compare the compressed result of 7zip vs the other method. In these cases a few Kb of header information is no longer going to make much of a noticeable difference.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, Eigenvektor said:

7zip is a universal compression algorithm, which means the compressed file includes a header. For very small file sizes, the header will be larger than the original file, making it useless. In these cases a less generic compression algorithm that requires no header definitely has an advantage.

 

Try again with larger file sizes. Take a text file that is e.g. 1 MB or 5 MB in size and then compare the compressed result of 7zip vs the other method. In these cases a few Kb of header information is no longer going to make much of a noticeable difference.

I would do this but I can't right now because I can't upload that much to two websites.

Link to comment
Share on other sites

Link to post
Share on other sites

You are using a specific input text that happens to play very nice with PNG's filtering. More random data or text won't fair anywhere near as well, and you could implement the same algorithm yourself without the convoluted process of going through an image...

 

All you're showing is that some algorithms work better with some input data than others. If one were to tailor a compression algorithm for that specific file you could likely make it even smaller.

 

F@H
Desktop: i9-13900K, ASUS Z790-E, 64GB DDR5-6000 CL36, RTX3080, 2TB MP600 Pro XT, 2TB SX8200Pro, 2x16TB Ironwolf RAID0, Corsair HX1200, Antec Vortex 360 AIO, Thermaltake Versa H25 TG, Samsung 4K curved 49" TV, 23" secondary, Mountain Everest Max

Mobile SFF rig: i9-9900K, Noctua NH-L9i, Asrock Z390 Phantom ITX-AC, 32GB, GTX1070, 2x1TB SX8200Pro RAID0, 2x5TB 2.5" HDD RAID0, Athena 500W Flex (Noctua fan), Custom 4.7l 3D printed case

 

Asus Zenbook UM325UA, Ryzen 7 5700u, 16GB, 1TB, OLED

 

GPD Win 2

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×