我正在使用代码创建随机图像,然后检查该图像的哈希值并将其与已知的哈希值进行比较。这是我的代码:
import os
import time
import hashlib
import numpy
from io import BytesIO
from PIL import Image
def create_image(width = 1920, height = 1080, num_of_images = 100):
width = int(width)
height = int(height)
num_of_images = int(num_of_images)
current = time.strftime("%Y%m%d%H%M%S")
os.mkdir(current)
i=0
for n in range(num_of_images):
filename = '{0}/{0}_{1:03d}.jpg'.format(current, n)
rgb_array = numpy.random.rand(height,width,3) * 255
image = Image.fromarray(rgb_array.astype('uint8')).convert('RGB')
image.save(filename)
imagee = open(filename, "rb").read()
hashnum = hashlib.md5(imagee).hexdigest()
if(hashnum=="B3D740C2F83F7EE120FD16EAED266B43"):
image.save(filename)
print(filename)
else:
os.remove(filename)
i+=1
print("Done with ", i, end='\r')
def main(args):
create_image(width = args[0], height = args[1], num_of_images = args[2])
return 0
if __name__ == '__main__':
import sys
status = main(sys.argv[1:])
sys.exit(status)
到目前为止,该程序每秒处理大约1k 17宽14高度的图像,我想知道这是否会更有效。我考虑过删除整个保存映像,然后删除,但是我不确定如何做到这一点。同样抱歉,如果此帖子有错误,英语不是我的母语。