Cartesian bitmap?
March 3, 2009 6:40 PM   Subscribe

Is there a convenient way to convert image files into x, y, z coordinates

I'd like to be able to take a greyscale image and convert each pixel into an x, y, z coordinate. The z would be the greyscale of each pixel. Is doesn't matter if the scaling is off, relative numbers are fine and can be converted later.

Is there a way I can do this?

Original file type doesn't matter too much to me, but most convenient would be bitmaps.
posted by ian1977 to Computers & Internet (9 answers total)
 
You've just essentially described what a greyscale bitmap image file is: an [encoded] set of numeric values describing the brightness of each pixel. Can you offer a little more information about your desired output format? Are you trying to create a list? A spreadsheet?
posted by Songdog at 6:49 PM on March 3, 2009


Response by poster: My desired output would be an excel file with 3 columns of integers: x, y & z.

Barring that, a text file list would be fine too.
posted by ian1977 at 6:55 PM on March 3, 2009


Here's a Python script that does what you want.

1. Install Python and the Python Imaging Library.
2. Paste the code below into a file named "coords.py"
3. Edit source_file and output_file to have the correct filenames
4. Double-click coords.py

This will dump x,y,value coordinates into a CSV file that can be imported by Excel. The image file must be grayscale (otherwise it will dump RGB tuples into the CSV)

-------------------------------

from PIL import Image

input_file = "foo.jpg"
output_file = "foo.csv"

im = Image.open(input_file)
out = open(output_file,'w')
width, height = im.size
for x in xrange(width):
for y in xrange(height):
print >> out, "%s, %s, %s" % (x,y,im.getpixel((x,y)))
out.close()
posted by qxntpqbbbqxl at 7:19 PM on March 3, 2009


Best answer: Oops... indentation got trashed, and that's important in Python. Let's try again:

from PIL import Image

input_file = "foo.jpg"
output_file = "foo.csv"

im = Image.open(input_file)
out = open(output_file,'w')
width, height = im.size
for x in xrange(width):
  for y in xrange(height):
    print >> out, "%s, %s, %s" % (x,y,im.getpixel((x,y)))
out.close()
posted by qxntpqbbbqxl at 7:20 PM on March 3, 2009 [1 favorite]


Best answer: OK. This is definitely possible, at least with a little programming (someone may chime in and suggest an existing solution). It's been a while since I did much with image processing but there are libraries available for various programming languages for the purpose of reading and writing standard image file formats, and most image file formats are well documented if you want to get something written.

I don't know whether you're a programmer yourself or what kind of relationship you have with computers (or what platform you're working with). When a piece of software opens an image file for display or manipulation it gets loaded into memory where it is typically represented as something like a multidimensional array of pixels. The specifics of the file format are important, but let's assume that the image is in memory as grayscale. At this point you can point to the x and y coordinates of any pixel (relative to an origin in one of the corners) and read out the z value, which is its brightness. By iterating over all of the available x and y values the desired table can be created, probably as a comma- or tab-separated value text file, which Excel can happily read.

I said the file format was important, though, and here's how: the simplest file formats contain more or less straight up lists of pixel values, either rows-then-columns or the other way around, but most image files are compressed, which means they must be decompressed to access the image data itself. Some compression schemes (such as JPEG) are "lossy," which means that image data which has been compressed and decompressed is not identical to the original image data. The quality settings of the compression will affect the amount of data loss, but this sort of image format is obviously not as suitable for scientific analysis.

On preview: qxntpqbbbqxl, that's a great specific example, and of course you can combine the RGB values to get grayscale if that's an issue.
posted by Songdog at 7:26 PM on March 3, 2009


Best answer: I suspect (depending a lot on your application) that you'll want to go from your original image to netpbm to your (x,y,z) format.

In case you're not familiar, netpbm files can be pretty huge, but many (most?) general purpose image conversion programs (e.g., ImageMagick, gimp, ...) will convert from just about anything into pbm (black and white), pgm (grayscale), or ppm (color).

Here's an example pbm file (just black and white) showing the letter j from the Wikipedia article:

---
P1
# This is an example bit map file j.pbm
6 10
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
1 0 0 0 1 0
0 1 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
---

PGM (the grayscale format) is similar. You'd probably have to write a program to convert from netpbm to your (x,y,z) format though.

(Maybe there's a more direct bitmap format though, as Songdog notes, you're basically asking about how to make a bitmap of a particular format...)

(On preview: you can also use your favorite image library calls, as the example above with PIL shows.)
posted by pbh at 7:28 PM on March 3, 2009


Best answer: This is the fourth time I've recommended this program on Askme, but the freeware Imagej will do what you want.

Download, open image, covert to greyscale if need be, then go to Analyze -> Save XY coordinates.

It then gives you a text file:
(example)
0 0 146
1 0 130
2 0 112
3 0 104
4 0 109
5 0 112
6 0 111
7 0 104
8 0 100
9 0 93
10 0 87
11 0 88
12 0 91
etc.

Just copy and past into excel and you're done.
posted by kisch mokusch at 9:46 PM on March 3, 2009


Also, the java plugin that does the job is here, for anyone interested (I can't read these things, but clearly there are people here that can). This comes with the imagej download linked above, so there's no need to download it separately.
posted by kisch mokusch at 10:07 PM on March 3, 2009


Not what you're looking for, but Photoshop CS4 Extended can convert greyscale images into a 3d terrain map based on the luminosity of the pixel.
posted by parabola01 at 5:09 AM on March 4, 2009


« Older Apple Says It Should Copy ... Why Won't It?   |   Help me pick a good comic book series for a 9 year... Newer »
This thread is closed to new comments.