now with parrallel
This commit is contained in:
parent
cdb18baf1f
commit
48ac1e1392
120
main.py
120
main.py
|
@ -3,32 +3,10 @@ import pickle
|
||||||
from typing import List
|
from typing import List
|
||||||
from PIL import Image, ImageFile
|
from PIL import Image, ImageFile
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
from multiprocessing import Pool, Manager
|
||||||
|
|
||||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||||
|
|
||||||
target_path = input('Input relative path for target image: ')
|
|
||||||
Image.open(target_path)
|
|
||||||
|
|
||||||
user_input = input('Input segment width count (Higher=more pictures used. Default = 40): ')
|
|
||||||
global width_segments_count
|
|
||||||
try:
|
|
||||||
width_segments_count = int(user_input)
|
|
||||||
except:
|
|
||||||
width_segments_count = 40
|
|
||||||
|
|
||||||
user_input = input('Input segment height count (Higher=more pictures used. Default = 40): ')
|
|
||||||
global height_segments_count
|
|
||||||
try:
|
|
||||||
height_segments_count = int(user_input)
|
|
||||||
except:
|
|
||||||
height_segments_count = 40
|
|
||||||
|
|
||||||
user_input = input('Input quality for filling pictures (1=default, 2=double the size): ')
|
|
||||||
global FILLING_QUALITY
|
|
||||||
try:
|
|
||||||
FILLING_QUALITY = int(user_input)
|
|
||||||
except:
|
|
||||||
FILLING_QUALITY = 1
|
|
||||||
|
|
||||||
class PictureInfos:
|
class PictureInfos:
|
||||||
def __init__(self, image_path, color, used):
|
def __init__(self, image_path, color, used):
|
||||||
self.image_path = image_path
|
self.image_path = image_path
|
||||||
|
@ -67,7 +45,7 @@ def get_picture_names(file_path):
|
||||||
return os.listdir(file_path)
|
return os.listdir(file_path)
|
||||||
|
|
||||||
|
|
||||||
def get_closest(target):
|
def get_closest(target, allPictures):
|
||||||
image: PictureInfos
|
image: PictureInfos
|
||||||
closest = np.inf
|
closest = np.inf
|
||||||
closest_path = ''
|
closest_path = ''
|
||||||
|
@ -83,12 +61,57 @@ def get_closest(target):
|
||||||
closest_path = image.image_path
|
closest_path = image.image_path
|
||||||
return closest_path
|
return closest_path
|
||||||
|
|
||||||
|
def create_picture_segment(target, allPictures, starting_layer, end_layer, width_segments_count, PIXELS_TO_FILL_WIDTH, PIXELS_TO_FILL_HEIGHT, FILLING_QUALITY):
|
||||||
|
picture_piece = Image.new('RGB', (int(width_segments_count*PIXELS_TO_FILL_WIDTH*FILLING_QUALITY), int((end_layer-starting_layer)*PIXELS_TO_FILL_HEIGHT*FILLING_QUALITY)))
|
||||||
|
layer_offset = 0
|
||||||
|
for layer in target[int(starting_layer):int(end_layer)]:
|
||||||
|
segment_offset = 0
|
||||||
|
for segment in layer:
|
||||||
|
closest = get_closest(segment, allPictures)
|
||||||
|
closest_img = Image.open(closest)
|
||||||
|
closest_img = closest_img.resize((PIXELS_TO_FILL_WIDTH*FILLING_QUALITY, PIXELS_TO_FILL_HEIGHT*FILLING_QUALITY))
|
||||||
|
picture_piece.paste(closest_img, (segment_offset*PIXELS_TO_FILL_WIDTH*FILLING_QUALITY, layer_offset*PIXELS_TO_FILL_HEIGHT*FILLING_QUALITY))
|
||||||
|
segment_offset +=1
|
||||||
|
layer_offset += 1
|
||||||
|
print(layer_offset,'Layer done')
|
||||||
|
return picture_piece
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
source_categories = os.listdir('./101_ObjectCategories')
|
|
||||||
|
|
||||||
if 'allPictures.pkl' not in os.listdir():
|
if __name__ == '__main__':
|
||||||
|
### Starting sequence ###
|
||||||
|
target_path = input('Input relative path for target image: ')
|
||||||
|
Image.open(target_path)
|
||||||
|
|
||||||
|
user_input = input('Input segment width count (Higher=more pictures used. Default = 100. Musn\'t exceed pixel of original): ')
|
||||||
|
try:
|
||||||
|
width_segments_count = int(user_input)
|
||||||
|
except:
|
||||||
|
width_segments_count = 100
|
||||||
|
|
||||||
|
user_input = input('Input segment height count (Higher=more pictures used. Default = 100. Musn\'t exceed pixel of original): ')
|
||||||
|
try:
|
||||||
|
height_segments_count = int(user_input)
|
||||||
|
except:
|
||||||
|
height_segments_count = 100
|
||||||
|
|
||||||
|
user_input = input('Input quality for filling pictures (1=default, 2=double the size): ')
|
||||||
|
try:
|
||||||
|
FILLING_QUALITY = int(user_input)
|
||||||
|
except:
|
||||||
|
FILLING_QUALITY = 1
|
||||||
|
|
||||||
|
user_input = input('Input worker count: (default 1) Recomended PC\'s cores if you dont want to kill your PC: ')
|
||||||
|
try:
|
||||||
|
working_size = int(user_input)
|
||||||
|
except:
|
||||||
|
working_size = 1
|
||||||
|
|
||||||
|
### Getting all pictures ###
|
||||||
|
|
||||||
|
if 'allPictures.pkl' not in os.listdir():
|
||||||
|
source_categories = os.listdir('./101_ObjectCategories')
|
||||||
allPictures = list()
|
allPictures = list()
|
||||||
print('Getting all pictures')
|
print('Getting all pictures')
|
||||||
for category in source_categories:
|
for category in source_categories:
|
||||||
|
@ -102,30 +125,35 @@ if 'allPictures.pkl' not in os.listdir():
|
||||||
with open('allPictures.pkl', 'wb') as file:
|
with open('allPictures.pkl', 'wb') as file:
|
||||||
pickle.dump(allPictures, file)
|
pickle.dump(allPictures, file)
|
||||||
print('done')
|
print('done')
|
||||||
else:
|
else:
|
||||||
with open('allPictures.pkl', 'rb') as file:
|
with open('allPictures.pkl', 'rb') as file:
|
||||||
allPictures = pickle.load(file)
|
allPictures = pickle.load(file)
|
||||||
|
|
||||||
print('Segmenting target')
|
print('Segmenting target')
|
||||||
target = get_color_space_array(target_path)
|
target = get_color_space_array(target_path)
|
||||||
print('Segmenting done')
|
print('Segmenting done')
|
||||||
|
|
||||||
|
### Start to create image ###
|
||||||
|
# print((int(width_segments_count*PIXELS_TO_FILL_WIDTH*FILLING_QUALITY), int(height_segments_count*PIXELS_TO_FILL_HEIGHT*FILLING_QUALITY)))
|
||||||
|
|
||||||
|
|
||||||
# print((int(width_segments_count*PIXELS_TO_FILL_WIDTH*FILLING_QUALITY), int(height_segments_count*PIXELS_TO_FILL_HEIGHT*FILLING_QUALITY)))
|
|
||||||
|
|
||||||
new_im = Image.new('RGB', (int(width_segments_count*PIXELS_TO_FILL_WIDTH*FILLING_QUALITY), int(height_segments_count*PIXELS_TO_FILL_HEIGHT*FILLING_QUALITY)))
|
working_length = np.floor(len(target)/working_size)
|
||||||
|
|
||||||
|
pool = Pool()
|
||||||
|
picture_segments = pool.starmap(create_picture_segment, [(target, allPictures, working_length*i, working_length*(i+1), width_segments_count, PIXELS_TO_FILL_WIDTH, PIXELS_TO_FILL_HEIGHT, FILLING_QUALITY) for i in range(working_size)])
|
||||||
|
pool.close()
|
||||||
|
pool.join()
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
print(picture_segments)
|
||||||
|
|
||||||
|
new_im = Image.new('RGB', (int(width_segments_count*PIXELS_TO_FILL_WIDTH*FILLING_QUALITY), int(height_segments_count*PIXELS_TO_FILL_HEIGHT*FILLING_QUALITY)))
|
||||||
|
for segment in picture_segments:
|
||||||
|
new_im.paste(segment, (0, int(i*PIXELS_TO_FILL_HEIGHT*FILLING_QUALITY*working_length)))
|
||||||
|
i +=1
|
||||||
|
|
||||||
|
new_im.show()
|
||||||
|
new_im.save('final.jpg')
|
||||||
|
|
||||||
layer_offset = 0
|
|
||||||
for layer in target:
|
|
||||||
segment_offset = 0
|
|
||||||
for segment in layer:
|
|
||||||
closest = get_closest(segment)
|
|
||||||
closest_img = Image.open(closest)
|
|
||||||
closest_img = closest_img.resize((PIXELS_TO_FILL_WIDTH*FILLING_QUALITY, PIXELS_TO_FILL_HEIGHT*FILLING_QUALITY))
|
|
||||||
new_im.paste(closest_img, (segment_offset*PIXELS_TO_FILL_WIDTH*FILLING_QUALITY, layer_offset*PIXELS_TO_FILL_HEIGHT*FILLING_QUALITY))
|
|
||||||
segment_offset +=1
|
|
||||||
layer_offset += 1
|
|
||||||
print(layer_offset,'Layer done')
|
|
||||||
|
|
||||||
new_im.show()
|
|
||||||
new_im.save('final.jpg')
|
|
|
@ -0,0 +1,20 @@
|
||||||
|
from multiprocessing import Pool, Manager
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
|
||||||
|
def some_function(some_input):
|
||||||
|
time.sleep(random.random())
|
||||||
|
return some_input
|
||||||
|
|
||||||
|
def test():
|
||||||
|
global testStr
|
||||||
|
testStr = 'Hello'
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test()
|
||||||
|
pool = Pool()
|
||||||
|
manager = Manager()
|
||||||
|
print(pool.map(some_function, [('Hello') for i in range(4)]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue