Advertisement
EaterSun

Extract Information PNG

Nov 27th, 2024
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | Cybersecurity | 0 0
  1. def extract_png_chunks(file_path):
  2.     with open(file_path, 'rb') as file:
  3.         signature = file.read(8)
  4.         if signature != b'\x89PNG\r\n\x1a\n':
  5.             raise ValueError("File is not a valid PNG file.")
  6.        
  7.         print("Signature:", signature)
  8.        
  9.         while True:
  10.  
  11.             length_bytes = file.read(4)
  12.             if len(length_bytes) < 4:
  13.                 break  
  14.  
  15.             length = int.from_bytes(length_bytes, 'big')
  16.  
  17.             chunk_type = file.read(4).decode('ascii')
  18.  
  19.             chunk_data = file.read(length)
  20.  
  21.             crc = file.read(4).hex()
  22.  
  23.             print(f"Chunk Type: {chunk_type}")
  24.             print(f"  Length: {length} bytes")
  25.             print(f"  CRC: {crc}")
  26.             print()
  27.  
  28.             if chunk_type == 'IEND':
  29.                 break
  30.  
  31. extract_png_chunks('image.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement