求助:无需Python图像处理库,将BMP图像数据转为十六进制等格式写入文件
Hey there! I see what's going on here—you're trying to read a BMP file without relying on image processing libraries, then output its data in a human-readable format (hex, binary, or decimal). The issue with your current code is that you're writing raw binary data directly to a text file. Text editors interpret those binary bytes as character encodings (like UTF-8), which turns non-ASCII values into that garbled text you're seeing.
Let's break down how to convert the BMP's binary data into each of the formats you want:
1. Output as Hexadecimal
Hex is the most common format for viewing binary data, since it's compact and easy to parse. We can use Python's built-in binascii module to handle the conversion, or do it manually with string formatting.
import binascii # Read the BMP file in binary mode with open('test7.bmp', 'rb') as bmp_file: bmp_raw_data = bmp_file.read() # Convert raw binary to a hex string (decode to make it a readable text string) hex_string = binascii.hexlify(bmp_raw_data).decode('utf-8') # Optional: Format the hex string to wrap every 16 bytes (32 hex characters) for readability formatted_hex = '\n'.join([hex_string[i:i+32] for i in range(0, len(hex_string), 32)]) # Write the formatted hex data to a text file with open('Image_hex_details.txt', 'w', encoding='utf-8') as output_file: output_file.write(formatted_hex)
2. Output as Binary
If you want to see each byte as an 8-bit binary string, we can use Python's format() function to ensure each byte is padded with leading zeros to make it 8 characters long.
# Read the BMP file with open('test7.bmp', 'rb') as bmp_file: bmp_raw_data = bmp_file.read() # Convert each byte to an 8-bit binary string, separated by spaces binary_string = ' '.join([format(byte, '08b') for byte in bmp_raw_data]) # Optional: Wrap every 8 bytes (64 characters + spaces) for readability formatted_binary = '\n'.join([binary_string[i:i+64] for i in range(0, len(binary_string), 64)]) # Write to text file with open('Image_binary_details.txt', 'w', encoding='utf-8') as output_file: output_file.write(formatted_binary)
3. Output as Decimal
For decimal values (each byte represented as a number between 0 and 255), we can convert each byte directly to a string and separate them with spaces.
# Read the BMP file with open('test7.bmp', 'rb') as bmp_file: bmp_raw_data = bmp_file.read() # Convert each byte to a decimal string, separated by spaces decimal_string = ' '.join([str(byte) for byte in bmp_raw_data]) # Optional: Wrap every 16 values for readability formatted_decimal = '\n'.join([decimal_string[i:i+47] for i in range(0, len(decimal_string), 47)]) # Write to text file with open('Image_decimal_details.txt', 'w', encoding='utf-8') as output_file: output_file.write(formatted_decimal)
Key Notes:
- Always use
rbmode when reading binary files like BMPs—this ensures you get the raw byte data instead of trying to interpret it as text. - When writing the formatted data, use
wmode (notwb) with a specified encoding (like UTF-8) because we're now writing text strings, not raw binary. - The optional formatting steps (adding newlines) make the output easier to read, especially for large BMP files.
内容的提问来源于stack exchange,提问作者aravind telidevara

