Python PIL.ImageFile.Parser() Examples
The following are 10
code examples of PIL.ImageFile.Parser().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
PIL.ImageFile
, or try the search function
.
Example #1
Source File: upsidedownternet.py From piSociEty with GNU General Public License v3.0 | 6 votes |
def response(self, response, request, data): try: isImage = getattr(request, 'isImage') except AttributeError: isImage = False if isImage: try: #For some reason more images get parsed using the parser #rather than a file...PIL still needs some work I guess p = ImageFile.Parser() p.feed(data) im = p.close() im = im.transpose(Image.ROTATE_180) output = StringIO() im.save(output, format=self.imageType) data = output.getvalue() output.close() self.clientlog.info("Flipped image", extra=request.clientInfo) except Exception as e: self.clientlog.info("Error: {}".format(e), extra=request.clientInfo) return {'response': response, 'request': request, 'data': data}
Example #2
Source File: upsidedownternet.py From MITMf with GNU General Public License v3.0 | 6 votes |
def response(self, response, request, data): try: isImage = getattr(request, 'isImage') except AttributeError: isImage = False if isImage: try: #For some reason more images get parsed using the parser #rather than a file...PIL still needs some work I guess p = ImageFile.Parser() p.feed(data) im = p.close() im = im.transpose(Image.ROTATE_180) output = StringIO() im.save(output, format=self.imageType) data = output.getvalue() output.close() self.clientlog.info("Flipped image", extra=request.clientInfo) except Exception as e: self.clientlog.info("Error: {}".format(e), extra=request.clientInfo) return {'response': response, 'request': request, 'data': data}
Example #3
Source File: test_file_jpeg2k.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_parser_feed(self): # Arrange from PIL import ImageFile with open('Tests/images/test-card-lossless.jp2', 'rb') as f: data = f.read() # Act p = ImageFile.Parser() p.feed(data) # Assert self.assertEqual(p.image.size, (640, 480))
Example #4
Source File: test_imagefile.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_ico(self): with open('Tests/images/python.ico', 'rb') as f: data = f.read() with ImageFile.Parser() as p: p.feed(data) self.assertEqual((48, 48), p.image.size)
Example #5
Source File: test_imagefile.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_raise_typeerror(self): with self.assertRaises(TypeError): parser = ImageFile.Parser() parser.feed(1)
Example #6
Source File: images.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def get_image_dimensions(file_or_path, close=False): """ Returns the (width, height) of an image, given an open file or a path. Set 'close' to True to close the file at the end if it is initially in an open state. """ # Try to import PIL in either of the two ways it can end up installed. try: from PIL import ImageFile as PILImageFile except ImportError: import ImageFile as PILImageFile p = PILImageFile.Parser() if hasattr(file_or_path, 'read'): file = file_or_path file_pos = file.tell() file.seek(0) else: file = open(file_or_path, 'rb') close = True try: # Most of the time PIL only needs a small chunk to parse the image and # get the dimensions, but with some TIFF files PIL needs to parse the # whole file. chunk_size = 1024 while 1: data = file.read(chunk_size) if not data: break p.feed(data) if p.image: return p.image.size chunk_size = chunk_size*2 return None finally: if close: file.close() else: file.seek(file_pos)
Example #7
Source File: images.py From python-compat-runtime with Apache License 2.0 | 5 votes |
def get_image_dimensions(file_or_path, close=False): """ Returns the (width, height) of an image, given an open file or a path. Set 'close' to True to close the file at the end if it is initially in an open state. """ # Try to import PIL in either of the two ways it can end up installed. try: from PIL import ImageFile as PILImageFile except ImportError: import ImageFile as PILImageFile p = PILImageFile.Parser() if hasattr(file_or_path, 'read'): file = file_or_path file_pos = file.tell() file.seek(0) else: file = open(file_or_path, 'rb') close = True try: while 1: data = file.read(1024) if not data: break p.feed(data) if p.image: return p.image.size return None finally: if close: file.close() else: file.seek(file_pos)
Example #8
Source File: get_subj_sizes.py From Data-digging with MIT License | 5 votes |
def getsizes(subject): # if it already exists just return it back try: return None, (subject['imsize_x_pix'], subject['imsize_y_pix']) except: try: return None, (subject[1]['meta_json']['imsize_x_pix'], subject[1]['meta_json']['imsize_y_pix']) except: #print("You shouldn't be here") uri = subject[1]['loc_im0'] # get file size *and* image size (None if not known) file = urllib.request.urlopen(uri) size = file.headers.get("content-length") if size: size = int(size) p = ImageFile.Parser() while True: data = file.read(1024) if not data: break p.feed(data) if p.image: return size, p.image.size break file.close() return size, None
Example #9
Source File: images.py From GTDWeb with GNU General Public License v2.0 | 4 votes |
def get_image_dimensions(file_or_path, close=False): """ Returns the (width, height) of an image, given an open file or a path. Set 'close' to True to close the file at the end if it is initially in an open state. """ from PIL import ImageFile as PillowImageFile p = PillowImageFile.Parser() if hasattr(file_or_path, 'read'): file = file_or_path file_pos = file.tell() file.seek(0) else: file = open(file_or_path, 'rb') close = True try: # Most of the time Pillow only needs a small chunk to parse the image # and get the dimensions, but with some TIFF files Pillow needs to # parse the whole file. chunk_size = 1024 while 1: data = file.read(chunk_size) if not data: break try: p.feed(data) except zlib.error as e: # ignore zlib complaining on truncated stream, just feed more # data to parser (ticket #19457). if e.args[0].startswith("Error -5"): pass else: raise if p.image: return p.image.size chunk_size *= 2 return None finally: if close: file.close() else: file.seek(file_pos)
Example #10
Source File: test_imagefile.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 4 votes |
def test_parser(self): def roundtrip(format): im = hopper("L").resize((1000, 1000)) if format in ("MSP", "XBM"): im = im.convert("1") test_file = BytesIO() im.copy().save(test_file, format) data = test_file.getvalue() parser = ImageFile.Parser() parser.feed(data) imOut = parser.close() return im, imOut self.assert_image_equal(*roundtrip("BMP")) im1, im2 = roundtrip("GIF") self.assert_image_similar(im1.convert('P'), im2, 1) self.assert_image_equal(*roundtrip("IM")) self.assert_image_equal(*roundtrip("MSP")) if "zip_encoder" in codecs: try: # force multiple blocks in PNG driver ImageFile.MAXBLOCK = 8192 self.assert_image_equal(*roundtrip("PNG")) finally: ImageFile.MAXBLOCK = MAXBLOCK self.assert_image_equal(*roundtrip("PPM")) self.assert_image_equal(*roundtrip("TIFF")) self.assert_image_equal(*roundtrip("XBM")) self.assert_image_equal(*roundtrip("TGA")) self.assert_image_equal(*roundtrip("PCX")) if EpsImagePlugin.has_ghostscript(): im1, im2 = roundtrip("EPS") # This test fails on Ubuntu 12.04, PPC (Bigendian) It # appears to be a ghostscript 9.05 bug, since the # ghostscript rendering is wonky and the file is identical # to that written on ubuntu 12.04 x64 # md5sum: ba974835ff2d6f3f2fd0053a23521d4a # EPS comes back in RGB: self.assert_image_similar(im1, im2.convert('L'), 20) if "jpeg_encoder" in codecs: im1, im2 = roundtrip("JPEG") # lossy compression self.assert_image(im1, im2.mode, im2.size) self.assertRaises(IOError, roundtrip, "PDF")