Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ipanagiotidou/43e44d2eb5c1f9f4bf0d5da97330af38 to your computer and use it in GitHub Desktop.

Select an option

Save ipanagiotidou/43e44d2eb5c1f9f4bf0d5da97330af38 to your computer and use it in GitHub Desktop.

Revisions

  1. @aminzabardast aminzabardast revised this gist Jun 15, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Python 3 PFM Reader-Writer
    Original file line number Diff line number Diff line change
    @@ -45,6 +45,8 @@ def read_pfm(file):
    Write a Numpy array to a PFM file.
    '''
    def write_pfm(file, image, scale = 1):
    file = open(file, 'wb')

    color = None

    if image.dtype.name != 'float32':
  2. @aminzabardast aminzabardast revised this gist Jun 15, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Python 3 PFM Reader-Writer
    Original file line number Diff line number Diff line change
    @@ -57,14 +57,14 @@ def write_pfm(file, image, scale = 1):
    else:
    raise Exception('Image must have H x W x 3, H x W x 1 or H x W dimensions.')

    file.write('PF\n' if color else 'Pf\n')
    file.write('%d %d\n' % (image.shape[1], image.shape[0]))
    file.write(b'PF\n' if color else b'Pf\n')
    file.write(b'%d %d\n' % (image.shape[1], image.shape[0]))

    endian = image.dtype.byteorder

    if endian == '<' or endian == '=' and sys.byteorder == 'little':
    scale = -scale

    file.write('%f\n' % scale)
    file.write(b'%f\n' % scale)

    image.tofile(file)
  3. @aminzabardast aminzabardast revised this gist Jun 15, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Python 3 PFM Reader-Writer
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ import re
    import sys

    '''
    read a PFM file into a Numpy array. Note that it will have
    Read a PFM file into a Numpy array. Note that it will have
    a shape of H x W, not W x H. Returns a tuple containing the
    loaded image and the scale factor from the file.
    '''
    @@ -42,9 +42,9 @@ def read_pfm(file):
    return np.reshape(data, shape), scale

    '''
    Save a Numpy array to a PFM file.
    Write a Numpy array to a PFM file.
    '''
    def save_pfm(file, image, scale = 1):
    def write_pfm(file, image, scale = 1):
    color = None

    if image.dtype.name != 'float32':
  4. @aminzabardast aminzabardast renamed this gist Jun 15, 2018. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions Python PFM <-> Numpy library → Python 3 PFM Reader-Writer
    Original file line number Diff line number Diff line change
    @@ -3,26 +3,28 @@ import re
    import sys

    '''
    Load a PFM file into a Numpy array. Note that it will have
    read a PFM file into a Numpy array. Note that it will have
    a shape of H x W, not W x H. Returns a tuple containing the
    loaded image and the scale factor from the file.
    '''
    def load_pfm(file):
    def read_pfm(file):
    file = open(file, 'rb')

    color = None
    width = None
    height = None
    scale = None
    endian = None

    header = file.readline().rstrip()
    if header == 'PF':
    if header.decode('ascii') == 'PF':
    color = True
    elif header == 'Pf':
    elif header.decode('ascii') == 'Pf':
    color = False
    else:
    raise Exception('Not a PFM file.')

    dim_match = re.match(r'^(\d+)\s(\d+)\s$', file.readline())
    dim_match = re.search(r'(\d+)\s(\d+)', file.readline().decode('ascii'))
    if dim_match:
    width, height = map(int, dim_match.groups())
    else:
  5. @chpatrick chpatrick created this gist Feb 11, 2014.
    68 changes: 68 additions & 0 deletions Python PFM <-> Numpy library
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    import numpy as np
    import re
    import sys

    '''
    Load a PFM file into a Numpy array. Note that it will have
    a shape of H x W, not W x H. Returns a tuple containing the
    loaded image and the scale factor from the file.
    '''
    def load_pfm(file):
    color = None
    width = None
    height = None
    scale = None
    endian = None

    header = file.readline().rstrip()
    if header == 'PF':
    color = True
    elif header == 'Pf':
    color = False
    else:
    raise Exception('Not a PFM file.')

    dim_match = re.match(r'^(\d+)\s(\d+)\s$', file.readline())
    if dim_match:
    width, height = map(int, dim_match.groups())
    else:
    raise Exception('Malformed PFM header.')

    scale = float(file.readline().rstrip())
    if scale < 0: # little-endian
    endian = '<'
    scale = -scale
    else:
    endian = '>' # big-endian

    data = np.fromfile(file, endian + 'f')
    shape = (height, width, 3) if color else (height, width)
    return np.reshape(data, shape), scale

    '''
    Save a Numpy array to a PFM file.
    '''
    def save_pfm(file, image, scale = 1):
    color = None

    if image.dtype.name != 'float32':
    raise Exception('Image dtype must be float32.')

    if len(image.shape) == 3 and image.shape[2] == 3: # color image
    color = True
    elif len(image.shape) == 2 or len(image.shape) == 3 and image.shape[2] == 1: # greyscale
    color = False
    else:
    raise Exception('Image must have H x W x 3, H x W x 1 or H x W dimensions.')

    file.write('PF\n' if color else 'Pf\n')
    file.write('%d %d\n' % (image.shape[1], image.shape[0]))

    endian = image.dtype.byteorder

    if endian == '<' or endian == '=' and sys.byteorder == 'little':
    scale = -scale

    file.write('%f\n' % scale)

    image.tofile(file)