Dabarti_Capture折腾吐血
2019-04-01 本文已影响0人
皿卜土
python调用外部程序,需要安装pywin32,命令行参数最前面需要有一个空格。
# coding=UTF-8
import win32process
import win32event
import time
import sys
# --------------------------#
# py -2 test.py hello world #
# --------------------------#
myStr = ' '
for i in range(1, len(sys.argv)):
myStr += sys.argv[i] + ' '
handle = win32process.CreateProcess(
'd:\\putin\\console1.exe', # 可执行文件名
myStr, # 命令行参数
None, # 默认进程安全性
None, # 默认线程安全性
0, # 继承标志
win32process.CREATE_NEW_CONSOLE, # 为新进程创建一个新的控制台窗口
None, # 使用本进程的环境变量
None, # 使用本进程的驱动器和目录
win32process.STARTUPINFO()
)
stop = win32event.WaitForSingleObject(handle[0], -1)
if stop == 0:
print("dajiahao.")
拆分大图片
# coding=UTF-8
import os
import cv2
import numpy as np
pic = []
light = []
mask = ''
file_dir = '.\\sample\\'
dir_list = os.listdir(file_dir)
for cur_file in dir_list:
# 获取文件的绝对路径
path = os.path.join(file_dir, cur_file)
if os.path.isfile(path): # 判断是否是文件还是目录需要用绝对路径
if cur_file[0:3] == 'pic':
pic.append(cur_file)
if cur_file[0:3] == 'lig':
light.append(cur_file)
if cur_file[0:3] == 'mas':
mask = cur_file
print "{0} : is file!".format(path)
"""
输入:图片路径(path+filename),裁剪获得小图片的列数、行数(也即宽、高)
"""
def clip_one_picture(path, filename, cols, rows):
img = cv2.imread(
path + filename, -1
) # 读取彩色图像,图像alpha通道被忽略,可以用1,0,-1来表示
sum_rows, sum_cols = img.shape[:2] # 高度 宽度
save_path = path + "\\crop{0}_{1}\\".format(cols, rows) # 保存的路径
if not os.path.exists(save_path):
os.makedirs(save_path)
#执行chcp 65001就可以把cmd的编码设置成uft-8了
print("裁剪所得{0}列图片,{1}行图片.".format(
int(sum_cols / cols), int(sum_rows / rows)))
for i in range(int(sum_cols / cols) + 1):
for j in range(int(sum_rows / rows) + 1):
cv2.imwrite(
save_path + os.path.splitext(filename)[0] + '_' + str(j) +
'_' + str(i) + os.path.splitext(filename)[1],
img[j * rows:(j + 1) * rows, i * cols:(i + 1) * cols, :])
image = cv2.imread(file_dir + light[0])
width = 1024 - image.shape[0]
hight = 1024 - image.shape[1]
clip_one_picture(file_dir, pic[0], width, hight)
填充进1024图片
# coding=UTF-8
import os
import cv2
import numpy as np
file_dir = '.\\sample\\'
logo = cv2.imread(file_dir + "light0000.png")
srcImage = cv2.imread(file_dir + "crop836_836\\pic0000_1_2.png")
I = np.zeros((1024, 1024), dtype=np.uint8)
I = cv2.cvtColor(I, cv2.COLOR_GRAY2BGR)
lightROI = I[0:logo.shape[0], 1024-logo.shape[1]:1024]
srcROI = I[1024-srcImage.shape[0]:1024, 0:srcImage.shape[1]]
cv2.addWeighted(lightROI, 0, logo, 1, 0, lightROI)
cv2.addWeighted(srcROI, 0, srcImage, 1, 0, srcROI)
cv2.imwrite(file_dir + "merge.png", I)
运算
再合并
分割图片的进度
# coding=UTF-8
import sys
import getopt
import os
import cv2
import numpy as np
import win32process
import win32event
class SingleBlock:
path = ''
def __init__(self, name, size):
self.__name = name
self.__size = size
def get_name(self):
return self.__name
def get_size(self):
return self.__size
class Action:
__crop_dirs = []
def __init__(self, path):
self.__path = path
def get_path(self):
return self.__path
def execute_action(self):
print("总路径: {0}".format(self.__path))
dirs = self.get_dirs()
files = []
if len(dirs) == 0:
print("没有文件夹可供计算")
else:
for dir in dirs:
self.single_dir_split(dir)
if len(self.__crop_dirs) > 0:
print("开始计算,数量为:{0}".format(len(self.__crop_dirs)))
self.calculate(self.__crop_dirs[0])
# 单个目录分割
def single_dir_split(self, dir):
# 获取文件夹名称
strlist = list(filter(None, dir.split('\\')))
dir_name = strlist[len(strlist) - 1]
files = self.get_files(dir)
if len(files) != 17:
print("{0}没有文件夹可供计算".format(dir))
else:
mask = ""
pics = []
for file in files:
if "mask" in file:
mask = file
if "pic" in file:
pics.append(file)
img = cv2.imread(mask, -1)
msize = img.shape[0]
self.split_files(pics, msize)
def calculate(self, arg):
myStr = ' ' + arg
print("计算:{0}".format(myStr))
handle = win32process.CreateProcess(
'd:\\putin\\console1.exe', # 可执行文件名
myStr, # 命令行参数
None, # 默认进程安全性
None, # 默认线程安全性
0, # 继承标志
win32process.CREATE_NEW_CONSOLE, # 为新进程创建一个新的控制台窗口
None, # 使用本进程的环境变量
None, # 使用本进程的驱动器和目录
win32process.STARTUPINFO())
del self.__crop_dirs[0]
if len(self.__crop_dirs) > 0:
stop = win32event.WaitForSingleObject(handle[0], -1)
if stop == 0:
self.calculate(self.__crop_dirs[0])
else:
print("计算结束")
# 分割图像,并且放在相应的目录
def split_files(self, files, msize):
size = 1024 - msize
for file in files:
self.clip_file(file, size)
def clip_file(self, file, size):
img = cv2.imread(file)
sum_rows, sum_cols = img.shape[:2]
path = file[0:file.rfind('\\', 1) + 1]
filename = file[file.rfind('\\', 1) + 1:]
for i in range(int(sum_cols / size) + 1):
for j in range(int(sum_rows / size) + 1):
save_path = path + "crop{0}_{1}\\".format(i, j)
if save_path not in self.__crop_dirs:
self.__crop_dirs.append(save_path)
self.create_dir(save_path)
crop_img = img[j * size:(j + 1) * size, i * size:(i + 1) *
size, :]
save_full_path = save_path + os.path.splitext(
filename)[0] + '_' + str(j) + '_' + str(
i) + os.path.splitext(filename)[1]
self.write_pic(crop_img, size, path, filename, save_full_path)
print("path: {0} filename: {1}".format(path, filename))
def write_pic(self, crop_img, size, path, filename, save_full_path):
lightname = filename.replace('pic', 'light')
logo = cv2.imread(path + lightname)
I = np.zeros((1024, 1024), dtype=np.uint8)
I = cv2.cvtColor(I, cv2.COLOR_GRAY2BGR)
# print("尺寸为:{0}, {1}".format(crop_img.shape[0], crop_img.shape[1]))
lightROI = I[0:logo.shape[0], 1024 - logo.shape[1]:1024]
srcROI = I[1024 - crop_img.shape[0]:1024, 0:crop_img.shape[1]]
cv2.addWeighted(lightROI, 0, logo, 1, 0, lightROI)
cv2.addWeighted(srcROI, 0, crop_img, 1, 0, srcROI)
cv2.imwrite(save_full_path, I)
def create_dir(self, path):
if not os.path.exists(path):
os.makedirs(path)
# 获取需要计算的文件夹
def get_dirs(self):
dirs = []
dir_list = os.listdir(self.__path)
# 获取要计算的文件夹路径
for cur_file in dir_list:
path = os.path.join(self.__path, cur_file)
if os.path.isdir(path):
dirs.append(path + "\\")
print(path)
print("计算{0}个文件夹".format(len(dirs)))
return dirs
# 获取单个文件夹里的文件
def get_files(self, dir):
files = []
file_list = os.listdir(dir)
for cur_file in file_list:
path = os.path.join(dir, cur_file)
if os.path.isfile(path):
files.append(path)
return files
def main(argv):
if len(argv) <= 1:
print("请输入路径参数")
else:
# args = getopt.getopt(argv[1:])
topPath = ".\\" + argv[1] + "\\"
action = Action(topPath)
action.execute_action()
if __name__ == "__main__":
main(sys.argv)
一不小心把软件破解了,就此打住了。