本文整理汇总了Python中django.core.files.images.ImageFile方法的典型用法代码示例。如果您正苦于以下问题:Python images.ImageFile方法的具体用法?Python images.ImageFile怎么用?Python images.ImageFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.files.images
的用法示例。
在下文中一共展示了images.ImageFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_logo_image
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def test_logo_image(self):
homepage = HomePage.objects.get()
self.assertIsNone(homepage.logo_image)
with self.settings(CONFERENCE_ID=self.conference.id):
# Test that default logo image appears.
response = self.client.get(homepage.url)
self.assertContains(response, "/logo.png")
# Replace default logo with a new image.
test_logo_name = Faker().uuid4()
image_file = ImageFile(
open("conf_site/cms/tests/test-logo.png", "rb"), test_logo_name
)
ImageModel = get_image_model()
image = ImageModel(file=image_file)
# The image must be saved before it is attached
# to the homepage.
image.save()
homepage.logo_image = image
homepage.save()
response = self.client.get(homepage.url)
self.assertNotContains(response, "/logo.288981a8dfa8.png")
self.assertContains(response, test_logo_name)
示例2: test_get
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def test_get(self):
"""Test GET when there are odlcs."""
odlc = Odlc(mission=self.mission,
user=self.team,
odlc_type=interop_api_pb2.Odlc.STANDARD)
odlc.save()
with open(test_image('A.jpg'), 'rb') as f:
odlc.thumbnail.save('%d.%s' % (odlc.pk, 'jpg'), ImageFile(f))
odlc.save()
response = self.client.get(odlcs_review_url)
self.assertEqual(200, response.status_code)
data = json.loads(response.content)
self.assertEqual(1, len(data))
self.assertIn('odlc', data[0])
self.assertIn('type', data[0]['odlc'])
self.assertEqual('STANDARD', data[0]['odlc']['type'])
示例3: setUpClass
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def setUpClass(cls):
super(BaseOrphanedMediaCommandTestsMixin, cls).setUpClass()
set_media_tag(get_random_name())
TestModelWithoutFile.objects.create(name='without file')
TestModel.objects.create(name='without file')
TestImageModel.objects.create(name='without image')
cls.file = cls.add_file_to_model(TestModel(name='with file')).file.name
cls.file_2 = cls.add_file_to_model(TestModel(name='with file')).file.name
image_model_instance = cls.add_file_to_model(TestImageModel(name='with file and image'))
cls.file_removed = image_model_instance.file.name
cls.add_file_to_model(image_model_instance)
cls.file_removed_2 = image_model_instance.file.name
cls.add_file_to_model(image_model_instance)
cls.file_3 = image_model_instance.file.name
image = ImageFile(open(os.path.join('tests', 'dummy-files', 'dummy-image.jpg'), 'rb'))
image_model_instance.image.save(get_random_name(), image)
cls.file_4 = image_model_instance.image.name
示例4: setUp
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def setUp(self):
"""
Creates a pristine temp directory (or deletes and recreates if it
already exists) that the model uses as its storage directory.
Sets up two ImageFile instances for use in tests.
"""
if os.path.exists(temp_storage_dir):
shutil.rmtree(temp_storage_dir)
os.mkdir(temp_storage_dir)
file_path1 = os.path.join(os.path.dirname(__file__), '4x8.png')
self.file1 = self.File(open(file_path1, 'rb'), name='4x8.png')
file_path2 = os.path.join(os.path.dirname(__file__), '8x4.png')
self.file2 = self.File(open(file_path2, 'rb'), name='8x4.png')
示例5: handle
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def handle(self, *args, **options):
# Get the only instance of Magazine Index Page
magazine_index_page = MagazineIndexPage.objects.get()
with open(options["file"]) as import_file:
issues = csv.DictReader(import_file)
for issue in issues:
response = requests.get(issue["cover_image_url"])
image_file = BytesIO(response.content)
image = Image(
title=issue["title"] + " cover image",
file=ImageFile(image_file, name=issue["cover_image_file_name"]),
)
image.save()
import_issue = MagazineIssue(
title=issue["title"],
publication_date=issue["publication_date"],
first_published_at=issue["publication_date"],
issue_number=issue["issue_number"],
cover_image=image,
)
# Add issue to site page hiererchy
magazine_index_page.add_child(instance=import_issue)
magazine_index_page.save()
self.stdout.write("All done!")
示例6: upload_img
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def upload_img(model, name, path):
with open(path, 'rb')as f:
img = ImageFile(f)
a = model(name=name)
a.img.save(name, img)
示例7: get_test_image_file
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def get_test_image_file(filename='test.png', colour='white', size=(640, 480)):
f = BytesIO()
image = PIL.Image.new('RGBA', size, colour)
image.save(f, 'PNG')
return ImageFile(f, name=filename)
示例8: get_test_image_file_jpeg
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def get_test_image_file_jpeg(filename='test.jpg', colour='white', size=(640, 480)):
f = BytesIO()
image = PIL.Image.new('RGB', size, colour)
image.save(f, 'JPEG')
return ImageFile(f, name=filename)
示例9: get_test_image_file_webp
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def get_test_image_file_webp(filename='test.webp', colour='white', size=(640, 480)):
f = BytesIO()
image = PIL.Image.new('RGB', size, colour)
image.save(f, 'WEBP')
return ImageFile(f, name=filename)
示例10: dataToImageFile
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def dataToImageFile(data):
image = NamedTemporaryFile(delete=False)
image.write(data)
image.flush()
return ImageFile(image)
示例11: shrunkImage
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def shrunkImage(picture, filename):
api_key = settings.TINYPNG_API_KEY
if not api_key or not filename.endswith('.png'):
return picture
img_shrunked = NamedTemporaryFile(delete=False)
shrink_info = shrink_file(
picture.name,
api_key=api_key,
out_filepath=img_shrunked.name
)
img_shrunked.flush()
return ImageFile(img_shrunked)
示例12: downloadFile
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def downloadFile(url):
img_temp = NamedTemporaryFile(delete=True)
req = urllib2.Request(url, headers={ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36' })
img_temp.write(urllib2.urlopen(req).read())
img_temp.flush()
return ImageFile(img_temp)
示例13: _store_external_image
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def _store_external_image(image_url: str) -> MozImage:
"""Download an image from the given URL and store it as a Wagtail image"""
response = requests.get(image_url)
filename = image_url.split("/")[-1]
image = MozImage(
title=filename, file=ImageFile(BytesIO(response.content), name=filename)
)
image.save()
return image
示例14: __call__
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def __call__(self, resolution, image_format='JPEG', name=None):
filename = name or 'default.{}'.format(image_format.lower())
color = 'blue'
image = Image.new('RGB', resolution, color)
image_data = BytesIO()
image.save(image_data, image_format)
image_content = base.ContentFile(image_data.getvalue())
return images.ImageFile(image_content.file, filename)
示例15: get_image
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import ImageFile [as 别名]
def get_image(self, name):
"""Get one of the test images from the test data directory."""
return ImageFile(open(TEST_DATA_ROOT + name + '.png'))