comparison transformations/add_background_image.py @ 32:4d4248f7e2fb

Modified background adding class for compliance with testmod.py
author Guillaume Sicard <guitch21@gmail.com>
date Mon, 01 Feb 2010 19:08:32 -0500
parents 64dac4aabc04
children ab70fbca513c
comparison
equal deleted inserted replaced
31:7ef8aac2cdb5 32:4d4248f7e2fb
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # -*- coding: iso-8859-1 -*- 2 # -*- coding: iso-8859-1 -*-
3 # usage : add_background_image.py "image_name"
4 # Chooses a random image in "image_dir" and set a random crop of it as a background to the character image "image_name" given as argument
5 3
6 ################## 4 '''
7 # import libraries 5 Implementation of random background adding to a specific image
8 ################## 6
7 Author: Guillaume Sicard
8 '''
9 9
10 import sys, os, random, fnmatch 10 import sys, os, random, fnmatch
11 import Image 11 import Image, numpy
12 12
13 ########### 13 class AddBackground():
14 # variables 14 def __init__(self, threshold = 128):
15 ########### 15 self.h = 32
16 # don't forget the "/" at the end of directories 16 self.w = 32
17 self.threshold = threshold;
18 self.bg_image_dir = './images/'
19 self.pattern = '*.jpg'
17 20
18 if len(sys.argv) < 2: 21 # get threshold value
19 print "No argument, exiting" 22 def get_settings_names(self):
20 sys.exit() 23 return [str(self.threshold)]
21 24
22 char_image = sys.argv[1] 25 # no need, except for testmod.py
23 image_dir = "./images/" 26 def regenerate_parameters(self, complexity):
24 pattern = "*.jpg" 27 value = random.gauss(0, 0.5*complexity)
25 invert = False 28 return [value]
26 threshold = 100;
27 29
28 ########### 30 # load an image
29 # functions 31 def load_image(self,filename):
30 ########### 32 image = Image.open(filename).convert('L')
33 image = numpy.asarray(image)
34 image = (image / 255.0).astype(numpy.float32)
35 return image
31 36
32 # make a random 32x32 crop of image "image" and returns the new image_dir 37 # save an image
33 def rand_crop(image): 38 def save_image(self,array, filename):
34 w, h = image.size 39 image = (array * 255.0).astype('int')
35 x, y = random.randint(1,w - 32), random.randint(1,h - 32) 40 image = Image.fromarray(image)
41 if (filename != ''):
42 image.save(filename)
43 else:
44 image.show()
36 45
37 return image.crop((x, y, x + 32, y + 32)) 46 # make a random 32x32 crop of an image
47 def rand_crop(self,image):
48 i_w, i_h = image.shape
49 x, y = random.randint(0, i_w - self.w), random.randint(0, i_h - self.h)
50 return image[x:x + self.w, y:y + self.h]
38 51
39 # select a random image from "image_dir" and crops it 52 # select a random background image from "bg_image_dir" and crops it
40 def rand_image(): 53 def rand_bg_image(self):
41 files = os.listdir(image_dir) 54 files = os.listdir(self.bg_image_dir)
42 image_files = fnmatch.filter(files, pattern) 55 image_files = fnmatch.filter(files, self.pattern)
43 i = random.randint(0, len(image_files) - 1) 56 i = random.randint(0, len(image_files) - 1)
44 57
45 image = Image.open(image_dir + image_files[i]).convert("L") 58 image = self.load_image(self.bg_image_dir + image_files[i])
59 self.bg_image = self.rand_crop(image)
46 60
47 return rand_crop(image) 61 # set "bg_image" as background to "image", based on a pixels threshold
62 def set_bg(self,image):
63 b = (image < self.threshold / 255.0).astype(numpy.float32)
64 return b * self.bg_image + ( 1 - b) * image
48 65
66 # transform an image file and return an array
67 def transform_image_from_file(self, filename):
68 self.rand_bg_image()
69 image = self.load_image(filename)
70 image = self.set_bg(image)
71 return image
49 72
50 # set "bg_image" as background to "image", based on a pixels threshold 73 # standard array to array transform
51 def set_bg(image, bg_image, threshold): 74 def transform_image(self, image):
52 pix = image.load() 75 self.rand_bg_image()
53 bg_pix = bg_image.load() 76 image = self.set_bg(image)
77 return image
54 78
55 for x in range(1, 32): 79 # test method
56 for y in range(1, 32): 80 def test(self,filename):
57 if pix[x, y] > threshold: 81 import time
58 pix[x, y] = bg_pix[x, y]
59 elif invert:
60 pix[x, y] = 255 - pix[x, y]
61 82
62 return image 83 sys.stdout.write('Starting addBackground test : loading image')
84 sys.stdout.flush()
63 85
64 ###### 86 image = self.load_image(filename)
65 # main
66 ######
67 87
68 sys.stdout.write("Applying background to " + char_image + " with threshold " + str(threshold) + "... ") 88 t = 0
69 sys.stdout.flush() 89 n = 500
90 for i in range(n):
91 t0 = time.time()
92 image2 = self.transform_image(image)
93 t = ( i * t + (time.time() - t0) ) / (i + 1)
94 sys.stdout.write('.')
95 sys.stdout.flush()
96
97 print "Done!\nAverage time : " + str(1000 * t) + " ms"
70 98
71 image = Image.open(char_image).convert("L") 99 if __name__ == '__main__':
72 100
73 bg_image = rand_image() 101 myAddBackground = AddBackground()
74 102 myAddBackground.test('./images/0-LiberationSans-Italic.ttf.jpg')
75 image = set_bg(image, bg_image, threshold)
76
77 image.save(char_image + "-bg.jpg")
78 #image.show()
79
80 sys.stdout.write(" Done.\n")