如何提高图像生成和哈希的效率?



我正在使用代码创建一个随机图像,然后检查该图像的哈希值并将其与已知的哈希值进行比较。这是我的代码:

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 高度的图像,我想知道这是否可以更有效。我想过摆脱整个保存图像,然后删除,但我不确定该怎么做。也很抱歉,如果这篇文章有错误,英语不是我的第一语言。

此答案向您展示了如何避免在散列之前将映像保存到磁盘。

删除磁盘写入/读取应该会使其明显更快

最新更新