本文整理汇总了Python中willow.image.Image.open方法的典型用法代码示例。如果您正苦于以下问题:Python Image.open方法的具体用法?Python Image.open怎么用?Python Image.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类willow.image.Image
的用法示例。
在下文中一共展示了Image.open方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_raises_error_on_invalid_header
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def test_raises_error_on_invalid_header(self):
import imghdr
f = io.BytesIO()
f.write(b'Not an image')
f.seek(0)
with self.assertRaises(UnrecognisedImageFormatError) as e:
Image.open(f)
示例2: has_jpeg_support
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def has_jpeg_support():
wagtail_jpg = os.path.join(os.path.dirname(__file__), 'check_files', 'wagtail.jpg')
succeeded = True
with open(wagtail_jpg, 'rb') as f:
try:
Image.open(f)
except (IOError, Image.LoaderError):
succeeded = False
return succeeded
示例3: test_image_format_detection
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def test_image_format_detection(self):
Image.loaders = {
'png': [
(0, self.FakeBackend),
],
'jpeg': [
(100, self.AnotherFakeBackend),
],
}
self.assertIsInstance(Image.open('tests/images/transparent.png').backend, self.FakeBackend)
self.assertIsInstance(Image.open('tests/images/flower.jpg').backend, self.AnotherFakeBackend)
示例4: test_image_detect_stream
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def test_image_detect_stream(self):
Image.loaders = {
'png': [
(0, self.FakeBackend),
],
'jpeg': [
(100, self.AnotherFakeBackend),
],
}
with open('tests/images/transparent.png', 'rb') as f:
self.assertIsInstance(Image.open(f).backend, self.FakeBackend)
with open('tests/images/flower.jpg', 'rb') as f:
self.assertIsInstance(Image.open(f).backend, self.AnotherFakeBackend)
示例5: get_willow_image
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def get_willow_image(self):
# Open file if it is closed
close_file = False
try:
image_file = self.file
if self.file.closed:
# Reopen the file
if self.is_stored_locally():
self.file.open('rb')
else:
# Some external storage backends don't allow reopening
# the file. Get a fresh file instance. #1397
storage = self._meta.get_field('file').storage
image_file = storage.open(self.file.name, 'rb')
close_file = True
except IOError as e:
# re-throw this as a SourceImageIOError so that calling code can distinguish
# these from IOErrors elsewhere in the process
raise SourceImageIOError(text_type(e))
# Seek to beginning
image_file.seek(0)
try:
yield WillowImage.open(image_file)
finally:
if close_file:
image_file.close()
示例6: convert_bmp_image
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def convert_bmp_image(self, file_path):
new_file_path = file_path.replace("bmp", "jpg")
with open(file_path, 'rb') as f:
img = WillowImage.open(f)
output = open(new_file_path, 'wb')
img.save_as_jpeg(output)
output.close()
return new_file_path
示例7: test_opens_gif
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def test_opens_gif(self):
import imghdr
f = io.BytesIO()
f.write(b'GIF89a')
f.seek(0)
image = Image.open(f)
self.assertIsInstance(image, GIFImageFile)
self.assertEqual(image.format_name, 'gif')
self.assertEqual(image.original_format, 'gif')
示例8: test_opens_png
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def test_opens_png(self):
import imghdr
f = io.BytesIO()
f.write(b'\x89PNG\x0d\x0a\x1a\x0a')
f.seek(0)
image = Image.open(f)
self.assertIsInstance(image, PNGImageFile)
self.assertEqual(image.format_name, 'png')
self.assertEqual(image.original_format, 'png')
示例9: test_opens_jpeg
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def test_opens_jpeg(self):
import imghdr
f = io.BytesIO()
f.write(b'\xff\xd8\xff\xe0\x00\x10JFIF\x00')
f.seek(0)
image = Image.open(f)
self.assertIsInstance(image, JPEGImageFile)
self.assertEqual(image.format_name, 'jpeg')
self.assertEqual(image.original_format, 'jpeg')
示例10: get_willow_image
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def get_willow_image(self):
try:
image_file = self.file.file # triggers a call to self.storage.open, so IOErrors from missing files will be raised at this point
except IOError as e:
# re-throw this as a SourceImageIOError so that calling code can distinguish
# these from IOErrors elsewhere in the process
raise SourceImageIOError(text_type(e))
image_file.open('rb')
image_file.seek(0)
return WillowImage.open(image_file)
示例11: get_willow_image
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def get_willow_image(self):
# Open file if it is closed
close_file = False
try:
if self.file.closed:
self.file.open('rb')
close_file = True
except IOError as e:
# re-throw this as a SourceImageIOError so that calling code can distinguish
# these from IOErrors elsewhere in the process
raise SourceImageIOError(text_type(e))
# Seek to beginning
self.file.seek(0)
try:
yield WillowImage.open(self.file)
finally:
if close_file:
self.file.close()
示例12: get_willow_image
# 需要导入模块: from willow.image import Image [as 别名]
# 或者: from willow.image.Image import open [as 别名]
def get_willow_image(self):
with self.open_file() as image_file:
yield WillowImage.open(image_file)