From 557648d2a2788b819b57c86ca1f286fdd22490b1 Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Wed, 22 May 2019 18:22:33 +0300 Subject: [PATCH] Update cr2fits --- cr2fits/PKGBUILD | 18 +-- cr2fits/cr2fits.py | 369 --------------------------------------------- 2 files changed, 6 insertions(+), 381 deletions(-) delete mode 100644 cr2fits/cr2fits.py diff --git a/cr2fits/PKGBUILD b/cr2fits/PKGBUILD index 7dbd850..093d00a 100644 --- a/cr2fits/PKGBUILD +++ b/cr2fits/PKGBUILD @@ -1,23 +1,17 @@ -# Maintainer: Sabart Otto - Seberm 65535: - raise ValueError("data out of range: %i" % maxval) - data = data.astype('u1' if maxval < 256 else '>u2') - self._data = data - if data.ndim > 2 and data.shape[-1] in (3, 4): - self.depth = data.shape[-1] - self.width = data.shape[-2] - self.height = data.shape[-3] - self.magicnum = b'P7' if self.depth == 4 else b'P6' - else: - self.depth = 1 - self.width = data.shape[-1] - self.height = data.shape[-2] - self.magicnum = b'P5' if maxval > 1 else b'P4' - self.maxval = maxval - self.tupltypes = [self._types[self.magicnum]] - self.header = self._header() - - def _tofile(self, fileid, pam=False): - """Write Netbm file.""" - fileid.seek(0) - fileid.write(self._header(pam)) - data = self.asarray(copy=False) - if self.maxval == 1: - data = numpy.packbits(data, axis=-1) - data.tofile(fileid) - - def _header(self, pam=False): - """Return file header as byte string.""" - if pam or self.magicnum == b'P7': - header = "\n".join(("P7", - "HEIGHT %i" % self.height, - "WIDTH %i" % self.width, - "DEPTH %i" % self.depth, - "MAXVAL %i" % self.maxval, - "\n".join("TUPLTYPE %s" % unicode(i) for i in self.tupltypes), - "ENDHDR\n")) - elif self.maxval == 1: - header = "P4 %i %i\n" % (self.width, self.height) - elif self.depth == 1: - header = "P5 %i %i %i\n" % (self.width, self.height, self.maxval) - else: - header = "P6 %i %i %i\n" % (self.width, self.height, self.maxval) - if sys.version_info[0] > 2: - header = bytes(header, 'ascii') - return header - - def __str__(self): - """Return information about instance.""" - return unicode(self.header) - - -if sys.version_info[0] > 2: - basestring = str - unicode = lambda x: str(x, 'ascii') - -### --- END OF NETPBMFILE SOURCE CODE --- ### - -### CR2FITS SOURCE CODE ### - - -try : - cr2FileName = sys.argv[1] - colorInput = int(sys.argv[2]) # 0=R 1=G 2=B -except : - print("ERROR : You probably don't know how to use it?") - print("./cr2fits.py ") - print("The can take 3 values:0,1,2 for R,G,B respectively.") - print("Example :\n\t$ ./cr2fits.py myimage.cr2 1") - print("The above example will create 2 outputs.") - print("\tmyimage.ppm : The PPM, which you can delete.") - print("\tmyimage-G.fits : The FITS image in the Green channel, which is the purpose!") - print("For details : http://github.com/eaydin/cr2fits") - print("Version : %s" % version) - raise SystemExit - -colors = {0:"Red",1:"Green",2:"Blue"} -colorState = any([True for i in colors.keys() if i == colorInput]) - -if colorState == False : - print("ERROR : Color value can be set as 0:Red, 1:Green, 2:Blue.") - raise SystemExit - -print("Reading file %s...") % cr2FileName -try : - #Converting the CR2 to PPM - p = subprocess.Popen(["dcraw","-6","-j","-W",cr2FileName]).communicate()[0] - - #Getting the EXIF of CR2 with dcraw - p = subprocess.Popen(["dcraw","-i","-v",cr2FileName],stdout=subprocess.PIPE) - cr2header = p.communicate()[0] - - #Catching the Timestamp - m = re.search('(?<=Timestamp:).*',cr2header) - date1=m.group(0).split() - months = { 'Jan' : 1, 'Feb' : 2, 'Mar' : 3, 'Apr' : 4, 'May' : 5, 'Jun' : 6, 'Jul' : 7, 'Aug' : 8, 'Sep' : 9, 'Oct' : 10, 'Nov' : 11, 'Dec' : 12 } - date = datetime.datetime(int(date1[4]),months[date1[1]],int(date1[2]),int(date1[3].split(':')[0]),int(date1[3].split(':')[1]),int(date1[3].split(':')[2])) - date ='{0:%Y-%m-%d %H:%M:%S}'.format(date) - - #Catching the Shutter Speed - m = re.search('(?<=Shutter:).*(?=sec)',cr2header) - shutter = m.group(0).strip() - - #Catching the Aperture - m = re.search('(?<=Aperture: f/).*',cr2header) - aperture = m.group(0).strip() - - #Catching the ISO Speed - m = re.search('(?<=ISO speed:).*',cr2header) - iso = m.group(0).strip() - - #Catching the Focal length - m = re.search('(?<=Focal length: ).*(?=mm)',cr2header) - focal = m.group(0).strip() - - #Catching the Original Filename of the cr2 - m = re.search('(?<=Filename:).*',cr2header) - original_file = m.group(0).strip() - - #Catching the Camera Type - m = re.search('(?<=Camera:).*',cr2header) - camera = m.group(0).strip() - -except : - print("ERROR : Something went wrong with dcraw. Do you even have dcraw?") - raise SystemExit - -print("Reading the PPM output...") -try : - #Reading the PPM - ppm_name = cr2FileName.split('.')[0] + '.ppm' - im_ppm = NetpbmFile(ppm_name).asarray() -except : - print("ERROR : Something went wrong while reading the PPM file.") - raise SystemExit - -print("Extracting %s color channels... (may take a while)" % colors[colorInput]) -try : - #Extracting the Green Channel Only - im_green = numpy.zeros((im_ppm.shape[0],im_ppm.shape[1]),dtype=numpy.uint16) - for row in xrange(0,im_ppm.shape[0]) : - for col in xrange(0,im_ppm.shape[1]) : - im_green[row,col] = im_ppm[row,col][colorInput] -except : - print("ERROR : Something went wrong while extracting color channels.") - raise SystemExit - -print("Creating the FITS file...") -try : -#Creating the FITS File - hdu = pyfits.PrimaryHDU(im_green) - hdu.header.set('OBSTIME',date) - hdu.header.set('EXPTIME',shutter) - hdu.header.set('APERTUR',aperture) - hdu.header.set('ISO',iso) - hdu.header.set('FOCAL',focal) - hdu.header.set('ORIGIN',original_file) - hdu.header.set('FILTER',colors[colorInput]) - hdu.header.set('CAMERA',camera) - hdu.header.add_comment('FITS File Created with cr2fits.py available at %s'%(sourceweb)) - hdu.header.add_comment('cr2fits.py version %s'%(version)) - hdu.header.add_comment('EXPTIME is in seconds.') - hdu.header.add_comment('APERTUR is the ratio as in f/APERTUR') - hdu.header.add_comment('FOCAL is in mm') -except : - print("ERROR : Something went wrong while creating the FITS file.") - raise SystemExit - -print("Writing the FITS file...") -try : - hdu.writeto(cr2FileName.split('.')[0]+"-"+colors[colorInput][0]+'.fits') -except : - print("ERROR : Something went wrong while writing the FITS file. Maybe it already exists?") - raise SystemExit - -print("Conversion successful!")