[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ccp4bb]: Diffraction images to gif/jpg



William Scott at Santa Cruz points out that ADSC detector files sometimes have
multiple header lines giving the size; in which case the second entry is
valid.  The image-producing code I sent before needs to be modified.  I also
added a variable contrast parameter:

import Image,Numeric,re,math

def ReadADSCImage(filename):
  rawdata = open(filename,"rb").read()
  headeropen = rawdata.index("{")
  headerclose= rawdata.index("}")
  header = rawdata[headeropen+1:headerclose-headeropen]

  parameters={}
  for item in ['HEADER_BYTES','SIZE1','SIZE2','CCD_IMAGE_SATURATION']:
    pattern = re.compile(item+'='+r'(.*);')
    matches = pattern.findall(header)
    parameters[item] = int(matches[-1])
  ptr = parameters['HEADER_BYTES']

  numrawdata = Numeric.array(rawdata)
  bigendchar = numrawdata[ptr:ptr+2*parameters['SIZE1']*parameters['SIZE2']:2]

  bigend = bigendchar.astype(Numeric.UnsignedInt8)
  smallendchar =
numrawdata[ptr+1:ptr+2*parameters['SIZE1']*parameters['SIZE2']+1:2]
  smallend = smallendchar.astype(Numeric.UnsignedInt8)
  linearintdata = 256*bigend+smallend
  return parameters,linearintdata

def writeJPEG(filein,fileout,contrast):
  #contrast between 0 and 1, typically 0.99
  params, linearintdata = ReadADSCImage(filein)
  outscale = 256
  inscale = params['CCD_IMAGE_SATURATION']
  cfactor = math.tan(contrast*math.pi/2.0)
  adjust = outscale*(1.0-linearintdata*(cfactor/inscale)) # to make pretty
grayscale
  imageout = Image.new("L",(params['SIZE1'],params['SIZE2']),128) # 'L' is
grayscale
  imageout.putdata(adjust)
  imageout.save(fileout,"JPEG")

writeJPEG("adsc_1_000.img","./x.jpg",0.995)

Nicholas K. Sauter, Ph.D.
Lawrence Berkeley National Lab
1 Cyclotron Road, Bldg 4R0230
Berkeley, CA 94720-8235

nksauter@lbl.gov
Voice: 510-486-5713
Fax: 510-486-5909

begin:vcard 
n:Sauter;Nicholas
tel;fax:510-486-5909
tel;home:415-564-5061
tel;work:510-486-5713
x-mozilla-html:FALSE
org:Lawrence Berkeley National Lab;Computational Crystallography Initiative
adr:;;1 Cyclotron Road Mailstop 4-230;Berkeley;CA;94720;USA
version:2.1
email;internet:nksauter@lbl.gov
title:Computer Scientist
fn:Nicholas K. Sauter
end:vcard