import numpy as np
from PIL import Image
if __name__ == '__main__':
image_file = '3.jpg'
height = 100
img = Image.open(image_file)
img_width, img_height = img.size
width = 2 * height * img_width // img_height # 假定字符的高度是宽度的2倍
img = img.resize((width, height), Image.ANTIALIAS) #图片反编码
pixels = np.array(img.convert('L')) # 不同模式图像之间的转换
print(pixels.shape)
print(pixels)
chars = "MNHQ$OC?7>!:-;. "
N = len(chars)
step = 256 // N
print(N)
result = ''
for i in range(height):
for j in range(width):
result += chars[pixels[i][j] // step]
result += '\n'
with open('text2.txt', mode='w') as f:
f.write(result)
机器学习之图像处理灰化程序
最新推荐文章于 2024-09-04 22:00:35 发布