Created
August 26, 2011 22:13
-
-
Save TheBHump/1174559 to your computer and use it in GitHub Desktop.
Transparent Image Mashup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Image | |
import sys | |
im = Image.open(open(sys.argv[1])) | |
im = im.convert("RGBA") | |
data = im.load() | |
width, height = im.size | |
for x in range(0,width): | |
for y in range(0,height): | |
if data[x,y] == (0,0,0,255): | |
data[x,y] = (0,0,0,0) | |
else: | |
data[x,y] = (255,255,255,255) | |
im.save(sys.argv[2]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Image | |
import sys | |
import os | |
import random | |
from datetime import datetime | |
import resource | |
rsrc = resource.RLIMIT_STACK | |
soft, hard = resource.getrlimit(rsrc) | |
print 'Soft limit starts as :', soft | |
resource.setrlimit(rsrc, (4000000000, hard)) | |
soft, hard = resource.getrlimit(rsrc) | |
print 'Soft limit changed to :', soft | |
def make_image_size(image, width, height, crop_center = False): | |
ratio = width/float(height) | |
resize = (0,0) | |
crop = (width, height) | |
size = image.size | |
if size[0]>ratio*size[1]: | |
resize = (int(size[0]/(size[1]/float(height))), height) | |
else: | |
resize = (width, int(size[1]/(size[0]/float(width)))) | |
to_return = create_image(image, crop[0], crop[1], resize[0], resize[1], crop_center = crop_center) | |
print width, height | |
print to_return.size | |
return to_return | |
def create_image(image, crop_width, crop_height, resize_width, resize_height, crop_x =None, crop_y =None, resize_first = True, crop_center = False): | |
if crop_x == None: | |
if crop_center: | |
crop_x = resize_width/2-crop_width/2 | |
else: | |
crop_x = 0 | |
if crop_y == None: | |
crop_y = 0 | |
if crop_x<0 or crop_y<0 or crop_height<0 or crop_width<0: | |
raise Exception("Invalid image dimensions") | |
if resize_first and (crop_x+crop_width>resize_width or crop_y+crop_height>resize_height): | |
raise Exception("Invalid image dimensions") | |
if (not resize_first) and (crop_x+crop_width>image.size[0] or crop_y+crop_height>image.size[1]): | |
raise Exception("Invalid image dimensions") | |
if resize_first: | |
crop = image.resize((resize_width, resize_height), Image.ANTIALIAS) | |
return crop.crop((crop_x, crop_y, crop_x + crop_width, crop_y + crop_height)) | |
else: | |
crop = image.crop((crop_x, crop_y, crop_x + crop_width, crop_y + crop_height)) | |
return crop.resize((resize_width, resize_height), Image.ANTIALIAS) | |
def findall(x,y, inc, size, xmin, ymin, xmax, ymax, pixelsize = 0): | |
if x<0 or x>=size[0] or y<0 or y>=size[1]: | |
return (xmin, ymin, xmax, ymax, pixelsize) | |
if image_data[x,y][3] == 255 or mirror_data[x,y] > 0: | |
return (xmin, ymin, xmax, ymax, pixelsize) | |
xmin = min(x,xmin) | |
ymin = min(y, ymin) | |
xmax = max(x, xmax) | |
ymax = max(y, ymax) | |
pixelsize = pixelsize +1 | |
mirror_data[x,y] = inc | |
xmin, ymin, xmax, ymax, pixelsize = findall( x+1, y,inc, size, xmin, ymin, xmax, ymax, pixelsize) | |
xmin, ymin, xmax, ymax, pixelsize = findall( x-1, y,inc, size, xmin, ymin, xmax, ymax, pixelsize) | |
xmin, ymin, xmax, ymax, pixelsize = findall( x, y+1,inc, size, xmin, ymin, xmax, ymax, pixelsize) | |
xmin, ymin, xmax, ymax, pixelsize = findall( x, y-1,inc, size, xmin, ymin, xmax, ymax, pixelsize) | |
return (xmin, ymin, xmax, ymax, pixelsize) | |
def test(x,y): | |
return cmp(x[0], y[0]) | |
im = Image.open(open(sys.argv[1])) | |
img_list = os.popen("ls %s" % sys.argv[2]).read().split() | |
data = im.load() | |
image_data=data | |
imgs = [data] | |
width, height = im.size | |
sys.setrecursionlimit(width*height*10) | |
mirror = Image.new( "I", (width,height)) | |
mirror_data = mirror.load() | |
inc = 1 | |
extrema = {0:(0,0, width, height, width*height)} | |
bigs = [(width*height, 0)] | |
for x in range(0,width): | |
for y in range(0,height): | |
if data[x,y][3] < 255 and mirror_data[x,y] == 0: | |
extrema[inc] = findall(x,y, inc, im.size, im.size[0], im.size[1], 0, 0) | |
bigs.append((extrema[inc][4], inc)) | |
inc = inc + 1 | |
bigs = sorted(bigs, cmp = test, reverse = True) | |
if len(bigs) > len(img_list): | |
bigs = bigs[:len(img_list)+1] | |
new_extrema = {} | |
i = 0 | |
for big in bigs: | |
new_extrema[big[1]] = extrema[big[1]] | |
i = i+1 | |
extrema = new_extrema | |
random.shuffle(img_list) | |
else: | |
img_list = random.sample(img_list,len(extrema)-1) | |
mirror.save("mirror.png") | |
print bigs | |
print extrema | |
i = 0 | |
raw_imgs = [] | |
for index, ext in extrema.items(): | |
if index == 0: | |
continue | |
new_image = make_image_size(Image.open(open("%s/%s" % (sys.argv[2],img_list[i]))).convert("RGB"), ext[2]-ext[0]+1,ext[3]-ext[1]+1) | |
raw_imgs.append(new_image) | |
imgs.append(new_image.load()) | |
i+=1 | |
out_img = Image.new("RGB", im.size) | |
out_img_data = out_img.load() | |
indexes = [] | |
for x in range(0,width): | |
for y in range(0,height): | |
index = mirror_data[x,y] | |
if not index in extrema: | |
out_img_data[x,y] = (0,0,0) | |
continue | |
image_index = extrema.keys().index(index) | |
if not image_index in indexes: | |
indexes.append(image_index) | |
image = imgs[image_index] | |
extrema_offset = extrema[index] | |
out_img_data[x,y] = image[x-extrema_offset[0],y-extrema_offset[1]] | |
out_img.save(sys.argv[3]) | |
print indexes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment