annotate transformations/add_background_image.py @ 8:bdaa5bd26dcf

Added : script to merge a character image with a random background image
author guitch
date Tue, 26 Jan 2010 18:58:10 -0500
parents
children 64dac4aabc04
rev   line source
8
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
1 #!/usr/bin/python
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
2 # -*- coding: iso-8859-1 -*-
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
3 # usage : add_background_image.py "image_name"
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
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
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
5
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
6 ##################
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
7 # import libraries
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
8 ##################
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
9
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
10 import sys, os, random, fnmatch
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
11 import Image
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
12
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
13 ###########
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
14 # variables
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
15 ###########
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
16 # don't forget the "/" at the end of directories
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
17
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
18 if len(sys.argv) < 2:
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
19 print "No argument, exiting"
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
20 sys.exit()
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
21
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
22 char_image = sys.argv[1]
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
23 image_dir = "./images/"
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
24 pattern = "*.jpg"
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
25 threshold = 100;
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
26
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
27 ###########
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
28 # functions
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
29 ###########
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
30
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
31 # make a random 32x32 crop of image "image" and returns the new image_dir
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
32 def rand_crop(image):
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
33 w, h = image.size
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
34 x, y = random.randint(1,w - 32), random.randint(1,h - 32)
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
35
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
36 return image.crop((x, y, x + 32, y + 32))
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
37
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
38 # select a random image from "image_dir" and crops it
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
39 def rand_image():
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
40 files = os.listdir(image_dir)
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
41 image_files = fnmatch.filter(files, pattern)
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
42 i = random.randint(0, len(image_files) - 1)
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
43
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
44 image = Image.open(image_dir + image_files[i]).convert("L")
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
45
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
46 return rand_crop(image)
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
47
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
48
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
49 # set "bg_image" as background to "image", based on a pixels threshold
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
50 def set_bg(image, bg_image, threshold):
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
51 pix = image.load()
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
52 bg_pix = bg_image.load()
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
53
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
54 for x in range(1, 32):
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
55 for y in range(1, 32):
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
56 if pix[x, y] > threshold:
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
57 pix[x, y] = bg_pix[x, y]
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
58
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
59 return image
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
60
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
61 ######
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
62 # main
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
63 ######
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
64
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
65 sys.stdout.write("Applying background to " + char_image + " with threshold " + str(threshold) + "... ")
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
66 sys.stdout.flush()
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
67
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
68 image = Image.open(char_image).convert("L")
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
69
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
70 bg_image = rand_image()
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
71
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
72 image = set_bg(image, bg_image, threshold)
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
73
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
74 image.save(char_image + "-bg.jpg")
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
75 #image.show()
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
76
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
77 sys.stdout.write(" Done.\n")
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
78
bdaa5bd26dcf Added : script to merge a character image with a random background image
guitch
parents:
diff changeset
79