Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
values = list(line.split())
while len(values) < len(HEADERITEMS):
line = f.readline()
while line[0] == '#':
line = f.readline()
values += line.split()
for k, v in zip(HEADERITEMS, values):
self.header[k] = v.strip()
# set the dimensions
dim1 = int(self.header[six.b("WIDTH")])
dim2 = int(self.header[six.b("HEIGHT")])
self._shape = dim2, dim1
# figure out how many bytes are used to store the data
# case construct here!
m = int(self.header[six.b('MAXVAL')])
if m < 256:
self._dtype = numpy.dtype(numpy.uint8)
elif m < 65536:
self._dtype = numpy.dtype(numpy.uint16)
elif m < 2147483648:
self._dtype = numpy.dtype(numpy.uint32)
logger.warning('32-bit pixels are not really supported by the netpgm standard')
else:
raise IOError('could not figure out what kind of pixels you have')
s = line.lsplit(' ', 1)
if s[0] not in P7HEADERITEMS:
raise IOError('Illegal pam (netpnm p7) headeritem %s' % s[0])
self.header[s[0]] = s[1]
else:
values = list(line.split())
while len(values) < len(HEADERITEMS):
line = f.readline()
while line[0] == '#':
line = f.readline()
values += line.split()
for k, v in zip(HEADERITEMS, values):
self.header[k] = v.strip()
# set the dimensions
dim1 = int(self.header[six.b("WIDTH")])
dim2 = int(self.header[six.b("HEIGHT")])
self._shape = dim2, dim1
# figure out how many bytes are used to store the data
# case construct here!
m = int(self.header[six.b('MAXVAL')])
if m < 256:
self._dtype = numpy.dtype(numpy.uint8)
elif m < 65536:
self._dtype = numpy.dtype(numpy.uint16)
elif m < 2147483648:
self._dtype = numpy.dtype(numpy.uint32)
logger.warning('32-bit pixels are not really supported by the netpgm standard')
else:
raise IOError('could not figure out what kind of pixels you have')
def _readheader(self, f):
# pnm images have a 3-line header but ignore lines starting with '#'
# 1st line contains the pnm image sub format
# 2nd line contains the image pixel dimension
# 3rd line contains the maximum pixel value (at least for grayscale - check this)
line = f.readline().strip()
if line not in SUBFORMATS:
raise IOError('unknown subformat of pnm: %s' % line)
else:
self.header[six.b('SUBFORMAT')] = line
if self.header[six.b('SUBFORMAT')] == 'P7':
# this one has a special header
while six.b('ENDHDR') not in line:
line = f.readline()
while(line[0] == '#'):
line = f.readline()
s = line.lsplit(' ', 1)
if s[0] not in P7HEADERITEMS:
raise IOError('Illegal pam (netpnm p7) headeritem %s' % s[0])
self.header[s[0]] = s[1]
else:
values = list(line.split())
while len(values) < len(HEADERITEMS):
line = f.readline()
while line[0] == '#':
line = f.readline()
values += line.split()
if s[0] not in P7HEADERITEMS:
raise IOError('Illegal pam (netpnm p7) headeritem %s' % s[0])
self.header[s[0]] = s[1]
else:
values = list(line.split())
while len(values) < len(HEADERITEMS):
line = f.readline()
while line[0] == '#':
line = f.readline()
values += line.split()
for k, v in zip(HEADERITEMS, values):
self.header[k] = v.strip()
# set the dimensions
dim1 = int(self.header[six.b("WIDTH")])
dim2 = int(self.header[six.b("HEIGHT")])
self._shape = dim2, dim1
# figure out how many bytes are used to store the data
# case construct here!
m = int(self.header[six.b('MAXVAL')])
if m < 256:
self._dtype = numpy.dtype(numpy.uint8)
elif m < 65536:
self._dtype = numpy.dtype(numpy.uint16)
elif m < 2147483648:
self._dtype = numpy.dtype(numpy.uint32)
logger.warning('32-bit pixels are not really supported by the netpgm standard')
else:
raise IOError('could not figure out what kind of pixels you have')
def write(self, fname):
"""
try to write image. For now, limited to
:param fname: name of the file
"""
self.header[six.b("SUBFORMAT")] = "P5"
self.header[six.b("WIDTH")] = self.shape[-1]
self.header[six.b("HEIGHT")] = self.shape[-2]
self.header[six.b("MAXVAL")] = self.data.max()
header = six.b(" ".join([str(self.header[key]) for key in HEADERITEMS[1:]]))
with open(fname, "wb") as fobj:
fobj.write(six.b("P5 \n"))
fobj.write(header)
fobj.write(six.b(" \n"))
if numpy.little_endian:
fobj.write(self.data.byteswap().tobytes())
else:
fobj.write(self.data.tobytes())
def write(self, fname):
"""
try to write image. For now, limited to
:param fname: name of the file
"""
self.header[six.b("SUBFORMAT")] = "P5"
self.header[six.b("WIDTH")] = self.shape[-1]
self.header[six.b("HEIGHT")] = self.shape[-2]
self.header[six.b("MAXVAL")] = self.data.max()
header = six.b(" ".join([str(self.header[key]) for key in HEADERITEMS[1:]]))
with open(fname, "wb") as fobj:
fobj.write(six.b("P5 \n"))
fobj.write(header)
fobj.write(six.b(" \n"))
if numpy.little_endian:
fobj.write(self.data.byteswap().tobytes())
else:
fobj.write(self.data.tobytes())
def write(self, fname):
"""
try to write image. For now, limited to
:param fname: name of the file
"""
self.header[six.b("SUBFORMAT")] = "P5"
self.header[six.b("WIDTH")] = self.shape[-1]
self.header[six.b("HEIGHT")] = self.shape[-2]
self.header[six.b("MAXVAL")] = self.data.max()
header = six.b(" ".join([str(self.header[key]) for key in HEADERITEMS[1:]]))
with open(fname, "wb") as fobj:
fobj.write(six.b("P5 \n"))
fobj.write(header)
fobj.write(six.b(" \n"))
if numpy.little_endian:
fobj.write(self.data.byteswap().tobytes())
else:
fobj.write(self.data.tobytes())
"""
__authors__ = ["Jérôme Kieffer", "Henning O. Sorensen", "Erik Knudsen"]
__date__ = "03/04/2020"
__license__ = "MIT"
__copyright__ = "ESRF, Grenoble & Risoe National Laboratory"
__status__ = "stable"
import logging
import numpy
logger = logging.getLogger(__name__)
from .fabioimage import FabioImage
from .fabioutils import six
SUBFORMATS = [six.b(i) for i in ('P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7')]
HEADERITEMS = [six.b(i) for i in ('SUBFORMAT', 'WIDTH', 'HEIGHT', 'MAXVAL')]
P7HEADERITEMS = [six.b(i) for i in ('WIDTH', 'HEIGHT', 'DEPTH', 'MAXVAL', 'TUPLTYPE', 'ENDHDR')]
class PnmImage(FabioImage):
DESCRIPTION = "PNM file format"
DEFAULT_EXTENSIONS = ["pnm", "pgm", "pbm"]
def __init__(self, *arg, **kwargs):
FabioImage.__init__(self, *arg, **kwargs)
self.header['Subformat'] = 'P5'
def _readheader(self, f):