當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。