当前位置: 首页>>代码示例>>Python>>正文


Python Image.create方法代码示例

本文整理汇总了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)
#.........这里部分代码省略.........
开发者ID:KyubiSystems,项目名称:Yomiko,代码行数:103,代码来源:build_archive.py


注:本文中的app.models.Image.create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。