|
from TileImageClass import TileableImage |
|
from TiledImageClass import TiledImage |
|
import random |
|
import Image |
|
#import ImageDraw |
|
import time |
|
|
|
target_size = 256 |
|
tile_size = 64 |
|
image_source_folder = "./images/" |
|
target_image_path = "./turntable_small.jpg" |
|
out_filename = "./test.jpg" |
|
|
|
def GenerateSourceImageDB(source_folder): |
|
def GetImageFilePaths(imageListDir): |
|
import re |
|
import os |
|
imageList = os.listdir(imageListDir) |
|
imageList = imageList[:] |
|
imagefilter = re.compile("\.(jpg|png)", re.IGNORECASE) |
|
imageList = filter(imagefilter.search, imageList) |
|
for index in range(len(imageList)): |
|
imageList[index] = imageListDir + imageList[index] |
|
return imageList |
|
imageDB = [] |
|
image_path_list = GetImageFilePaths(image_source_folder) |
|
for image_path in image_path_list: |
|
a = TileableImage(image_path) |
|
imageDB.append(a) |
|
# print "Processed", len(imageDB), "images." |
|
return imageDB |
|
|
|
|
|
def ColorTupleToDict(colortuple): |
|
return {'red': colortuple[0], 'green': colortuple[1], 'blue': colortuple[2]} |
|
|
|
|
|
def FindImageMatch(tileimage_list, target_color): |
|
def ColorDistance(colordict1, colordict2): |
|
deltaRed = colordict1['red'] - colordict2['red'] |
|
deltaGreen = colordict1['green'] - colordict2['green'] |
|
deltaBlue = colordict1['blue'] - colordict2['blue'] |
|
return 3*(deltaRed**2) + 6*(deltaGreen**2) + (deltaBlue**2) |
|
min_color_dist = 10*(255**2) |
|
index_closest = 0 |
|
for index in range(len(tileimage_list)): |
|
source_color = tileimage_list[index].average_color |
|
distance = ColorDistance(source_color, target_color) |
|
if(distance < min_color_dist): |
|
min_color_dist = distance |
|
index_closest = index |
|
return tileimage_list[index_closest] |
|
|
|
# Generate the database of source images |
|
print "Generating database of source images..." |
|
start_time = time.clock() |
|
source_images = GenerateSourceImageDB(image_source_folder) |
|
total_time = time.clock() - start_time |
|
time_per_image = total_time / len(source_images) |
|
print "Operation time:", total_time, "seconds" |
|
print "Total images gathered:", len(source_images) |
|
print "Took an average of", time_per_image, "seconds per image." |
|
# Pick a target image |
|
target_image = TileableImage(target_image_path) |
|
print 'Using image:', target_image.image_filepath |
|
# For each pixel in the target image, find the source image that is closest to it |
|
target_image_file = Image.open(target_image.image_filepath) |
|
print 'Original dimensions of target image:', target_image_file.size[0], 'x', target_image_file.size[1] |
|
target_image_file.thumbnail((target_size, target_size)) |
|
print 'New dimensions of target image:', target_image_file.size[0], 'x', target_image_file.size[1] |
|
target_width = target_image_file.size[0] |
|
target_height = target_image_file.size[1] |
|
tile_table = [] |
|
for y in range(target_height): |
|
print "Processing pixel row", y |
|
for x in range(target_width): |
|
target_color = target_image_file.getpixel((x, y)) |
|
target_color_dict = ColorTupleToDict(target_color) |
|
closest_image_obj = FindImageMatch(source_images, target_color_dict) |
|
# Make a tiled object from the source image object |
|
tiled_source = TiledImage(closest_image_obj, tile_size, tile_size) |
|
# Save the tiled object along with the pixel coordinates |
|
tile_table.append([tiled_source, x, y]) |
|
|
|
print "Rendering output image..." |
|
out_image = Image.new("RGB", (tile_size * target_width, tile_size * target_height)) |
|
#out_draw = ImageDraw.Draw(out_image, "RGB") |
|
for tile in tile_table: |
|
im = tile[0].RenderedImage() |
|
offset_x = tile[1] * tile_size |
|
offset_y = tile[2] * tile_size |
|
out_image.paste(im, (int(offset_x), int(offset_y))) |
|
print "Saving to", out_filename, "..." |
|
out_image.save(out_filename, "JPEG") |
|
|