本文整理汇总了Python中PIL.ImageFile.Parser方法的典型用法代码示例。如果您正苦于以下问题:Python ImageFile.Parser方法的具体用法?Python ImageFile.Parser怎么用?Python ImageFile.Parser使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PIL.ImageFile
的用法示例。
在下文中一共展示了ImageFile.Parser方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: response
# 需要导入模块: from PIL import ImageFile [as 别名]
# 或者: from PIL.ImageFile import Parser [as 别名]
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}
示例2: test_parser_feed
# 需要导入模块: from PIL import ImageFile [as 别名]
# 或者: from PIL.ImageFile import Parser [as 别名]
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))
示例3: test_ico
# 需要导入模块: from PIL import ImageFile [as 别名]
# 或者: from PIL.ImageFile import Parser [as 别名]
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)
示例4: test_raise_typeerror
# 需要导入模块: from PIL import ImageFile [as 别名]
# 或者: from PIL.ImageFile import Parser [as 别名]
def test_raise_typeerror(self):
with self.assertRaises(TypeError):
parser = ImageFile.Parser()
parser.feed(1)
示例5: get_image_dimensions
# 需要导入模块: from PIL import ImageFile [as 别名]
# 或者: from PIL.ImageFile import Parser [as 别名]
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)
示例6: get_image_dimensions
# 需要导入模块: from PIL import ImageFile [as 别名]
# 或者: from PIL.ImageFile import Parser [as 别名]
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)
示例7: getsizes
# 需要导入模块: from PIL import ImageFile [as 别名]
# 或者: from PIL.ImageFile import Parser [as 别名]
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
示例8: get_image_dimensions
# 需要导入模块: from PIL import ImageFile [as 别名]
# 或者: from PIL.ImageFile import Parser [as 别名]
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)
示例9: test_parser
# 需要导入模块: from PIL import ImageFile [as 别名]
# 或者: from PIL.ImageFile import Parser [as 别名]
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")