View Single Post
  #28  
Old July 8th, 2008, 01:19 PM
lch's Avatar

lch lch is offline
General
 
Join Date: Feb 2007
Location: R'lyeh
Posts: 3,861
Thanks: 144
Thanked 403 Times in 176 Posts
lch is on a distinguished road
Default Getting the number of provinces in an image

Ok, apparently the solution that I provided to count the number of provinces doesn't work because ImageMagick refuses to generate a histogram for an image as big as Westeros.bmp.

What I used to count the number of pixels was some small Python program. It uses PIL, the Python Imaging Library.

Here's my code:

Code:
from PIL import Image

img = Image.open("Westeros.bmp")
(xdim, ydim) = img.size
# yeah, this is a hackish way to do things
channels = len(img.getbands())
white = tuple([255]*channels)

count = 0
if Image.VERSION >= "1.1.6":
data = img.load()
for y in range(ydim):
for x in range(xdim):
if data[x,y] == white:
count += 1

else:
data = img.getdata()
for y in range(ydim):
for x in range(xdim):
if data[x+y*xdim] == white:
count += 1

print count, "provinces"


This takes about 30 seconds to complete, because of the for loops which do the pixel checks. There are ways to speed this up. First, you could consider mapping to the luminance:
Code:
from PIL import Image
count = list(Image.open('Westeros.bmp').convert('L').getda ta()).count(255)
print count, "provinces"


This takes half as long, but the conversion from the internal PIL format to Python data containers still takes too long. If you want to make full use of the underlying C code that PIL is built on, then you can do the following:
Code:
from PIL import Image
count = Image.open('/home/lch/dominions3/maps/Westeros.bmp').convert('L').histogram().pop()
print count, "provinces"


This takes less than a second on the Westeros.bmp for me.

The way that I am getting the information that I want here, over histograms, can be done with image editing programs, too. Just look where your image editing program has the Histogram function, and set it to Luminance/Value. Then enter a range of 255-255 (or pick the pure white color) and it should tell you the number of provinces. GIMP seems to have a bug, though: Although it does work correctly with other map images that I have tried, it reports something like 12038 pure white pixels here, which clearly is wrong.

Instructions for GIMP:
- open image
- open Histogram: either through "Colors > Info > Histogram" or "Dialogs > Histogram"
- change channel to "Value"
- enter 255 as left and right limit
- look at "Count" value

Workaround for GIMP:
- open image
- choose "Select > by color" (Shift+O)
- select a pure white pixel
- choose "Edit > Copy" (Ctrl+C)
- choose "Edit > Paste as > New Image"
- do the above, but with 0 and 255 as limits for the range
__________________
Come to the Dom3 Wiki and help us to build the biggest Dominions-centered knowledge base on the net.
Visit my personal user page there, too!
Pretender file password recovery
Emergency comic relief

Last edited by llamabeast; June 7th, 2009 at 06:00 AM..
Reply With Quote