本文整理汇总了Python中imagekit.lib.Image.open方法的典型用法代码示例。如果您正苦于以下问题:Python Image.open方法的具体用法?Python Image.open怎么用?Python Image.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类imagekit.lib.Image
的用法示例。
在下文中一共展示了Image.open方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_default_photo
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def get_default_photo(self):
try:
return Photo.objects.filter(user_default=True)[0]
except IndexError:
user_fallback = "%s/photos/%s" % (settings.MEDIA_ROOT, "img_user_fallback.png")
try:
fp = open(user_fallback, "r")
image = Image.open(fp)
image.verify()
photo = Photo(user_default=True)
photo.save()
Photo.objects.filter(pk=photo.pk).update(image="photos/img_user_fallback.png")
photo = Photo.objects.get(pk=photo.pk)
fp.close()
return photo
except:
user_fallback = "%s/images/%s" % (settings.GLOBALS_STATIC_ROOT, "img_user_fallback.png")
fp = open(user_fallback, "r")
image = Image.open(fp)
image.verify()
fp2 = open(user_fallback, "r")
target_file = File(fp2)
name = "img_user_fallback.png"
photo = Photo(user_default=True)
photo.image.save(name, target_file, save=True)
fp.close()
fp2.close()
return photo
示例2: set_watermark
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def set_watermark(image):
watermark = Archive.objects.get(id=51)
watermark = Image.open(watermark.archive)
image = Image.open(image)
scaled = ImageWatermark(watermark, position=("center", "center"), scale=True, opacity=1)
img_scaled = scaled.process(image)
return img_scaled
示例3: get_watermark
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def get_watermark(self):
# open the image despite the format that the user provided for it
if self.watermark:
return self.watermark
if self.watermark_image:
return self.watermark_image
if self.watermark_file:
return Image.open(self.watermark_file)
if self.watermark_path:
return Image.open(self.watermark_path)
示例4: process_zipfile
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def process_zipfile(self):
if os.path.isfile(self.zip_file.path):
# TODO: implement try-except here
zip = zipfile.ZipFile(self.zip_file.path)
bad_file = zip.testzip()
if bad_file:
raise Exception('"%s" in the .zip archive is corrupt.' % bad_file)
count = 1
if self.gallery:
gallery = self.gallery
else:
gallery = Gallery.objects.create(title=self.title,
title_slug=slugify(self.title),
description=self.description,
is_public=self.is_public)
from cStringIO import StringIO
for filename in zip.namelist():
if filename.startswith('__'): # do not process meta files
continue
data = zip.read(filename)
if len(data):
try:
# the following is taken from django.newforms.fields.ImageField:
# load() is the only method that can spot a truncated JPEG,
# but it cannot be called sanely after verify()
trial_image = Image.open(StringIO(data))
trial_image.load()
# verify() is the only method that can spot a corrupt PNG,
# but it must be called immediately after the constructor
trial_image = Image.open(StringIO(data))
trial_image.verify()
except Exception, e:
# if a "bad" file is found we just skip it.
raise e
continue
while 1:
title = ' '.join([self.title, str(count)])
slug = slugify(title)
try:
p = Photo.objects.get(title_slug=slug)
except Photo.DoesNotExist:
photo = Photo(title=title,
title_slug=slug,
caption=self.caption,
is_public=self.is_public)
photo.image.save(filename, ContentFile(data))
gallery.photos.add(photo)
count = count + 1
break
count = count + 1
zip.close()
return gallery
示例5: generate_lenna
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def generate_lenna():
"""
See also:
http://en.wikipedia.org/wiki/Lenna
http://sipi.usc.edu/database/database.php?volume=misc&image=12
"""
tmp = tempfile.TemporaryFile()
lennapath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'assets', 'lenna-800x600-white-border.jpg')
with open(lennapath, "r+b") as lennafile:
Image.open(lennafile).save(tmp, 'JPEG')
tmp.seek(0)
return tmp
示例6: process
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def process(self, image):
mask = Image.open(os.path.join(settings.STATIC_ROOT, 'img/avatar-mask.png'))
layer = Image.new('RGBA', image.size)
layer.paste(mask)
newImage = Image.composite(layer, image, layer)
return newImage
示例7: resized
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def resized(self, h=0, w=0, footer=True, extended=True, crop=False, upscale=False):
"""Get the image, resized and optionally watermarked"""
fn = self.image.file.name
suffix = ['_']
if not w:
suffix.append('0')
else:
suffix.append(str(w))
suffix.append('x')
if not h:
suffix.append('0')
else:
suffix.append(str(h))
if footer or extended:
suffix.append('-')
if footer:
suffix.append('w')
if extended:
suffix.append('e')
suffix = ''.join(suffix)
fn = get_scaled_media_path(self, filename=fn, suffix=suffix)
if os.path.exists(fn):
img = Image.open(fn)
log.debug('returning cached image: %s', fn)
else:
img = Image.open(self.image.file.name)
cw, ch = img.size
if h == 0 and w == 0:
h = ch
w = cw
if h != ch or w != cw:
img = resize_image(img, h, w, crop=crop, upscale=upscale)
if footer or extended:
img = add_watermark(img, footer=footer, extended=extended)
img.save(fn)
log.debug('Created and cached new image: %s', fn)
return img, fn
示例8: __getattribute__
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def __getattribute__(self, key):
if key == "username":
if not object.__getattribute__(self, "username") and self.id:
self.username = self.user.username
self.save()
return self.username
if key == "main_profile_pic":
if not object.__getattribute__(self, "main_profile_pic"):
try:
default_photo = Photo.objects.filter(user_default=True)[0]
return default_photo
except IndexError:
user_fallback = "%s/photos/%s" % (settings.MEDIA_ROOT, "img_user_fallback.png")
try:
fp = open(user_fallback, "r")
image = Image.open(fp)
image.verify()
photo = Photo(user_default=True)
photo.save()
Photo.objects.filter(pk=photo.pk).update(image="photos/img_user_fallback.png")
photo = Photo.objects.get(pk=photo.pk)
fp.close()
return photo
except Exception, e:
user_fallback = "%s/images/%s" % (settings.GLOBALS_STATIC_ROOT, "img_user_fallback.png")
fp = open(user_fallback, "r")
image = Image.open(fp)
image.verify()
fp2 = open(user_fallback, "r")
target_file = File(fp2)
name = "img_user_fallback.png"
photo = Photo(user_default=True)
photo.image.save(name, target_file, save=True)
fp.close()
fp2.close()
return photo
示例9: create_user_profile
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def create_user_profile(self, user):
# Already exists, return
if self.filter(user=user).exists():
return user.get_profile(), False
up = self.create(user=user)
anonymous = User.objects.get(username='AnonymousUser')
if user.id == anonymous.id:
user_fallback = '%s/images/%s' % (settings.GLOBALS_STATIC_ROOT, 'img_user_fallback.png')
image = Image.open(user_fallback)
image.verify()
fp = open(user_fallback,'r')
target_file = File(fp)
name = 'img_user_fallback.png'
up.image.save(name, target_file, save=True)
fp.close()
up.save()
else:
up.image = anonymous.get_profile().image
up.save()
return user.get_profile(), True
示例10: get_picture_from_url
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def get_picture_from_url(img_url, alter_name=None, update_photo=None, get_raw_file=False):
"""
By default, we use uuid_as_name because this can promise that in multiple threads situation
no filename will be the same with each other.
"""
if img_url:
try:
path = settings.DOWNLOAD_DIRECTORY + str(uuid.uuid4())
filename = uuid.uuid4()
content = urllib.urlretrieve(smart_str(img_url),path)
content_type = content[1]['content-type']
if 'image' in content_type:
filetype = '.' + content_type.split('/')[1]
else:
raise InvalidImageException
# Verify picture
image = Image.open(content[0])
image.verify()
if get_raw_file:
return open(content[0])
target_file = File(open(content[0]))
name = '%s%s'%(filename, filetype)
photo = update_photo if update_photo else Photo()
photo.image.save(name, target_file, save=True)
os.remove(content[0])
return photo
except Exception, e:
import traceback, sys
etype, value, tb = sys.exc_info()
print('%s\n' % ''.join(traceback.format_exception(etype, value, tb, 20)))
return None
示例11: open_image
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def open_image(target):
target.seek(0)
img = Image.open(target)
img.copy = types.MethodType(_wrap_copy(img.copy), img, img.__class__)
return img
示例12: create_image
# 需要导入模块: from imagekit.lib import Image [as 别名]
# 或者: from imagekit.lib.Image import open [as 别名]
def create_image():
return Image.open(get_image_file())