def call_fixture(self, request): image_filename, expected_class = request.param image_path = test_file(image_filename) with open(image_path, 'rb') as f: blob = f.read() image_stream = BytesIO(blob) image_stream.seek(666) return image_stream, expected_class
def from_offset_fixture(self): bytes_ = b'\x00\x00\x00\x2A\x00\x00\x00\x18\x01' stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN) offset, horz_px_per_unit, vert_px_per_unit, units_specifier = (0, 42, 24, 1) return (stream_rdr, offset, horz_px_per_unit, vert_px_per_unit, units_specifier)
def from_stream_fixture(self, request, _App0Marker__init_): bytes_ = b'\x00\x10JFIF\x00\x01\x01\x01\x00\x2A\x00\x18' stream_reader = StreamReader(BytesIO(bytes_), BIG_ENDIAN) marker_code, offset, length = JPEG_MARKER_CODE.APP0, 0, 16 density_units, x_density, y_density = 1, 42, 24 return (stream_reader, marker_code, offset, _App0Marker__init_, length, density_units, x_density, y_density)
def iter_fixture(self, _IfdEntryFactory_, ifd_entry_, ifd_entry_2_): stream_rdr = StreamReader(BytesIO(b'\x00\x02'), BIG_ENDIAN) offsets = [2, 14] ifd_parser = _IfdParser(stream_rdr, offset=0) expected_entries = [ifd_entry_, ifd_entry_2_] return (ifd_parser, _IfdEntryFactory_, stream_rdr, offsets, expected_entries)
def from_stream_fixture(self, request, _SofMarker__init_): bytes_ = b'\x00\x11\x00\x00\x2A\x00\x18' stream_reader = StreamReader(BytesIO(bytes_), BIG_ENDIAN) marker_code, offset, length = JPEG_MARKER_CODE.SOF0, 0, 17 px_width, px_height = 24, 42 return (stream_reader, marker_code, offset, _SofMarker__init_, length, px_width, px_height)
def next_fixture(self, request): start, marker_code, segment_offset = request.param bytes_ = b'\xFF\xD8\xFF\xE0\x00\x01\xFF\x00\xFF\xFF\xFF\xD9' stream_reader = StreamReader(BytesIO(bytes_), BIG_ENDIAN) marker_finder = _MarkerFinder(stream_reader) expected_code_and_offset = (marker_code, segment_offset) return marker_finder, start, expected_code_and_offset
def from_file(cls, image_descriptor): """ Return a new |Image| instance loaded from the image file identified by *image_descriptor*, a path or file-like object. """ if is_string(image_descriptor): path = image_descriptor with open(path, 'rb') as f: blob = f.read() stream = BytesIO(blob) filename = os.path.basename(path) else: stream = image_descriptor stream.seek(0) blob = stream.read() filename = None return cls._from_stream(stream, blob, filename)
def from_stream_fixture(self, Bmp__init__): cx, cy, horz_dpi, vert_dpi = 26, 43, 200, 96 bytes_ = ( b'fillerfillerfiller\x1A\x00\x00\x00\x2B\x00\x00\x00' b'fillerfiller\xB8\x1E\x00\x00\x00\x00\x00\x00' ) stream = BytesIO(bytes_) return stream, Bmp__init__, cx, cy, horz_dpi, vert_dpi
def from_stream_fixture(self, request, _App1Marker__init_, _tiff_from_exif_segment_): bytes_ = b'\x00\x42Exif\x00\x00' stream_reader = StreamReader(BytesIO(bytes_), BIG_ENDIAN) marker_code, offset, length = JPEG_MARKER_CODE.APP1, 0, 66 horz_dpi, vert_dpi = 42, 24 return (stream_reader, marker_code, offset, _App1Marker__init_, length, _tiff_from_exif_segment_, horz_dpi, vert_dpi)
def iter_offsets_fixture(self): bytes_ = b'-filler-\x00\x00\x00\x00IHDRxxxx\x00\x00\x00\x00IEND' stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN) chunk_parser = _ChunkParser(stream_rdr) expected_chunk_offsets = [ (PNG_CHUNK_TYPE.IHDR, 16), (PNG_CHUNK_TYPE.IEND, 28), ] return chunk_parser, expected_chunk_offsets
def it_can_construct_from_a_gif_stream(self, Gif__init__): cx, cy = 42, 24 bytes_ = b'filler\x2A\x00\x18\x00' stream = BytesIO(bytes_) gif = Gif.from_stream(stream) Gif__init__.assert_called_once_with(ANY, cx, cy, 72, 72) assert isinstance(gif, Gif)
def from_stream_fixture( self, _parse_value_, _IfdEntry__init_, value_): bytes_ = b'\x00\x01\x66\x66\x00\x00\x00\x02\x00\x00\x00\x03' stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN) offset, tag_code, value_count, value_offset = 0, 1, 2, 3 return ( stream_rdr, offset, _parse_value_, value_count, value_offset, _IfdEntry__init_, tag_code, value_ )
def it_can_construct_from_a_bmp_stream(self, Bmp__init__): cx, cy, horz_dpi, vert_dpi = 26, 43, 200, 96 bytes_ = (b'fillerfillerfiller\x1A\x00\x00\x00\x2B\x00\x00\x00' b'fillerfiller\xB8\x1E\x00\x00\x00\x00\x00\x00') stream = BytesIO(bytes_) bmp = Bmp.from_stream(stream) Bmp__init__.assert_called_once_with(ANY, cx, cy, horz_dpi, vert_dpi) assert isinstance(bmp, Bmp)
def it_can_construct_from_non_Exif_APP1_segment(self, _App1Marker__init_): bytes_ = b'\x00\x42Foobar' marker_code, offset, length = JPEG_MARKER_CODE.APP1, 0, 66 stream = StreamReader(BytesIO(bytes_), BIG_ENDIAN) app1_marker = _App1Marker.from_stream(stream, marker_code, offset) _App1Marker__init_.assert_called_once_with(ANY, marker_code, offset, length, 72, 72) assert isinstance(app1_marker, _App1Marker)
def it_can_construct_from_a_stream_and_offset(self, request, _SofMarker__init_): bytes_ = b'\x00\x11\x00\x00\x2A\x00\x18' marker_code, offset, length = JPEG_MARKER_CODE.SOF0, 0, 17 px_width, px_height = 24, 42 stream = StreamReader(BytesIO(bytes_), BIG_ENDIAN) sof_marker = _SofMarker.from_stream(stream, marker_code, offset) _SofMarker__init_.assert_called_once_with(ANY, marker_code, offset, length, px_width, px_height) assert isinstance(sof_marker, _SofMarker)
def it_can_construct_from_a_stream_and_offset(self, _App0Marker__init_): bytes_ = b'\x00\x10JFIF\x00\x01\x01\x01\x00\x2A\x00\x18' marker_code, offset, length = JPEG_MARKER_CODE.APP0, 0, 16 density_units, x_density, y_density = 1, 42, 24 stream = StreamReader(BytesIO(bytes_), BIG_ENDIAN) app0_marker = _App0Marker.from_stream(stream, marker_code, offset) _App0Marker__init_.assert_called_once_with(ANY, marker_code, offset, length, density_units, x_density, y_density) assert isinstance(app0_marker, _App0Marker)
def it_can_construct_from_a_stream_and_offset(self, _parse_value_, _IfdEntry__init_, value_): bytes_ = b"\x00\x01\x66\x66\x00\x00\x00\x02\x00\x00\x00\x03" stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN) offset, tag_code, value_count, value_offset = 0, 1, 2, 3 _parse_value_.return_value = value_ ifd_entry = _IfdEntry.from_stream(stream_rdr, offset) _parse_value_.assert_called_once_with(stream_rdr, offset, value_count, value_offset) _IfdEntry__init_.assert_called_once_with(ANY, tag_code, value_) assert isinstance(ifd_entry, _IfdEntry)
def fixture(self, request, ifd_entry_, _IfdEntry_, _AsciiIfdEntry_, _ShortIfdEntry_, _LongIfdEntry_, _RationalIfdEntry_): bytes_, entry_type = request.param entry_cls_ = { 'BYTE': _IfdEntry_, 'ASCII': _AsciiIfdEntry_, 'SHORT': _ShortIfdEntry_, 'LONG': _LongIfdEntry_, 'RATIONAL': _RationalIfdEntry_, 'CUSTOM': _IfdEntry_, }[entry_type] stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN) offset = 0 return stream_rdr, offset, entry_cls_, ifd_entry_
def it_can_construct_from_a_stream_and_offset(self, _App1Marker__init_, _tiff_from_exif_segment_): bytes_ = b'\x00\x42Exif\x00\x00' marker_code, offset, length = JPEG_MARKER_CODE.APP1, 0, 66 horz_dpi, vert_dpi = 42, 24 stream = StreamReader(BytesIO(bytes_), BIG_ENDIAN) app1_marker = _App1Marker.from_stream(stream, marker_code, offset) _tiff_from_exif_segment_.assert_called_once_with( stream, offset, length) _App1Marker__init_.assert_called_once_with(ANY, marker_code, offset, length, horz_dpi, vert_dpi) assert isinstance(app1_marker, _App1Marker)
def fixture( self, request, ifd_entry_, _IfdEntry_, _AsciiIfdEntry_, _ShortIfdEntry_, _LongIfdEntry_, _RationalIfdEntry_, ): bytes_, entry_type = request.param entry_cls_ = { "BYTE": _IfdEntry_, "ASCII": _AsciiIfdEntry_, "SHORT": _ShortIfdEntry_, "LONG": _LongIfdEntry_, "RATIONAL": _RationalIfdEntry_, "CUSTOM": _IfdEntry_, }[entry_type] stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN) offset = 0 return stream_rdr, offset, entry_cls_, ifd_entry_
def it_can_parse_a_long_int_IFD_entry(self): bytes_ = b'foobaroo\x00\x00\x00\x2A' stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN) val = _LongIfdEntry._parse_value(stream_rdr, 0, 1, None) assert val == 42
def from_stream_fixture(self, request, _Marker__init_): marker_code, offset, length = request.param bytes_ = b'\xFF\xD8\xFF\xE0\x00\x10' stream_reader = StreamReader(BytesIO(bytes_), BIG_ENDIAN) return stream_reader, marker_code, offset, _Marker__init_, length
def read_str_fixture(self): stream = BytesIO(b'\x01\x02foobar\x03\x04') stream_rdr = StreamReader(stream, BIG_ENDIAN) expected_string = 'foobar' return stream_rdr, expected_string
def it_raises_on_unrecognized_image_stream(self): stream = BytesIO(b'foobar 666 not an image stream') with pytest.raises(UnrecognizedImageError): _ImageHeaderFactory(stream)
def get_tiff_fixture(self, request, BytesIO_, substream_, Tiff_, tiff_): bytes_ = b'xfillerxMM\x00*\x00\x00\x00\x42' stream_reader = StreamReader(BytesIO(bytes_), BIG_ENDIAN) offset, segment_length, segment_bytes = 0, 16, bytes_[8:] return (stream_reader, offset, segment_length, BytesIO_, segment_bytes, substream_, Tiff_, tiff_)
def it_can_parse_an_ascii_string_IFD_entry(self): bytes_ = b'foobar\x00' stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN) val = _AsciiIfdEntry._parse_value(stream_rdr, None, 7, 0) assert val == 'foobar'
def non_Exif_fixture(self, request, _App1Marker__init_): bytes_ = b'\x00\x42Foobar' stream_reader = StreamReader(BytesIO(bytes_), BIG_ENDIAN) marker_code, offset, length = JPEG_MARKER_CODE.APP1, 0, 66 return stream_reader, marker_code, offset, _App1Marker__init_, length
def it_can_parse_a_rational_IFD_entry(self): bytes_ = b'\x00\x00\x00\x2A\x00\x00\x00\x54' stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN) val = _RationalIfdEntry._parse_value(stream_rdr, None, 1, 0) assert val == 0.5
def mk_stream_rdr_fixture(self, request, StreamReader_, stream_rdr_): bytes_, endian = request.param stream = BytesIO(bytes_) return stream, StreamReader_, endian, stream_rdr_
def from_filelike_fixture(self, _from_stream_, image_): image_path = test_file('python-icon.png') with open(image_path, 'rb') as f: blob = f.read() image_stream = BytesIO(blob) return image_stream, _from_stream_, blob, image_