本文整理汇总了Python中app.models.Image.save方法的典型用法代码示例。如果您正苦于以下问题:Python Image.save方法的具体用法?Python Image.save怎么用?Python Image.save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app.models.Image
的用法示例。
在下文中一共展示了Image.save方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: import_from_directory
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import save [as 别名]
def import_from_directory(path_to_images):
connect("eventum")
creator = User.objects().get(gplus_id="super")
filenames = os.listdir(path_to_images)
filenames = [fn for fn in filenames if not fn.startswith(".")]
failures = []
for filename in filenames:
if Image.objects(filename=filename).count() > 0:
img = Image.objects().get(filename=filename)
img.delete()
old_path = os.path.join(path_to_images, filename)
shutil.copy(old_path, config["UPLOAD_FOLDER"])
default_path = config["RELATIVE_UPLOAD_FOLDER"] + filename
image = Image(filename=filename, default_path=default_path, creator=creator)
try:
image.save()
except ValidationError as e:
failures.append(filename)
print "FAIL: %s" % filename
print e
print "Processed %s images." % len(filenames)
print "%s success." % (len(filenames) - len(failures))
print "%s failures." % len(failures)
示例2: upload
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import save [as 别名]
def upload():
"""Upload an image to Eventum
**Route:** ``/admin/media/upload``
**Methods:** ``POST``
"""
form = UploadImageForm(request.form)
uploaded_from = form.uploaded_from.data
if form.validate_on_submit():
f = request.files['image']
if f and allowed_file(f.filename.lower()):
filename = create_filename(f, request.form['filename'])
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
image = Image(filename=filename,
default_path=app.config['RELATIVE_UPLOAD_FOLDER']+filename,
creator=g.user)
image.save()
return redirect(url_for('.index'))
flash("Filename {} is invalid".format(f.filename))
if form.errors:
flash(form.errors)
if uploaded_from:
return redirect(uploaded_from)
return render_template('admin/media/upload.html', form=form)
示例3: save
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import save [as 别名]
def save(self):
super(ImageFile, self).save()
img = Image()
img.file_id = self.id
img.image = self.base + '.' + self.ext
img.save()
return self.base
示例4: create_images
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import save [as 别名]
def create_images(num_images, superuser, printer):
"""Creates ``num_images`` image objects in the database. It will download
sample images from http://lorempixel.com, and add database entries.
:param int num_images: The number of images to create
:param superuser: The superuser object to associate with the images.
:type superuser: :class:`~app.models.User`
:param printer: The object to manage progress printing.
:type printer: :class:`~script.cli.ProgressPrinter`
:returns: A list of images that now exist.
:rtype: list(:class:`~app.models.Image`)
"""
print "Generating images..."
printer.line()
successes = []
failures = []
skips = []
for width in range(400, 1600, (1600 - 400) / num_images):
height = width / 2
filename = BASE_FILENAME.format(width, height)
path = config['UPLOAD_FOLDER'] + filename
url = BASE_URL.format(width, height)
printer.begin_status_line(filename)
# Download image if it doesn't exist already
if not exists(path):
try:
urllib.urlretrieve(url, path)
except IOError:
failures.append((filename, ''))
printer.status_fail()
continue # Failed to download, move on to the next image.
# Insert or fetch image from database
if Image.objects(filename=filename).count() == 0:
image = Image(filename=filename,
default_path=path,
creator=superuser)
image.save()
successes.append((filename, path))
printer.status_success()
else:
skips.append((filename, path))
printer.status_skip()
printer.line()
printer.results(len(successes), len(skips), len(failures))
return successes + skips
示例5: upload
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import save [as 别名]
def upload():
"""Upload an image to Eventum
:returns: A JSON containing the status of the file upload, or error
messages, if any.
:rtype: json
"""
form = UploadImageForm(request.form)
if form.validate_on_submit():
f = request.files['image']
if f:
filename = create_filename(f, request.form['filename'])
f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
default_path = app.config['RELATIVE_UPLOAD_FOLDER'] + filename
image = Image(filename=filename,
default_path=default_path,
creator=g.user)
image.save()
return jsonify({"status": "true"})
if form.errors:
return jsonify(form.errors)
return jsonify({"status": "error"})