word打开图片排版错乱怎么办?(word图片排版不乱)
765
2022-05-29
➤
01
背景
在 2020年人工神经网络课程第一次作业第八题 中需要对 Lena 图像使用AutoEncode网络进行压缩。将Lena(灰度图像)拆解成不同尺寸的大小形成训练压缩样本过程;或者从训练结果重新组合成Lena灰度图像是实验的基础。
下面给出相关操作的Python程序和相关的结果。
主要操作包括:
将512×512的Lena灰度图片(0 ~ 255)分割成边长8 ~ 16的图像块,并通过行扫描形行向量;
对图像进行归一化,形成数据在 -0.5 ~ 0.5之间的数据;
将训练结果恢复到0 ~ 255并组合成灰度图片,存盘,或者显示。
➤
02
图像分割
从Lena下载Lean灰度(512×512)的图片数据,存储在本地目录下:lean_gray.bmp。文件格式为BMP。
1.读取数据分割
blocksize : 图像块的大小:8 ~ 32
image_file :图像文件
outdata: 2D-array。每一行是图像block所形成的一位数据;总行数为block数量;
数据格式:float: -0.5 ~ 0.5
def lena2block(blocksize,image_file): img = Image.open(image_file).convert('RGB') imgdata = array(img)[:,:,0].astype(float32) imgheight = imgdata.shape[0] imgwidth = imgdata.shape[1] imgdata = (imgdata - 128) / 256 printf(imgdata.shape) outdata = [] for i in range(0, imgheight, blocksize): for j in range(0, imgwidth, blocksize): blockdata = imgdata[i:i+blocksize, j:j+blocksize].reshape(1, -1) if len(outdata) == 0: outdata = blockdata else: outdata = vstack((outdata, blockdata)) return outdata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2.测试上述模块
下面测试上面的lena2block程序,并将分割成的小图像块重新进行显示的结果。
tsprefreshimagebuffer(show_id) SHOW_LINES = 16 SHOW_COLS = 16 TEMP_FILE = r'd:\temp.bmp' for i in range(SHOW_LINES): for j in range(SHOW_COLS): blockid = i * 16 + j newimage = Image.fromarray((outdata[blockid].reshape(IMAGE_BLOCK_SIZE, IMAGE_BLOCK_SIZE) * 256 + 128).astype(uint8)) newimage.save(TEMP_FILE) x = j * (IMAGE_BLOCK_SIZE + 2) y = i * (IMAGE_BLOCK_SIZE + 2) tspshowimage(show_id, x, y, x + IMAGE_BLOCK_SIZE, y + IMAGE_BLOCK_SIZE, TEMP_FILE) tsprv()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
➤
03
图像合成
将前面分割的图像重新整合成lena图片图片。
1.图片合成代码
def block2lena(blockdata): blocknum = blockdata.shape[0] blocklen = blockdata.shape[1] block_size = int(sqrt(blocklen)) image_block_size = int(sqrt(blocknum)) block_line = [] for i in range(image_block_size): block_row = hstack([b.reshape(block_size, block_size) \ for b in blockdata[i*image_block_size:(i+1)*image_block_size]]) block_line.append(block_row) imagedata = vstack(block_line) imagedata = (imagedata * 256 + 128) imagedata[imagedata < 0] = 0 imagedata[imagedata > 255] = 255 return imagedata.astype(uint8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2.测试
newdata = block2lena(outdata) printf(newdata.shape) newimage = Image.fromarray(newdata) newimage.show()
1
2
3
4
为了显示对于block处理后的效果,下面对于每一块都将前面一半填充0,然后再合成。
for b in outdata: b[0:len(b)//2] = 0
1
2
因此显示的结果如下。
从上面结果来看,对于每一小块的处理最终体现在合成后的图片中。这也说明整个的图片的分割与合成程序功能正常。这位之后对2020年人工神经网络课程第一次作业第八题的效果进行评估提供了可视化的子函数。
➤
※ 结论
对于Lena灰度图像,通过两个子函数可以完成对其切分成小块,然后再合成,这由于2020年人工神经网络课程第一次作业第八题中的结果进行显示。便于评估图像处理结果。
□ 实验Python程序
#!/usr/local/bin/python # -*- coding: gbk -*- #============================================================ # SUBIMAGE.PY -- by Dr. ZhuoQing 2020-11-23 # # Note: #============================================================ from headm import * from PIL import Image #------------------------------------------------------------ lena_gray = r'D:\Temp\lena_gray.bmp' show_id = 6 def lena2block(blocksize,image_file): img = Image.open(image_file).convert('RGB') imgdata = array(img)[:,:,0].astype(float32) imgheight = imgdata.shape[0] imgwidth = imgdata.shape[1] imgdata = (imgdata - 128) / 256 printf(imgdata.shape) outdata = [] for i in range(0, imgheight, blocksize): for j in range(0, imgwidth, blocksize): blockdata = imgdata[i:i+blocksize, j:j+blocksize].reshape(1, -1) if len(outdata) == 0: outdata = blockdata else: outdata = vstack((outdata, blockdata)) return outdata #------------------------------------------------------------ def block2lena(blockdata): blocknum = blockdata.shape[0] blocklen = blockdata.shape[1] block_size = int(sqrt(blocklen)) image_block_size = int(sqrt(blocknum)) block_line = [] for i in range(image_block_size): block_row = hstack([b.reshape(block_size, block_size) \ for b in blockdata[i*image_block_size:(i+1)*image_block_size]]) block_line.append(block_row) imagedata = vstack(block_line) imagedata = (imagedata * 256 + 128) imagedata[imagedata < 0] = 0 imagedata[imagedata > 255] = 255 return imagedata.astype(uint8) #------------------------------------------------------------ IMAGE_BLOCK_SIZE = 32 outdata = lena2block(IMAGE_BLOCK_SIZE, lena_gray) printf(outdata.shape) #------------------------------------------------------------ tsprefreshimagebuffer(show_id) SHOW_LINES = 16 SHOW_COLS = 16 TEMP_FILE = r'd:\temp.bmp' for i in range(SHOW_LINES): for j in range(SHOW_COLS): blockid = i * 16 + j newimage = Image.fromarray((outdata[blockid].reshape(IMAGE_BLOCK_SIZE, IMAGE_BLOCK_SIZE) * 256 + 128).astype(uint8)) newimage.save(TEMP_FILE) x = j * (IMAGE_BLOCK_SIZE + 2) y = i * (IMAGE_BLOCK_SIZE + 2) tspshowimage(show_id, x, y, x + IMAGE_BLOCK_SIZE, y + IMAGE_BLOCK_SIZE, TEMP_FILE) tsprv() #------------------------------------------------------------ for b in outdata: b[0:len(b)//2] = 0 newdata = block2lena(outdata) printf(newdata.shape) newimage = Image.fromarray(newdata) newimage.show() #------------------------------------------------------------ # END OF FILE : SUBIMAGE.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
88
89
90
91
Python
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。