Пример #1
0
def image2pnm(infile, outfile, force_ppm=False, extension=None, mydir=None):
    """
    infile: input filename.
    outfile: output filename.
    force_ppm: boolean, convert PGM to PPM so that the output is always PPM.

    Returns: (type, error)

    - type: (string): image type: 'jpg', 'png', 'gif', etc., or None if
       image type isn't recognized.

    - error: (string): error string, or None
    """
    (ext, cmd, err) = get_image_type(infile)
    if ext is None:
        return (None, 'Image type not recognized: ' + err)

    tempfiles = []
    (outfile_dir, outfile_file) = os.path.split(outfile)

    if force_ppm:
        original_outfile = outfile
        outfile_dir = os.path.dirname(outfile)
        (f, outfile) = tempfile.mkstemp(suffix='.pnm',
                    dir=outfile_dir)
        # we might rename this file later, so don't add it to the list of
        # tempfiles to delete until later...
        os.close(f)
        logging.debug('temporary output file: %s' % outfile)

    if ext == fitsext and extension:
        cmd = an_fitstopnm_ext_cmd % extension

    if ext == fitsext and mydir:
        # an-fitstopnm: add explicit path...
        cmd = find_program(mydir, cmd)
        if cmd is None:
            return (None, 'Couldn\'t find the program "an-fitstopnm".')

    # Do the actual conversion
    do_command(cmd % (shell_escape(infile), shell_escape(outfile)))

    if force_ppm:
        if ext == pgmext:
            # Convert to PPM.
            do_command(pgmcmd % (shell_escape(outfile), shell_escape(original_outfile)))
            tempfiles.append(outfile)
        else:
            # print 'file type extension:', ext, '; renaming', outfile, 'to', original_outfile
            os.rename(outfile, original_outfile)

    for fn in tempfiles:
        os.unlink(fn)
    # Success
    return (ext, None)
Пример #2
0
def image2pnm(infile, outfile, force_ppm=False, extension=None, mydir=None):
    """
    infile: input filename.
    outfile: output filename.
    force_ppm: boolean, convert PGM to PPM so that the output is always PPM.

    Returns: (type, error)

    - type: (string): image type: 'jpg', 'png', 'gif', etc., or None if
       image type isn't recognized.

    - error: (string): error string, or None
    """
    (ext, cmd, err) = get_image_type(infile)
    if ext is None:
        return (None, 'Image type not recognized: ' + err)

    tempfiles = []
    (outfile_dir, outfile_file) = os.path.split(outfile)

    if force_ppm:
        original_outfile = outfile
        outfile_dir = os.path.dirname(outfile)
        (f, outfile) = tempfile.mkstemp(suffix='.pnm', dir=outfile_dir)
        # we might rename this file later, so don't add it to the list of
        # tempfiles to delete until later...
        os.close(f)
        logging.debug('temporary output file: %s' % outfile)

    if ext == fitsext and extension:
        cmd = an_fitstopnm_ext_cmd % extension

    if ext == fitsext and mydir:
        # an-fitstopnm: add explicit path...
        cmd = find_program(mydir, cmd)
        if cmd is None:
            return (None, 'Couldn\'t find the program "an-fitstopnm".')

    # Do the actual conversion
    do_command(cmd % (shell_escape(infile), shell_escape(outfile)))

    if force_ppm:
        if ext == pgmext:
            # Convert to PPM.
            do_command(pgmcmd %
                       (shell_escape(outfile), shell_escape(original_outfile)))
            tempfiles.append(outfile)
        else:
            # print 'file type extension:', ext, '; renaming', outfile, 'to', original_outfile
            os.rename(outfile, original_outfile)

    for fn in tempfiles:
        os.unlink(fn)
    # Success
    return (ext, None)
Пример #3
0
def filetype(fn):
    filecmd = 'file -b -N -L -k -r %s'

    cmd = filecmd % shell_escape(fn)
    (rtn,out,err) = run_command(cmd)
    if rtn:
        logverb('"file" command failed.  Command: "%s"' % cmd)
        logverb('  ', out)
        logverb('  ', err)
        return None

    out = out.strip()
    logverb('File: "%s"' % out)
    lst = []
    for line in out.split('\n- '):
        if line.endswith('\n-'):
            line = line[:-2]
        if len(line) == 0:
            continue
        p = line.split(', ', 1)
        if len(p) == 2:
            lst.append(tuple(p))
        else:
            lst.append((p[0], ''))
    return lst
Пример #4
0
def filetype(fn):
    filecmd = 'file -b -N -L -k -r %s'

    cmd = filecmd % shell_escape(fn)
    (rtn, out, err) = run_command(cmd)
    if rtn:
        logverb('"file" command failed.  Command: "%s"' % cmd)
        logverb('  ', out)
        logverb('  ', err)
        return None

    out = out.strip()
    logverb('File: "%s"' % out)
    lst = []
    for line in out.split('\n- '):
        if line.endswith('\n-'):
            line = line[:-2]
        if len(line) == 0:
            continue
        p = line.split(', ', 1)
        if len(p) == 2:
            lst.append(tuple(p))
        else:
            lst.append((p[0], ''))
    return lst
Пример #5
0
def filetype(fn):
    filecmd = 'file -b -N -L -k %s'

    cmd = filecmd % shell_escape(fn)
    (rtn,out,err) = run_command(cmd)
    if rtn:
        logverb('"file" command failed.  Command: "%s"' % cmd)
        logverb('  ', out)
        logverb('  ', err)
        return None

    out = out.strip()
    logverb('File: "%s"' % out)
    lst = []

    # The "file -r" flag, removed in some Ubuntu versions, used to
    # tell it not to convert non-printable characters to octal.  Without -r,
    # some versions print the string r'\012- ' instead of "\n- ".  Do that
    # manually here.
    out = out.replace(r'\012- ', '\n- ')

    for line in out.split('\n- '):
        if line.endswith('\n-'):
            line = line[:-2]
        if len(line) == 0:
            continue
        p = line.split(', ', 1)
        if len(p) == 2:
            lst.append(tuple(p))
        else:
            lst.append((p[0], ''))
    return lst
Пример #6
0
def filetype(fn):
    filecmd = 'file -b -N -L -k %s'

    cmd = filecmd % shell_escape(fn)
    (rtn, out, err) = run_command(cmd)
    if rtn:
        logverb('"file" command failed.  Command: "%s"' % cmd)
        logverb('  ', out)
        logverb('  ', err)
        return None

    out = out.strip()
    logverb('File: "%s"' % out)
    lst = []

    # The "file -r" flag, removed in some Ubuntu versions, used to
    # tell it not to convert non-printable characters to octal.  Without -r,
    # some versions print the string r'\012- ' instead of "\n- ".  Do that
    # manually here.
    out = out.replace(r'\012- ', '\n- ')

    for line in out.split('\n- '):
        if line.endswith('\n-'):
            line = line[:-2]
        if len(line) == 0:
            continue
        p = line.split(', ', 1)
        if len(p) == 2:
            lst.append(tuple(p))
        else:
            lst.append((p[0], ''))
    return lst
Пример #7
0
def uncompress_file(infile, uncompressed, typeinfo=None, extension=None):
    """
    infile: input filename.
    uncompressed: output filename.
    typeinfo: output from the 'file' command; if None we'll run 'file'.

    Returns: comptype
    comptype: None if the file wasn't compressed, or 'gz' or 'bz2'.
    """
    if typeinfo is None:
        typeinfo = filetype_short(infile)
        if typeinfo is None:
            logging.debug('Could not determine file type of "%s"' % infile)
            return None
    # print('uncompress_file: type is', typeinfo)
    (ext, cmd) = get_cmd(typeinfo, compcmds)
    # print('ext:', ext)
    if ext is None:
        # Check for fpack compressed FITS file.
        if fitstype in typeinfo:
            # FITS file.  Check header for ZIMAGE=T
            try:
                import fitsio
                logging.debug('Checking FITS header of', infile, 'ext',
                              extension, 'for ZIMAGE card (fpack compression)')
                hdr = fitsio.read_header(infile, ext=extension)
                if hdr.get('ZIMAGE', False):
                    # Compressed
                    cmd = (funpack_cmd % (extension or 0, shell_escape(infile),
                                          shell_escape(uncompressed)))
                    logging.debug('Fpack compressed; uncompressing with', cmd)
                    if os.system(cmd) == 0:
                        return 'fz'
            except:
                pass
        logging.debug('File is not compressed: "%s"' % '/'.join(typeinfo))
        return None
    assert uncompressed != infile
    logging.debug('Compressed file (type %s), dumping to: "%s"' %
                  (ext, uncompressed))
    do_command(cmd % (shell_escape(infile), shell_escape(uncompressed)))
    return ext
Пример #8
0
def uncompress_file(infile, uncompressed, typeinfo=None, extension=None):
    """
    infile: input filename.
    uncompressed: output filename.
    typeinfo: output from the 'file' command; if None we'll run 'file'.

    Returns: comptype
    comptype: None if the file wasn't compressed, or 'gz' or 'bz2'.
    """
    if typeinfo is None:
        typeinfo = filetype_short(infile)
        if typeinfo is None:
            logging.debug('Could not determine file type of "%s"' % infile)
            return None
    # print('uncompress_file: type is', typeinfo)
    (ext,cmd) = get_cmd(typeinfo, compcmds)
    # print('ext:', ext)
    if ext is None:
        # Check for fpack compressed FITS file.
        if fitstype in typeinfo:
            # FITS file.  Check header for ZIMAGE=T
            try:
                import fitsio
                logging.debug('Checking FITS header of', infile, 'ext',
                              extension, 'for ZIMAGE card (fpack compression)')
                hdr = fitsio.read_header(infile, ext=extension)
                if hdr.get('ZIMAGE', False):
                    # Compressed
                    cmd = (funpack_cmd % (
                        extension or 0,
                        shell_escape(infile), shell_escape(uncompressed)))
                    logging.debug('Fpack compressed; uncompressing with', cmd)
                    if os.system(cmd) == 0:
                        return 'fz'
            except:
                pass
        logging.debug('File is not compressed: "%s"' % '/'.join(typeinfo))
        return None
    assert uncompressed != infile
    logging.debug('Compressed file (type %s), dumping to: "%s"' % (ext, uncompressed))
    do_command(cmd % (shell_escape(infile), shell_escape(uncompressed)))
    return ext
Пример #9
0
def uncompress_file(infile, uncompressed, typeinfo=None):
	"""
	infile: input filename.
	uncompressed: output filename.
	typeinfo: output from the 'file' command; if None we'll run 'file'.

	Returns: comptype
	comptype: None if the file wasn't compressed, or 'gz' or 'bz2'.
	"""
	if typeinfo is None:
		typeinfo = filetype_short(infile)
		if typeinfo is None:
			logging.debug('Could not determine file type of "%s"' % infile)
			return None
	(ext,cmd) = get_cmd(typeinfo, compcmds)
	if ext is None:
		logging.debug('File is not compressed: "%s"' % '/'.join(typeinfo))
		return None
	assert uncompressed != infile
	logging.debug('Compressed file (type %s), dumping to: "%s"' % (ext, uncompressed))
	do_command(cmd % (shell_escape(infile), shell_escape(uncompressed)))
	return ext
Пример #10
0
def uncompress_file(infile, uncompressed, typeinfo=None):
	"""
	infile: input filename.
	uncompressed: output filename.
	typeinfo: output from the 'file' command; if None we'll run 'file'.

	Returns: comptype
	comptype: None if the file wasn't compressed, or 'gz' or 'bz2'.
	"""
	if typeinfo is None:
		typeinfo = filetype_short(infile)
		if typeinfo is None:
			logging.debug('Could not determine file type of "%s"' % infile)
			return None
	(ext,cmd) = get_cmd(typeinfo, compcmds)
	if ext is None:
		logging.debug('File is not compressed: "%s"' % '/'.join(typeinfo))
		return None
	assert uncompressed != infile
	logging.debug('Compressed file (type %s), dumping to: "%s"' % (ext, uncompressed))
	do_command(cmd % (shell_escape(infile), shell_escape(uncompressed)))
	return ext
Пример #11
0
def filetype(fn):
    filecmd = 'file -b -N -L -k -r %s'

    cmd = filecmd % shell_escape(fn)
    (rtn, out, err) = run_command(cmd)
    if rtn:
        logverb('"file" command failed.  Command: "%s"' % cmd)
        logverb('  ', out)
        logverb('  ', err)
        return None

    out = out.strip()
    logverb('File: "%s"' % out)
    parts = [line.split(', ', 1) for line in out.split('\n- ')]
    lst = []
    for p in parts:
        if len(p) == 2:
            lst.append(tuple(p))
        else:
            lst.append((p[0], ''))
    return lst
Пример #12
0
def image2pnm(infile, outfile, sanitized=None, force_ppm=False,
			  no_fits2fits=False, extension=None, mydir=None,
			  fix_sdss=False):
	"""
	infile: input filename.
	outfile: output filename.
	sanitized: for FITS images, output filename of sanitized (fits2fits'd) image.
	force_ppm: boolean, convert PGM to PPM so that the output is always PPM.

	Returns: (type, error)

	- type: (string): image type: 'jpg', 'png', 'gif', etc., or None if
	   image type isn't recognized.

	- error: (string): error string, or None
	"""
	(ext, cmd, err) = get_image_type(infile)
	if ext is None:
		return (None, 'Image type not recognized: ' + err)

	tempfiles = []

	(outfile_dir, outfile_file) = os.path.split(outfile)

	if (ext == fitsext) and fix_sdss and no_fits2fits:
		# We want to run fix_sdss_idr even if no_fits2fits is set.
		from fix_sdss_idr import is_sdss_idr_file, fix_sdss_idr_file

		if is_sdss_idr_file(infile):
			(f, fixidr) = tempfile.mkstemp('fix_sdss_idr', outfile_file, outfile_dir)
			os.close(f)
			tempfiles.append(fixidr)
			fix_sdss_idr_file(infile, fixidr)
			infile = fixidr

	# If it's a FITS file we want to filter it first because of the many
	# misbehaved FITS files. fits2fits is a sanitizer.
	if (ext == fitsext) and (not no_fits2fits):

		from fits2fits import fits2fits as fits2fits

		if not sanitized:
			(f, sanitized) = tempfile.mkstemp('sanitized', outfile_file, outfile_dir)
			os.close(f)
			tempfiles.append(sanitized)
		else:
			assert sanitized != infile
		errstr = fits2fits(infile, sanitized, fix_idr=fix_sdss)
		if errstr:
			return (None, errstr)
		infile = sanitized

	if force_ppm:
		original_outfile = outfile
		(outfile_dir, outfile_file) = os.path.split(outfile)
		(f, outfile) = tempfile.mkstemp('pnm', outfile_file, outfile_dir)
		# we might rename this file later, so don't add it to the list of
		# tempfiles to delete until later...
		os.close(f)
		logging.debug('temporary output file: ', outfile)

	if ext == fitsext and extension:
		cmd = an_fitstopnm_ext_cmd % extension

	if ext == fitsext and mydir:
		# an-fitstopnm: add explicit path...
		cmd = find_program(mydir, cmd)
		if cmd is None:
			return (None, 'Couldn\'t find the program "an-fitstopnm".')

	# Do the actual conversion
	do_command(cmd % (shell_escape(infile), shell_escape(outfile)))

	if force_ppm:
		if ext == pgmext:
			# Convert to PPM.
			do_command(pgmcmd % (shell_escape(outfile), shell_escape(original_outfile)))
			tempfiles.append(outfile)
		else:
			os.rename(outfile, original_outfile)

	for fn in tempfiles:
		os.unlink(fn)

	# Success
	return (ext, None)
Пример #13
0
def is_raw(fn):
	rtn = os.system(raw_id_cmd % shell_escape(fn))
	logging.debug('ran dcraw: return value %i' % rtn)
	return os.WIFEXITED(rtn) and (os.WEXITSTATUS(rtn) == 0)
Пример #14
0
def is_raw(fn):
	rtn = os.system(raw_id_cmd % shell_escape(fn))
	logging.debug('ran dcraw: return value %i' % rtn)
	return os.WIFEXITED(rtn) and (os.WEXITSTATUS(rtn) == 0)
Пример #15
0
def image2pnm(infile, outfile, sanitized=None, force_ppm=False,
			  no_fits2fits=False, extension=None, mydir=None,
			  fix_sdss=False):
	"""
	infile: input filename.
	outfile: output filename.
	sanitized: for FITS images, output filename of sanitized (fits2fits'd) image.
	force_ppm: boolean, convert PGM to PPM so that the output is always PPM.

	Returns: (type, error)

	- type: (string): image type: 'jpg', 'png', 'gif', etc., or None if
	   image type isn't recognized.

	- error: (string): error string, or None
	"""
	(ext, cmd, err) = get_image_type(infile)
	if ext is None:
		return (None, 'Image type not recognized: ' + err)

	tempfiles = []

	(outfile_dir, outfile_file) = os.path.split(outfile)

	if (ext == fitsext) and fix_sdss and no_fits2fits:
		# We want to run fix_sdss_idr even if no_fits2fits is set.
		from fix_sdss_idr import is_sdss_idr_file, fix_sdss_idr_file

		if is_sdss_idr_file(infile):
			(f, fixidr) = tempfile.mkstemp('fix_sdss_idr', outfile_file, outfile_dir)
			os.close(f)
			tempfiles.append(fixidr)
			logging.debug('fix_sdss_idr(in="%s", out="%s")' % (infile, fixidr))
			fix_sdss_idr_file(infile, fixidr)
			infile = fixidr

	# If it's a FITS file we want to filter it first because of the many
	# misbehaved FITS files. fits2fits is a sanitizer.
	if (ext == fitsext) and (not no_fits2fits):

		from fits2fits import fits2fits as fits2fits

		if not sanitized:
			(f, sanitized) = tempfile.mkstemp('sanitized', outfile_file, outfile_dir)
			os.close(f)
			tempfiles.append(sanitized)
		else:
			assert sanitized != infile
		logging.debug('fits2fits(in="%s", out="%s", fix_idr=%s)' %
					  (infile, sanitized, str(fix_sdss)))
		errstr = fits2fits(infile, sanitized, fix_idr=fix_sdss)
		if errstr:
			return (None, errstr)
		infile = sanitized

	if force_ppm:
		original_outfile = outfile
		(outfile_dir, outfile_file) = os.path.split(outfile)
		(f, outfile) = tempfile.mkstemp('pnm', outfile_file, outfile_dir)
		# we might rename this file later, so don't add it to the list of
		# tempfiles to delete until later...
		os.close(f)
		logging.debug('temporary output file: %s' % outfile)

	if ext == fitsext and extension:
		cmd = an_fitstopnm_ext_cmd % extension

	if ext == fitsext and mydir:
		# an-fitstopnm: add explicit path...
		cmd = find_program(mydir, cmd)
		if cmd is None:
			return (None, 'Couldn\'t find the program "an-fitstopnm".')

	# Do the actual conversion
	do_command(cmd % (shell_escape(infile), shell_escape(outfile)))

	if force_ppm:
		if ext == pgmext:
			# Convert to PPM.
			do_command(pgmcmd % (shell_escape(outfile), shell_escape(original_outfile)))
			tempfiles.append(outfile)
		else:
			os.rename(outfile, original_outfile)

	for fn in tempfiles:
		os.unlink(fn)

	# Success
	return (ext, None)