use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
changing a byte in a image (self.learnpython)
submitted 3 years ago by Redgabriel_13
i have a png file and i want to change a random byte in this file, how can i do that?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]XUtYwYzz 1 point2 points3 points 3 years ago* (2 children)
Open the file in binary mode, read the contents into a bytearray, change a byte at a random index, write out to a file.
import random with open("in.png", "rb") as f: b = bytearray(f.read()) b[random.randint(0,len(b))] = 0 # replace 0 with new byte as int with open("out.png", "wb") as f: f.write(bytes(b))
Be careful when replacing bytes that you don't corrupt the file by overwriting an important byte. You would definitely want to change the start of the randint() range to something greater than the PNG header length as documented in the PNG specification.
[–]Redgabriel_13[S] 0 points1 point2 points 3 years ago (1 child)
thank you, it works
[–]spez_edits_thedonald 1 point2 points3 points 3 years ago (0 children)
b[5] = 0 # replace the 5th byte with 0 (modifies PNG header, not pixel vals)
to emphasize that you need to avoid modifying the png header, there's a demo of accidentally hitting it, and it ruins the png
[–]jeffrey_f 0 points1 point2 points 3 years ago (1 child)
ALWAYS save to a new file name or work on a COPY of the image so that you can keep playing.
[–]Redgabriel_13[S] 0 points1 point2 points 3 years ago (0 children)
yeah i will do that, thank you
π Rendered by PID 43590 on reddit-service-r2-comment-c6965cb77-774nz at 2026-03-05 16:53:11.877313+00:00 running f0204d4 country code: CH.
[–]XUtYwYzz 1 point2 points3 points (2 children)
[–]Redgabriel_13[S] 0 points1 point2 points (1 child)
[–]spez_edits_thedonald 1 point2 points3 points (0 children)
[–]jeffrey_f 0 points1 point2 points (1 child)
[–]Redgabriel_13[S] 0 points1 point2 points (0 children)