146 lines
5.4 KiB
Python
146 lines
5.4 KiB
Python
import base64,hashlib,os,shutil
|
|
import numpy as np
|
|
from PIL import Image
|
|
|
|
class imageUtils:
|
|
|
|
@classmethod
|
|
def encodeImage(cls,enStr):
|
|
print("en",enStr)
|
|
enc = base64.b64decode(enStr)
|
|
print("解密:",enc)
|
|
m = hashlib.md5()
|
|
m.update(enc)
|
|
md5 = m.digest()
|
|
d = md5[-1]
|
|
print(md5)
|
|
try:
|
|
blocks = d % 10 + 5
|
|
except:
|
|
blocks = 0 %10 + 5
|
|
print("blocks=",blocks)
|
|
return blocks
|
|
|
|
@classmethod
|
|
def scrambleImage(cls,file_path):
|
|
#检测到未下载完的图像 直接返回None
|
|
if str(file_path).endswith(".downloads"):
|
|
os.remove(file_path)
|
|
return None
|
|
file_str = str(file_path).split("=")
|
|
#10_29.jpg
|
|
baseDir = file_str[0].replace("scramble","")
|
|
baseName = file_str[-1]
|
|
baseFN = baseName.split("_")
|
|
save_name = baseFN[1]
|
|
save_name_delesu = save_name.split(".")[0]
|
|
blocks = int(baseFN[0])
|
|
save_file_path = os.path.join(baseDir,save_name)
|
|
print("sva",save_file_path)
|
|
if os.path.exists(save_file_path):
|
|
print("图片已解密,已跳过:", save_file_path)
|
|
return None
|
|
image_su = str(file_path).split(".")[-1]
|
|
try:
|
|
img = Image.open(file_path)
|
|
except:
|
|
print(f"error Image")
|
|
width = img.width
|
|
height = img.height
|
|
#blocks = cls.encodeImage(enStr)
|
|
print("blocks=",blocks)
|
|
blockHeight = int(height / blocks)
|
|
blockWidth = int(width / blocks)
|
|
print("blockHeight=",blockHeight)
|
|
su = str(file_path).split(".")[-1]
|
|
split_path = os.path.join(baseDir,save_name_delesu+"split")
|
|
if image_su == "downloads":
|
|
return None
|
|
is_split = cls.splitimage(file_path,blocks,1,split_path)
|
|
if not is_split == None:
|
|
cls.image_compose(split_path,blocks,1,save_file_path,blockHeight,width)
|
|
else:
|
|
if os.path.exists(split_path):
|
|
shutil.rmtree(split_path)
|
|
if os.path.exists(file_path):
|
|
shutil.move(file_path, save_file_path)
|
|
#完成后清空
|
|
return file_path
|
|
|
|
@classmethod
|
|
def splitimage(cls,src,rownum,colnum,dstpath):
|
|
img=Image.open(src)
|
|
w,h=img.size
|
|
if rownum<= h and colnum<=w:
|
|
s=os.path.split(src)
|
|
if dstpath=='':
|
|
dstpath = s[0]
|
|
if not os.path.exists(dstpath):
|
|
os.makedirs(dstpath)
|
|
fn=s[1].split('.')
|
|
basename=fn[0]
|
|
ext=fn[-1]
|
|
num=0
|
|
rowheight=h//rownum
|
|
colwidth=w//colnum
|
|
for r in range(rownum):
|
|
for c in range(colnum):
|
|
box=(c*colwidth,r*rowheight,(c+1)*colwidth,(r+1)*rowheight)
|
|
count_image = "{:0>3d}".format(num)
|
|
file_path = os.path.join(dstpath,str(count_image)+'.'+ext)
|
|
print("file_path=",file_path)
|
|
img.crop(box).save(file_path)
|
|
num=num+1
|
|
return "成功"
|
|
else:
|
|
print('不数!')
|
|
return None
|
|
|
|
@classmethod
|
|
def image_compose(cls,src,row,column,save_path,image_height,image_width):
|
|
image_size = image_height
|
|
#image_height = 376
|
|
#image_width = 720
|
|
images_format = ['.png','.jpg']
|
|
|
|
#image_names = [name for name in os.listdir(src) for item in images_format if
|
|
# os.path.splitext(name)[1] == item][::-1]
|
|
img_list=os.listdir(src)
|
|
img_list.sort()
|
|
img_list.sort(key=lambda x: int(x[:-4]))
|
|
##文件名按数字排序
|
|
img_nums=len(img_list)
|
|
image_names = []
|
|
for i in range(img_nums):
|
|
img_name=os.path.join(src,img_list[i])
|
|
image_names.append(img_name)
|
|
#使用倒序
|
|
image_names = image_names[::-1]
|
|
# 简单的对于参数的设定和实际图片集的大小进行数量判断
|
|
if len(image_names) < row * column:
|
|
raise ValueError("合成图片的参数和要求的数量不能匹配!")
|
|
|
|
to_image = Image.new('RGB', (column * image_width, row * image_height)) #创建一个新图
|
|
# 循环遍历,把每张图片按顺序粘贴到对应位置上
|
|
for y in range(1, row + 1):
|
|
for x in range(1, column + 1):
|
|
#1 * (row=1 -1) col=1 -1
|
|
image_path = image_names[column * (y - 1) + x - 1]
|
|
print("split_image=",image_path)
|
|
from_image = Image.open(image_path)
|
|
#保持原图片大小
|
|
#.resize(
|
|
# (image_size, image_size),Image.ANTIALIAS)
|
|
to_image.paste(from_image, ((x - 1) * image_size, (y - 1) * image_size))
|
|
from_image.close()
|
|
to_image.save(save_path)
|
|
print("图片合并完成:", save_path)
|
|
shutil.rmtree(src)
|
|
# 保存新图
|
|
|
|
@classmethod
|
|
def getScrambleImage(cls,path):
|
|
scrambleFileCache = cls.scrambleImage(path)
|
|
if not scrambleFileCache == None:
|
|
if os.path.exists(scrambleFileCache):
|
|
os.remove(scrambleFileCache) |