Mercurial > ift6266
annotate transformations/add_background_image.py @ 18:827de2cc34f8
Error on the sign of the lower bound of the initialization of W2
author | Owner <salahmeister@gmail.com> |
---|---|
date | Thu, 28 Jan 2010 13:29:07 -0600 |
parents | 64dac4aabc04 |
children | 4d4248f7e2fb |
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] |
9
64dac4aabc04
Added in background script : character pixel inversion (from black to white, for dark backgrounds)
Guillaume Sicard <guitch21@gmail.com>
parents:
8
diff
changeset
|
23 image_dir = "./images/" |
64dac4aabc04
Added in background script : character pixel inversion (from black to white, for dark backgrounds)
Guillaume Sicard <guitch21@gmail.com>
parents:
8
diff
changeset
|
24 pattern = "*.jpg" |
64dac4aabc04
Added in background script : character pixel inversion (from black to white, for dark backgrounds)
Guillaume Sicard <guitch21@gmail.com>
parents:
8
diff
changeset
|
25 invert = False |
8
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
26 threshold = 100; |
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 ########### |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
29 # functions |
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 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
32 # 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
|
33 def rand_crop(image): |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
34 w, h = image.size |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
35 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
|
36 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
37 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
|
38 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
39 # 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
|
40 def rand_image(): |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
41 files = os.listdir(image_dir) |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
42 image_files = fnmatch.filter(files, pattern) |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
43 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
|
44 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
45 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
|
46 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
47 return rand_crop(image) |
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 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
50 # 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
|
51 def set_bg(image, bg_image, threshold): |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
52 pix = image.load() |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
53 bg_pix = bg_image.load() |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
54 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
55 for x in range(1, 32): |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
56 for y in range(1, 32): |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
57 if pix[x, y] > threshold: |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
58 pix[x, y] = bg_pix[x, y] |
9
64dac4aabc04
Added in background script : character pixel inversion (from black to white, for dark backgrounds)
Guillaume Sicard <guitch21@gmail.com>
parents:
8
diff
changeset
|
59 elif invert: |
64dac4aabc04
Added in background script : character pixel inversion (from black to white, for dark backgrounds)
Guillaume Sicard <guitch21@gmail.com>
parents:
8
diff
changeset
|
60 pix[x, y] = 255 - pix[x, y] |
8
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 return image |
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 # main |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
66 ###### |
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 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
|
69 sys.stdout.flush() |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
70 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
71 image = Image.open(char_image).convert("L") |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
72 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
73 bg_image = rand_image() |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
74 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
75 image = set_bg(image, bg_image, threshold) |
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 image.save(char_image + "-bg.jpg") |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
78 #image.show() |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
79 |
bdaa5bd26dcf
Added : script to merge a character image with a random background image
guitch
parents:
diff
changeset
|
80 sys.stdout.write(" Done.\n") |