-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gen_MergeSomeImagesInDirectory.py
67 lines (58 loc) · 1.85 KB
/
Gen_MergeSomeImagesInDirectory.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# @abrightmoore
# Read every image file in a directory and merge them. Output the result blended image.
from math import sqrt, tan, sin, cos, pi, ceil, floor, acos, atan, asin, degrees, radians, log, atan2
from random import randint, random, Random
from os import listdir
from os.path import isfile, join
from copy import deepcopy
import glob
import inspect
import pygame, sys
from pygame.locals import *
from io import BytesIO
import imageio
from numpy import *
from Colours import *
from ImageTools import *
def addImage(workspace,img):
width = img.size[0]
height = img.size[1]
pix = img.load()
for x in xrange(0,width):
for y in xrange(0,height):
(r,g,b,a) = pix[x,y]
#print r,g,b
workspace[x][y][0] = workspace[x][y][0]+r
workspace[x][y][1] = workspace[x][y][1]+g
workspace[x][y][2] = workspace[x][y][2]+b
del pix
def draw(img):
dir = "images/"
width = img.size[0]
height = img.size[1]
workspace = zeros((width,height,3))
extension = dir+"image_*.png"
images = glob.glob(extension)
if len(images) > 0:
numImagesProc = 0
while random() > 0.1 or numImagesProc < 3:
image = images[randint(0,len(images)-1)]
print "Processing "+image
imgIn = Image.open(image)
sx = imgIn.size[0]
sy = imgIn.size[1]
if sx != width or sy != height:
imgIn = imgIn.resize((width,height), Image.ANTIALIAS)
addImage(workspace,imgIn)
numImagesProc = numImagesProc + 1
pix = img.load()
for x in xrange(0,width):
for y in xrange(0,height):
r = workspace[x][y][0]/numImagesProc
g = workspace[x][y][1]/numImagesProc
b = workspace[x][y][2]/numImagesProc
pix[x,y] = (int(r),int(g),int(b),255)
del pix
# filename = "image_blended_"+str(randint(10000000000,99999999999))
# imageNormalize(img)
# img.save("images/"+filename+".png") # cache it