Python常用脚本

图片裁剪、分割、拼接

将图片从50612803先裁剪成50612003,补充0到60012003,按照3003003的大小进行分割显示,最后拼接出来原图

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
import matplotlib.image as mpimg
import numpy as np
import cv2

path = '../demo/'
image_names = 'A_three_test.png'
img = cv2.imread(path + image_names)

new_img = img[:,40:1240]
new_img = np.lib.pad(new_img, ((47,47),(0,0),(0,0)), 'constant', constant_values=(0,0))

# new_img = np.resize(img, (600, 1200))

img_shape = np.shape(new_img)
print img_shape, type(new_img)

size = 300
height_number = img_shape[0]/size # 2
width_number = img_shape[1]/size # 4

import PIL.Image as Image
toImage = Image.new('RGBA', (img_shape[1], img_shape[0]))

count = 0
for x in range(height_number):
for y in range(width_number):
cut_img = new_img[x*size:(x+1)*size, y*size:(y+1)*size]
# print np.shape(cut_img)
cv2.imwrite("%s.jpg" %(count), cut_img)
cut_img = Image.open("%s.jpg" %(count))
print (x * size, y * size)
toImage.paste(cut_img, (y * size, x * size))
cv2.imshow('img', np.array(cut_img))
while 1:
k = cv2.waitKey(10) & 0xff
if k == 27:
break
count += 1
toImage.save('./toImage_0.jpg')
cv2.destroyAllWindows()

删除字典中的特定项

删除字典中的特定项,从中删除’QAM16’, ‘QAM64’,从snr_del = list(np.arange(-20, 20, 2)),并打包成新的字典:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# -*- coding: utf-8 -*-
"""
@author: hzy
"""
import numpy as np

import cPickle
mod_del = ['QAM16', 'QAM64']
snr_del = list(np.arange(-20, 20, 2))

with open("./RML2016.10a_dict.dat", 'rb') as xd1:
Xd = cPickle.load(xd1) # ,encoding='latin1'
for each_mod in mod_del:
for each_snr in snr_del:
tmp = "'%s',%d" %(each_mod, each_snr)
tmp = eval(tmp)
del Xd[tmp]
cPickle.dump(Xd, file("test.dat", "wb"))

将一个文件夹中图片以及类标写入txt

将一个文件夹中图片以及类标(0到N)写入txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# -*- coding:utf-8 -*-
import random

import numpy as np
import os

src_path = './trainSNRm13'
name_list = ['am','ask','bpsk','CW','epsk','fm','fsk','hyp','LLFM','par','POWER','qpsk','sin']

if __name__ == '__main__':
picture_list = os.listdir(src_path)
count = len(picture_list)
random.shuffle(picture_list)
with open('./train.txt', 'w') as f:
for i, picture_name in enumerate(picture_list):
for index, name in enumerate(name_list):
if name in picture_name:
if i != count-1:
f.write(picture_name+' '+str(index))
f.write('\n')
else:
f.write(picture_name + ' ' + str(index))
f.close()
print('write success!')

写多个文件夹图片路径及其类标到txt

写多个文件夹图片路径及其类标 (0到N) ,并打乱到txt

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
# -*- coding:utf-8 -*-
import random
import numpy as np
import os

src_path = './testinit'
name_list = ['am', 'ask', 'bpsk', 'CW', 'epsk', 'fm', 'fsk', 'hyp', 'LLFM', 'par', 'power', 'qpsk', 'sin']
name_length = len(name_list)
# count = 0

def create_txt(picture_list):
for i, picture_name in enumerate(picture_list):
for index, name in enumerate(name_list):
if name in picture_name:
f.write(picture_name + ' ' + str(index))
f.write('\n')
else:
continue


if __name__ == '__main__':
folder_list = os.listdir(src_path)
with open('./test.txt', 'w') as f:
for folder in folder_list:
folder = src_path +'/'+ folder
picture_list = os.listdir(folder)
random.shuffle(picture_list)
# count += len(picture_list)
create_txt(picture_list)
f.close()
print('write success!')

复制多个文件夹图片到一个文件夹

代码如下:

目录testint有多个文件夹,每个文件夹里面有多张图片,复制所有图片到testint同级的val文件夹下

1
2
3
4
5
6
7
8
9
10
import os

src_path = './testinit'
src_path_ = '.'

if __name__ == '__main__':
dir_list = os.listdir(src_path)
for i in dir_list:
p = os.path.join(src_path, i)
os.system('cp %s/* %s/val' % (p, src_path_))
------ 本文结束------
坚持原创技术分享,您的支持将鼓励我继续创作!

欢迎关注我的其它发布渠道