本文整理汇总了Python中app.models.Image.create方法的典型用法代码示例。如果您正苦于以下问题:Python Image.create方法的具体用法?Python Image.create怎么用?Python Image.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app.models.Image
的用法示例。
在下文中一共展示了Image.create方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: scan_archive_file
# 需要导入模块: from app.models import Image [as 别名]
# 或者: from app.models.Image import create [as 别名]
def scan_archive_file(archives, filetype):
# Loop over zips & rars, get tags from titles
# Add volume to Volume DB
# Add tags to Tag DB (many-to-many mapping)
# Add pages to Image DB
# initialise mimetypes
mimetypes.init()
# Iterate over input list of archive files
for f in archives:
# Attempt to scan ZIP file
if filetype == 'zip':
try:
myfile = zipfile.ZipFile(INPUT_PATH+f, 'r')
except zipfile.BadZipfile as e:
raise Exception('"{}" is not a valid ZIP file! Error: {}'.format(f, e))
except:
print('Unknown error: ZIP extraction failed: {}'.format(f))
raise
# Scan RAR file
elif filetype == 'rar':
try:
myfile = rarfile.RarFile(INPUT_PATH+f, 'r')
except (rarfile.BadRarFile, rarfile.NotRarFile) as e:
raise Exception('"{}" is not a valid RAR file! Error: {}'.format(f, e))
except:
print('Unknown error: RAR extraction failed: {}'.format(f))
raise
else:
raise ValueError('Unrecognised archive type value: {}'.format(filetype))
# Get members from archive
members = myfile.namelist()
# Filter member list for images
members = [x for x in members if is_image(x) is True]
# Sort members
members = natsorted(members)
# Get number of images
member_count = len(members)
# If images found...
if member_count > 0:
# Generate MD5 checksum for archive file
md5 = md5sum(INPUT_PATH+f)
# Parse title, tags from filename
title, tags = tag.split_title_tags(f)
# Add volume to DB Volume table
vol = Volume.create(title=title, filename=f, md5=md5, filetype=filetype, num=member_count, comments='')
# Add tags to DB Tags table
for t in tags:
# check if tag already exists, insert if not
try:
new_tag = Tag.get(Tag.name == t)
except DoesNotExist:
new_tag = Tag.create(name=t, descr='')
# insert tag and volume id into TagRelation table
TagRelation.create(relVolume=vol.id, relTag=new_tag.id)
# Reset page counter (assume cover is page 0)
page = 0
# Generate display title
disptitle=title[:20]
if len(disptitle) < len(title):
disptitle = disptitle+'...'
disptitle.ljust(24)
# initialise progress bar display
widgets = [disptitle+': ', Counter(),'/'+str(member_count)+' ', Bar(marker='=', left='[', right=']'), ETA()]
pbar = ProgressBar(widgets=widgets, maxval=member_count).start()
# Attempt to create thumbnail directory if it doesn't already exist
path = THUMB_PATH + str(vol.id)
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise
# Iterate over images in zip file
for m in members:
# Guess mimetype for image from filename
(mimetype, encoding) = mimetypes.guess_type(m)
#.........这里部分代码省略.........