-
Notifications
You must be signed in to change notification settings - Fork 15
/
fitsimage.py
419 lines (350 loc) · 15.8 KB
/
fitsimage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python
# Library for treating FITS files as Python Imaging Library objects
# Copyright (c) 2005, 2006, 2007, Jeremy Brewer
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# * The names of the contributors may not be used to endorse or
# promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Changelog:
#
# 9/25/07 Added call to fits_simple_verify() to verify input file is FITS.
# Removed kwargs from FitsImage() because pyfits doesn't use them.
#
# 9/14/07 Change array usage from Numeric to numpy. Changed underlying
# FITS I/O library from fitslib to pyfits. Modifications made
# by Christopher Hanley.
#
# 8/20/07 Write arcsinh scaling algorithm and adding scaling options.
# Updated documentation. Dropped number of channels check on
# color -- PIL should handle this instead.
#
# 8/17/07 Wrote new scaling algorithm, percentile_range(), that determines
# the range to use from a configurable percentile cut. Now
# FitsImage() takes optional named arguments to configure which
# contrast algorithm to use. In addition, keyword arguments are
# passed on to Fits() to configure how minor errors are handled.
#
# 7/4/07 Updated to use Numeric. Improved speed of zscale_range().
#
# 10/10/06 Increased accuracy of draw_circle().
#
# 2/7/06 Updated documentation.
#
# 1/4/06 Fixed bug in zscale_range() where num_points and num_pixels
# sometimes differed, resulting in the sigma iteration failing because
# the arrays would differ in length. Now the arrays are both of
# size num_pixels. Some additional checks were also added.
#
# 12/10/05 Updated documentation.
#
# 12/8/05 Now draw_circle will not draw points that lie outside of the image.
#
# 12/7/05 Wrote zscale_range() function which implements the ds9 zscale
# autocontrast algorithm for FITs images. Wrote a new version of
# asImage(), now called FitsImage(), that returns a PIL Image object
# without use of the convert commandline utility. Rewrote convert()
# and resize() methods so that they do not have to use the convert
# command externally. Removed all of the other asImage() methods
# that weren't working.
"""
Module for treating a FITS image as a Python Imaging Library (PIL) object.
This is extremely useful if you want to convert a FITS image to jpeg and/or
perform various operations on it (such as drawing).
The contrast for FITS images is determined using the zscale algorithm by
default, but this can be configured with various options. See the
documentation for zscale_range() and percentile_range() for more information.
Example Usage:
# image is a full PIL object
image = fitsimage.FitsImage("foo.fits")
image.save("foo.jpg")
"""
__author__ = "Jeremy Brewer ([email protected])"
__copyright__ = "Copyright 2005, 2006, 2007 Jeremy Brewer"
__license__ = "BSD"
__version__ = "1.1"
import os
import sys
import cmath
import fitslib
import pyfits
import pointarray
import Image
import ImageDraw
import numpy
def zscale_range(image_data, contrast=0.25, num_points=600, num_per_row=120):
"""
Computes the range of pixel values to use when adjusting the contrast
of FITs images using the zscale algorithm. The zscale algorithm
originates in Iraf. More information about it can be found in the help
section for DISPLAY in Iraf.
Briefly, the zscale algorithm uses an evenly distributed subsample of the
input image instead of a full histogram. The subsample is sorted by
intensity and then fitted with an iterative least squares fit algorithm.
The endpoints of this fit give the range of pixel values to use when
adjusting the contrast.
Input: image_data -- the array of data contained in the FITs image
(must have 2 dimensions)
contrast -- the contrast parameter for the zscale algorithm
num_points -- the number of points to use when sampling the
image data
num_per_row -- number of points per row when sampling
Return: 1.) The minimum pixel value to use when adjusting contrast
2.) The maximum pixel value to use when adjusting contrast
"""
# check input shape
if len(image_data.shape) != 2:
raise ValueError("input data is not an image")
# check contrast
if contrast <= 0.0:
contrast = 1.0
# check number of points to use is sane
if num_points > numpy.size(image_data) or num_points < 0:
num_points = 0.5 * numpy.size(image_data)
# determine the number of points in each column
num_per_col = int(float(num_points) / float(num_per_row) + 0.5)
# integers that determine how to sample the control points
xsize, ysize = image_data.shape
row_skip = float(xsize - 1) / float(num_per_row - 1)
col_skip = float(ysize - 1) / float(num_per_col - 1)
# create a regular subsampled grid which includes the corners and edges,
# indexing from 0 to xsize - 1, ysize - 1
data = []
for i in xrange(num_per_row):
x = int(i * row_skip + 0.5)
for j in xrange(num_per_col):
y = int(j * col_skip + 0.5)
data.append(image_data[x, y])
# actual number of points selected
num_pixels = len(data)
# sort the data by intensity
data.sort()
# check for a flat distribution of pixels
data_min = min(data)
data_max = max(data)
center_pixel = (num_pixels + 1) / 2
if data_min == data_max:
return data_min, data_max
# compute the median
if num_pixels % 2 == 0:
median = data[center_pixel - 1]
else:
median = 0.5 * (data[center_pixel - 1] + data[center_pixel])
# compute an iterative fit to intensity
pixel_indeces = map(float, xrange(num_pixels))
points = pointarray.PointArray(pixel_indeces, data, min_err=1.0e-4)
fit = points.sigmaIterate()
num_allowed = 0
for pt in points.allowedPoints():
num_allowed += 1
if num_allowed < int(num_pixels / 2.0):
return data_min, data_max
# compute the limits
z1 = median - (center_pixel - 1) * (fit.slope / contrast)
z2 = median + (num_pixels - center_pixel) * (fit.slope / contrast)
if z1 > data_min:
zmin = z1
else:
zmin = data_min
if z2 < data_max:
zmax = z2
else:
zmax = data_max
# last ditch sanity check
if zmin >= zmax:
zmin = data_min
zmax = data_max
return zmin, zmax
def percentile_range(image_data, min_percent=3.0, max_percent=99.0,
num_points=5000, num_per_row=250):
"""
Computes the range of pixel values to use when adjusting the contrast
of FITs images using a simple percentile cut. For efficiency reasons,
only a subsample of the input image data is used.
Input: image_data -- the array of data contained in the FITs image
(must have 2 dimensions)
min_percent -- min percent value between (0, 100)
max_percent -- max percent value between (0, 100)
num_points -- the number of points to use when sampling the
image data
num_per_row -- number of points per row when sampling
Return: 1.) The minimum pixel value to use when adjusting contrast
2.) The maximum pixel value to use when adjusting contrast
"""
if not 0 <= min_percent <= 100:
raise ValueError("invalid value for min percent '%s'" % min_percent)
elif not 0 <= max_percent <= 100:
raise ValueError("invalid value for max percent '%s'" % max_percent)
min_percent = float(min_percent) / 100.0
max_percent = float(max_percent) / 100.0
# check input shape
if len(image_data.shape) != 2:
raise ValueError("input data is not an image")
# check number of points to use is sane
if num_points > numpy.size(image_data) or num_points < 0:
num_points = 0.5 * numpy.size(image_data)
# determine the number of points in each column
num_per_col = int(float(num_points) / float(num_per_row) + 0.5)
# integers that determine how to sample the control points
xsize, ysize = image_data.shape
row_skip = float(xsize - 1) / float(num_per_row - 1)
col_skip = float(ysize - 1) / float(num_per_col - 1)
# create a regular subsampled grid which includes the corners and edges,
# indexing from 0 to xsize - 1, ysize - 1
data = []
for i in xrange(num_per_row):
x = int(i * row_skip + 0.5)
for j in xrange(num_per_col):
y = int(j * col_skip + 0.5)
data.append(image_data[x, y])
# perform a simple percentile cut
data.sort()
zmin = data[int(min_percent * len(data))]
zmax = data[int(max_percent * len(data))]
return zmin, zmax
def FitsImage(fitsfile, contrast="zscale", contrast_opts={}, scale="linear",
scale_opts={}):
"""
Constructor-like function that returns a Python Imaging Library (PIL)
Image object. This allows extremely easy and powerful manipulation of
FITs files as images. The contrast is automatically adjusted using the
zscale algorithm (see zscale_range() above).
Input: fitsfile -- a FITS image filename
contrast -- the algorithm for determining the min/max
values in the FITS pixel data to use when
compressing the dynamic range of the FITS
data to something visible by the eye, either
"zscale" or "percentile"
contrast_opts -- options for the contrast algorithm, see
the optional args of [contrast]_range()
for what to name the keys
scale -- how to scale the pixel values between the
min/max values from the contrast
algorithm when converting to a a raster
format, either "linear" or "arcsinh"
scale_opts -- options for the scaling algorithm, currently
only "nonlinearity" is supported for arcsinh,
which has a default value of 3
"""
if contrast not in ("zscale", "percentile"):
raise ValueError("invalid contrast algorithm '%s'" % contrast)
if scale not in ("linear", "arcsinh"):
raise ValueError("invalid scale value '%s'" % scale)
# open the fits file and read the image data and size
fitslib.fits_simple_verify(fitsfile)
fits = pyfits.open(fitsfile)
try:
hdr = fits[0].header
xsize = hdr["NAXIS1"]
ysize = hdr["NAXIS2"]
fits_data = fits[0].data
finally:
fits.close()
# compute the proper scaling for the image
if contrast == "zscale":
contrast_value = contrast_opts.get("contrast", 0.25)
num_points = contrast_opts.get("num_points", 600)
num_per_row = contrast_opts.get("num_per_row", 120)
zmin, zmax = zscale_range(fits_data, contrast=contrast_value,
num_points=num_points,
num_per_row=num_per_row)
elif contrast == "percentile":
min_percent = contrast_opts.get("min_percent", 3.0)
max_percent = contrast_opts.get("max_percent", 99.0)
num_points = contrast_opts.get("num_points", 5000)
num_per_row = contrast_opts.get("num_per_row", 250)
zmin, zmax = percentile_range(fits_data, min_percent=min_percent,
max_percent=max_percent,
num_points=num_points,
num_per_row=num_per_row)
# set all points less than zmin to zmin and points greater than
# zmax to zmax
fits_data = numpy.where(fits_data > zmin, fits_data, zmin)
fits_data = numpy.where(fits_data < zmax, fits_data, zmax)
if scale == "linear":
scaled_data = (fits_data - zmin) * (255.0 / (zmax - zmin)) + 0.5
elif scale == "arcsinh":
# nonlinearity sets the range over which we sample values of the
# asinh function; values near 0 are linear and values near infinity
# are logarithmic
nonlinearity = scale_opts.get("nonlinearity", 3.0)
nonlinearity = max(nonlinearity, 0.001)
max_asinh = cmath.asinh(nonlinearity).real
scaled_data = (255.0 / max_asinh) * \
(numpy.arcsinh((fits_data - zmin) * \
(nonlinearity / (zmax - zmin))))
# convert to 8 bit unsigned int ("b" in numpy)
#scaled_data = scaled_data.astype("b")
# create the image
print 'hi'
image = Image.frombuffer("L", (xsize, ysize), scaled_data, "raw", "L", 0, 0)
return image
def draw_circle(image, x, y, radius, color):
"""
Draws a circle on image at position x, y with the given radius and
color.
Input: image -- the image object to draw the circle on
x -- the x position of the center of the circle
y -- the y position of the center of the circle
radius -- the radius of the circle in pixels
color -- a tuple containing the color of the border of the
circle, ranging from 0 to 255 for each channel
"""
# arc takes the upper left and lower right corners of a box bounding the
# circle as arguments. Here (x1, y1) gives the coordinates of the upper left
# corner and (x2, y2) gives the lower right corner of the bounding box.
x1 = int(x - radius + 0.5)
y1 = int(y - radius + 0.5)
x2 = int(x + radius + 0.5)
y2 = int(y + radius + 0.5)
xsize, ysize = image.size
# draw the circle
draw = ImageDraw.Draw(image)
draw.arc((x1, y1, x2, y2), 0, 360, fill=color)
def main(argv):
import time
if len(argv) != 2:
print "Usage: %s <fits-file>" % os.path.basename(argv[0])
print "Input file will be converted to JPEG"
sys.exit(2)
# FITS image to open and JPEG counterpart
fitsfile = argv[1]
name, ext = os.path.splitext(fitsfile)
jpegfile = "%s.jpg" % name
# open as PIL object
start = time.time()
image = FitsImage(fitsfile)#.convert("RGB")
stop = time.time()
print "Converting to PIL object took %f sec" % (stop - start)
image.show()
# save as a jpeg
start = time.time()
image.save(jpegfile)
stop = time.time()
print "Saving to '%s' took %f sec" % (jpegfile, stop - start)
if __name__ == "__main__":
main(sys.argv)