File size: 1,054 Bytes
c10aebf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
import os
from PIL import Image

import sd_bmab
from sd_bmab import util


def check_cache_dir(path):
	if not os.path.exists(path):
		os.mkdir(path)


def get_noise_from_cache(seed, width, height):
	path = os.path.dirname(sd_bmab.__file__)
	path = os.path.normpath(os.path.join(path, '../resources/cache'))
	check_cache_dir(path)
	cache_file = f'{path}/noise_{width}_{height}.png'
	if os.path.isfile(cache_file):
		return Image.open(cache_file)
	img = util.generate_noise(seed, width, height)
	img.save(cache_file)
	return img


def get_image_from_cache(filename):
	path = os.path.dirname(sd_bmab.__file__)
	path = os.path.normpath(os.path.join(path, '../resources/cache'))
	check_cache_dir(path)
	full_path = os.path.join(path, filename)
	if os.path.exists(full_path):
		return Image.open(full_path)
	return None


def put_image_to_cache(filename, image):
	path = os.path.dirname(sd_bmab.__file__)
	path = os.path.normpath(os.path.join(path, '../resources/cache'))
	check_cache_dir(path)
	full_path = os.path.join(path, filename)
	image.save(full_path)