diff --git a/main.py b/main.py index 8e2552c..7576d34 100644 --- a/main.py +++ b/main.py @@ -3,32 +3,10 @@ import pickle from typing import List from PIL import Image, ImageFile import numpy as np +from multiprocessing import Pool, Manager + 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: def __init__(self, image_path, color, used): self.image_path = image_path @@ -67,7 +45,7 @@ def get_picture_names(file_path): return os.listdir(file_path) -def get_closest(target): +def get_closest(target, allPictures): image: PictureInfos closest = np.inf closest_path = '' @@ -83,49 +61,99 @@ def get_closest(target): closest_path = image.image_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(): - allPictures = list() - print('Getting all pictures') - for category in source_categories: - imgs_path = './101_ObjectCategories/'+category - category_images = get_picture_names(imgs_path) - for img in category_images: - img_path = imgs_path +'/'+img - allPictures.append(PictureInfos(img_path, get_average_color(img_path), False)) - print(category,'done') +if __name__ == '__main__': + ### Starting sequence ### + target_path = input('Input relative path for target image: ') + Image.open(target_path) - with open('allPictures.pkl', 'wb') as file: - pickle.dump(allPictures, file) - print('done') -else: - with open('allPictures.pkl', 'rb') as file: - allPictures = pickle.load(file) + 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 -print('Segmenting target') -target = get_color_space_array(target_path) -print('Segmenting done') + 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() + print('Getting all pictures') + for category in source_categories: + imgs_path = './101_ObjectCategories/'+category + category_images = get_picture_names(imgs_path) + for img in category_images: + img_path = imgs_path +'/'+img + allPictures.append(PictureInfos(img_path, get_average_color(img_path), False)) + print(category,'done') + + with open('allPictures.pkl', 'wb') as file: + pickle.dump(allPictures, file) + print('done') + else: + with open('allPictures.pkl', 'rb') as file: + allPictures = pickle.load(file) + + print('Segmenting target') + target = get_color_space_array(target_path) + 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))) + + working_length = np.floor(len(target)/working_size) -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))) + 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') \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..f421944 --- /dev/null +++ b/test.py @@ -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)])) + + + +