为什么from PIL import Image def extract_text_from_image(image_file, output_file): # 读取图片 img = Image.open(image_file) width, height = img.size # 每个像素点可以存储3个字符(RGB三色通道),计算可存储的字符数 max_chars = (width * height) * 3 // 8 # 提取嵌入的二进制字符串 binary_list = [] pixel_index = 0 for row in range(height): for col in range(width): if pixel_index < max_chars: pixel = list(img.getpixel((col, row))) for i in range(3): if pixel_index < max_chars: binary_list.append(str(pixel[i] % 2)) pixel_index += 1 # 将二进制字符串转换为文本 binary_text = ''.join(binary_list) text = ''.join(chr(int(binary_text[i:i+8], 2)) for i in range(0, len(binary_text), 8)) # 保存文本文件 with open(output_file, 'w', encoding='utf-8') as f: f.write(text) if __name__ == '__main__': extract_text_from_image('output.bmp', 'recovered_text.txt')会提取出乱码,将修改后的代码发给我
时间: 2023-06-25 16:05:34 浏览: 149
这段代码的作用是从一张图片中提取出嵌入的文本信息,并保存为文本文件。可能出现乱码的原因是编码问题,可以尝试修改编码方式为 'gbk' 或 'utf-8',具体可以修改如下:
```
from PIL import Image
def extract_text_from_image(image_file, output_file):
# 读取图片
img = Image.open(image_file)
width, height = img.size
# 每个像素点可以存储3个字符(RGB三色通道),计算可存储的字符数
max_chars = (width * height) * 3 // 8
# 提取嵌入的二进制字符串
binary_list = []
pixel_index = 0
for row in range(height):
for col in range(width):
if pixel_index < max_chars:
pixel = list(img.getpixel((col, row)))
for i in range(3):
if pixel_index < max_chars:
binary_list.append(str(pixel[i] % 2))
pixel_index += 1
# 将二进制字符串转换为文本
binary_text = ''.join(binary_list)
text = ''.join(chr(int(binary_text[i:i+8], 2)) for i in range(0, len(binary_text), 8))
# 保存文本文件
with open(output_file, 'w', encoding='utf-8') as f:
f.write(text)
if __name__ == '__main__':
extract_text_from_image('output.bmp', 'recovered_text.txt')
```
如果还是出现乱码,可以尝试将编码方式修改为 'gbk',具体可以修改如下:
```
from PIL import Image
def extract_text_from_image(image_file, output_file):
# 读取图片
img = Image.open(image_file)
width, height = img.size
# 每个像素点可以存储3个字符(RGB三色通道),计算可存储的字符数
max_chars = (width * height) * 3 // 8
# 提取嵌入的二进制字符串
binary_list = []
pixel_index = 0
for row in range(height):
for col in range(width):
if pixel_index < max_chars:
pixel = list(img.getpixel((col, row)))
for i in range(3):
if pixel_index < max_chars:
binary_list.append(str(pixel[i] % 2))
pixel_index += 1
# 将二进制字符串转换为文本
binary_text = ''.join(binary_list)
text = ''.join(chr(int(binary_text[i:i+8], 2)) for i in range(0, len(binary_text), 8))
# 保存文本文件
with open(output_file, 'w', encoding='gbk') as f:
f.write(text)
if __name__ == '__main__':
extract_text_from_image('output.bmp', 'recovered_text.txt')
```
阅读全文