-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy_img_to_txt_0.py
88 lines (74 loc) · 2.8 KB
/
py_img_to_txt_0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from PIL import Image#导入Image库用与操作图片文件
import datetime
def image_to_txt(imgName):
#获取当前时间,转换成字符串
timenow = datetime.datetime.now()
timestr = timenow.strftime("%Y-%m-%d-%H-%M-%S")
#生成的Txt文件用<原图片文件名+ 当前时间字符串+ ".txt"后缀>作为文件名
namestr = "{0}-{1}.txt".format(imgName, timestr)
#打开或创建一个TxT文件文件
txt = open(namestr, "w+")
#打开图片文件文件
print("Open Image File [{0}]".format(imgName))
try:
img = Image.open(imgName)
except:
print("Error to Open [{0}]!!!".format(imgName))
#判断图片文件的格式, 这里必须为"RGB"格式, 如果不是"RGB"格式,
#则用convert函数转换成"RGB"格式.
if "RGB" == img.mode:
print("Size{0},Format({1}),Color({2})".format(img.size, img.format, img.mode))
else:
print("Not a RGB image file!!!")
img = img.convert("RGB")
print("Convert to RGB Success!!!")
#获取图片文件宽和高
width = img.size[0]
height = img.size[1]
zoom = 0
#如果图片文件大于 400*400像素,则对图片进行缩放,缩放比例依照宽度和高度中的最大值
# if width >= height:
# maxsize = width
# else:
# maxsize = height
# if maxsize >= 400:
# zoom = maxsize / 400
# width = int(width / zoom)
# height = int(height / zoom)
# img = img.resize((width, height))
# print("Image Size too large, Resize to", img.size)
#把图片文件转换成纯黑白的图片
img = img.convert("1")
index = 0
print("Start Process!")
for h in range(height):#遍历图片的高度[0, height)
#显示处理进度
index += 1
print("#", end="")
txt.write("/*")
if index >= 60:#大于60换行
index = 0
print("")
for w in range(width):#遍历图片的宽度,[0, width)
pixel = img.getpixel((w, h))#获取图片当前坐标点的像素值
#print("w=", w, "h=", h, "pixel=", pixel)
if pixel != 0:#因为是纯黑白图像,所以像素颜色只有0或255两种值
txt.write("_")#非0则往txt中写入"_"表示白色
#print("w=", w, "h=", h, "pixel=", pixel)
else:
txt.write("@")#0则往txt中写入"@"表示黑色
#print("w=", w, "h=", h, "pixel=", pixel)
txt.write("*/")
txt.write("\n")
#保存新生成的TXT文件
print("\nProcess Done!")
print("Save File As [{0}]".format(namestr))
txt.close()
print("Save Done!")
name = input("Please Input Image File Name:")
print("Start......")
try:
image_to_txt(name)
except:
print("Error!!!!!!")
print("Over......")