Python PIL.Image 模块,init() 实例源码
我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用PIL.Image.init()。
def get_bytes(self, format='PNG'):
'''
usage: i = Identicon('xx')
print(i.base64())
return: this image's base64 code
created by: liuzheng712
bug report: https://github.com/liuzheng712/identicons/issues
'''
self.calculate()
fp = io.BytesIO()
self.image.encoderinfo = {}
self.image.encoderconfig = ()
if format.upper() not in Image.SAVE:
Image.init()
save_handler = Image.SAVE[format.upper()]
try:
save_handler(self.image, fp, '')
finally:
fp.seek(0)
return fp
def test_pil_plugins(pyi_builder):
pyi_builder.test_source(
"""
# Verify packaging of PIL.Image. Specifically,the hidden import of FixTk
# importing tkinter is causing some problems.
from PIL.Image import fromstring
print(fromstring)
# PIL import hook should bundle all available PIL plugins. Verify that plugins
# are bundled.
from PIL import Image
Image.init()
MIN_PLUG_COUNT = 7 # Without all plugins the count is usually 6.
plugins = list(Image.SAVE.keys())
plugins.sort()
if len(plugins) < MIN_PLUG_COUNT:
raise SystemExit('No PIL image plugins were bundled!')
else:
print('PIL supported image formats: %s' % plugins)
""")
def test_pil_plugins(pyi_builder):
pyi_builder.test_source(
"""
# Verify packaging of PIL.Image. Specifically,the hidden import of FixTk
# importing tkinter is causing some problems.
from PIL.Image import fromstring
print(fromstring)
# PIL import hook should bundle all available PIL plugins. Verify that plugins
# are bundled.
from PIL import Image
Image.init()
MIN_PLUG_COUNT = 7 # Without all plugins the count is usually 6.
plugins = list(Image.SAVE.keys())
plugins.sort()
if len(plugins) < MIN_PLUG_COUNT:
raise SystemExit('No PIL image plugins were bundled!')
else:
print('PIL supported image formats: %s' % plugins)
""")
def get_available_image_extensions():
try:
from PIL import Image
except ImportError:
return []
else:
Image.init()
return [ext.lower()[1:] for ext in Image.EXTENSION.keys()]
def get_available_image_extensions():
try:
from PIL import Image
except ImportError:
return []
else:
Image.init()
return [ext.lower()[1:] for ext in Image.EXTENSION.keys()]
def test_pyttsx(pyi_builder):
pyi_builder.test_source(
"""
# Basic code example from pyttsx tutorial.
# http://packages.python.org/pyttsx/engine.html#examples
import pyttsx
engine = pyttsx.init()
engine.say('Sally sells seashells by the seashore.')
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
""")
def get_available_image_extensions():
try:
from PIL import Image
except ImportError:
return []
else:
Image.init()
return [ext.lower()[1:] for ext in Image.EXTENSION.keys()]
def save_image(image, destination=None, filename=None, **options):
"""
Save a PIL image.
"""
if destination is None:
destination = BytesIO()
filename = filename or ''
# Ensure plugins are fully loaded so that Image.EXTENSION is populated.
Image.init()
format = Image.EXTENSION.get(os.path.splitext(filename)[1].lower(), 'JPEG')
if format in ('JPEG', 'WEBP'):
options.setdefault('quality', 85)
saved = False
if format == 'JPEG':
if settings.THUMBNAIL_PROGRESSIVE and (
max(image.size) >= settings.THUMBNAIL_PROGRESSIVE):
options['progressive'] = True
try:
image.save(destination, format=format, optimize=1, **options)
saved = True
except IOError:
# Try again,without optimization (PIL can't optimize an image
# larger than ImageFile.MAXBLOCK,which is 64k by default). This
# shouldn't be triggered very often these days,as recent versions
# of pillow avoid the MAXBLOCK limitation.
pass
if not saved:
image.save(destination, **options)
if hasattr(destination, 'seek'):
destination.seek(0)
return destination
def get_available_image_extensions():
try:
from PIL import Image
except ImportError:
return []
else:
Image.init()
return [ext.lower()[1:] for ext in Image.EXTENSION.keys()]
def __init__(self, service_name='images', host_prefix=''):
"""Preloads PIL to load all modules in the unhardened environment.
Args:
service_name: Service name expected for all calls.
host_prefix: the URL prefix (protocol://host:port) to preprend to
image urls on a call to GetUrlBase.
"""
super(ImagesServiceStub, self).__init__(
service_name, max_request_size=MAX_REQUEST_SIZE)
self._blob_stub = images_blob_stub.ImagesBlobStub(host_prefix)
Image.init()
def get_available_image_extensions():
try:
from PIL import Image
except ImportError:
return []
else:
Image.init()
return [ext.lower()[1:] for ext in Image.EXTENSION.keys()]
def test_pyttsx(pyi_builder):
pyi_builder.test_source(
"""
# Basic code example from pyttsx tutorial.
# http://packages.python.org/pyttsx/engine.html#examples
import pyttsx
engine = pyttsx.init()
engine.say('Sally sells seashells by the seashore.')
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()
""")
def get_available_image_extensions():
try:
from PIL import Image
except ImportError:
return []
else:
Image.init()
return [ext.lower()[1:] for ext in Image.EXTENSION.keys()]
def calculate(self):
"""
Creates the identicon.
First three bytes are used to generate the color,
remaining bytes are used to create the drawing
"""
color = (self.hash & 0xff, self.hash >> 8 & 0xff, self.hash >> 16 & 0xff)
self.hash >>= 24 # skip first three bytes
square_x = square_y = 0 # init square position
for x in range(GRID_SIZE * (GRID_SIZE + 1) // 2):
if self.hash & 1:
x = BORDER_SIZE + square_x * SQUARE_SIZE
y = BORDER_SIZE + square_y * SQUARE_SIZE
self.draw.rectangle(
(x, y, x + SQUARE_SIZE, y + SQUARE_SIZE),
fill=color,
outline=color
)
# following is just for mirroring
x = BORDER_SIZE + (GRID_SIZE - 1 - square_x) * SQUARE_SIZE
self.draw.rectangle(
(x,
outline=color
)
self.hash >>= 1 # shift to right
square_y += 1
if square_y == GRID_SIZE: # done with first column
square_y = 0
square_x += 1
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。