Beispiel #1
0
def test_unsupported_module():
    # Arrange
    module = "unsupported_module"
    # Act / Assert
    with pytest.raises(ValueError):
        features.check_module(module)
    with pytest.raises(ValueError):
        features.version_module(module)
Beispiel #2
0
def test_check():
    # Check the correctness of the convenience function
    for module in features.modules:
        assert features.check_module(module) == features.check(module)
    for codec in features.codecs:
        assert features.check_codec(codec) == features.check(codec)
    for feature in features.features:
        assert features.check_feature(feature) == features.check(feature)
Beispiel #3
0
 def test_check(self):
     # Check the correctness of the convenience function
     for module in features.modules:
         self.assertEqual(features.check_module(module),
                          features.check(module))
     for codec in features.codecs:
         self.assertEqual(features.check_codec(codec),
                          features.check(codec))
     for feature in features.features:
         self.assertEqual(features.check_feature(feature),
                          features.check(feature))
Beispiel #4
0
 def test_check(self):
     # Check the correctness of the convenience function
     for module in features.modules:
         self.assertEqual(features.check_module(module),
                          features.check(module))
     for codec in features.codecs:
         self.assertEqual(features.check_codec(codec),
                          features.check(codec))
     for feature in features.features:
         self.assertEqual(features.check_feature(feature),
                          features.check(feature))
Beispiel #5
0
 def test_check_modules(self):
     for feature in features.modules:
         self.assertIn(features.check_module(feature), [True, False])
     for feature in features.codecs:
         self.assertIn(features.check_codec(feature), [True, False])
Beispiel #6
0
 def test_check_features(self):
     for feature in features.modules:
         self.assertTrue(features.check_module(feature) in [True, False, None])
     for feature in features.codecs:
         self.assertTrue(features.check_codec(feature) in [True, False])
Beispiel #7
0
 def test_unsupported_module(self):
     # Arrange
     module = "unsupported_module"
     # Act / Assert
     self.assertRaises(ValueError, lambda: features.check_module(module))
Beispiel #8
0
def test_check_modules():
    for feature in features.modules:
        assert features.check_module(feature) in [True, False]
Beispiel #9
0
                    (
                            fool_match or bot_was_mentioned or
                            (from_user == bot.get_me() and (random.random() < 0.1 or '?' in text)) or
                            random.random() < 0.01)
            ):
        if 'допиши' in text.lower(
        ) and message.reply_to_message and message.reply_to_message.text:
            neural_text = neuro_complete(message.reply_to_message.text)
        else:
            neural_text = neuro_response(last_messages)
        last_messages.append(neural_text)
        message.reply_text(neural_text, quote=True)


def calculate_oh(match: Optional[Match[AnyStr]]) -> str:
    return random.randint(1, 8) * match.group(1).lower()[0] + random.randint(
        1, 8) * 'х' + random.randint(0, 3) * '.'


if __name__ == '__main__':
    if not features.check_module('webp'):
        sys.exit('no webp support')
    start_handler = CommandHandler(str('start'), start)
    wut_handler = MessageHandler(
        Filters.text & ~Filters.update.edited_message,
        huge_and_ugly_mega_text_handler_whaddaya_gonna_do_about_huh_its_my_bot)
    dispatcher.add_handler(start_handler)
    dispatcher.add_handler(wut_handler)
    updater.start_polling(drop_pending_updates=True)
    updater.idle()
def read_root():
    f = features.check_module('webp')
    print(f)
    return {"Hello": str(f)}
Beispiel #11
0
 def test_check_modules(self):
     for feature in features.modules:
         self.assertIn(features.check_module(feature), [True, False])
     for feature in features.codecs:
         self.assertIn(features.check_codec(feature), [True, False])
Beispiel #12
0
 def test_check_features(self):
     for feature in features.modules:
         self.assertTrue(
             features.check_module(feature) in [True, False, None])
     for feature in features.codecs:
         self.assertTrue(features.check_codec(feature) in [True, False])
Beispiel #13
0
 def test_unsupported_module(self):
     # Arrange
     module = "unsupported_module"
     # Act / Assert
     self.assertRaises(ValueError, lambda: features.check_module(module))
Beispiel #14
0
    def execute(self, args):
        if features.check_module("webp") == False:
            print(
                "Error: The WebP module for Pillow seems to be missing, please make sure to install it."
            )
            return

        if INPUT_PATH is None:
            print(
                f"Invalid IMAGE_INPUT_PATH value '{INPUT_PATH}' received. Aborting."
            )
            return

        RAW_PATH = os.path.join(os.getcwd(), INPUT_PATH, CLEAN_FOLDER)
        OUTPUT_PATH = os.path.join(os.getcwd(), INPUT_PATH, OUTPUT_FOLDER)

        try:
            # Create a folder to store the images
            if not os.path.exists(OUTPUT_PATH):
                os.mkdir(OUTPUT_PATH)

            # Create a folder to store the corrupted images
            if not os.path.exists(CORRUPTED_FOLDER):
                os.mkdir(CORRUPTED_FOLDER)

            # Process every file
            for filepath_src in self.file_iter(RAW_PATH, ".jpg"):
                filepath_dst = os.path.splitext(filepath_src)[0] + ".jpg"
                filepath_dst = filepath_dst.replace(CLEAN_FOLDER,
                                                    OUTPUT_FOLDER)
                # print(filepath_dst)

                print(f"Processing: {filepath_src}")

                try:
                    # Load image
                    image_obj = Image.open(filepath_src)

                    # Convert to sRGB Color Profile
                    img_conv = self.convert_to_srgb(image_obj)

                    if image_obj.info.get("icc_profile",
                                          "") != img_conv.info.get(
                                              "icc_profile", ""):
                        # ICC profile was changed -> save converted file
                        img_conv.save(
                            filepath_dst,
                            format="JPEG",
                            quality=100,
                            icc_profile=img_conv.info.get("icc_profile", ""),
                        )
                    else:
                        # No need to change color profile, just save as JPEG
                        img_conv.save(filepath_dst, format="JPEG", quality=100)
                except Exception as e:
                    print(f"Error: could not open image: {filepath_src}.")
                    # print(e)

                    # Move file into 'corrupted' folder
                    shutil.move(
                        filepath_src,
                        filepath_src.replace(RAW_FOLDER, CORRUPTED_FOLDER))

                    # Ignore errors for invalid images
                    continue

        except Exception as e:
            print("Error: could not crop image.")
            print(e)
Beispiel #15
0
def update(session, scan_dir, force=False):
    songs = []
    scan_dir = Path(scan_dir)
    L.info(f"Scanning directory {scan_dir} for new files")
    warnings.simplefilter('error', Image.DecompressionBombWarning)

    image_format = "WEBP" if features.check_module('webp') else "JPEG"

    for root, dirs, files in os.walk(scan_dir):
        root = Path(root)
        for path in sorted(files):
            try:
                path = root / path
                path = path.absolute()
                if path.suffix.lower() not in ('.flac', '.mp3', '.m4a', '.aac',
                                               '.opus', '.ogg', '.tta', '.wav',
                                               '.alac', '.caf'
                                               '.aiff', '.aif'):
                    continue
                L.debug(f"Found path {path}")
                existingSong = session.query(Song).filter_by(
                    path=str(path)).one_or_none()
                if not force and existingSong:
                    L.info(
                        f"Song {existingSong} (path: {existingSong.path}) already exists in database, skipping"
                    )
                    continue
                hash = compute_hash(path)
                existingSong = session.query(Song).filter_by(
                    hash=hash).one_or_none()
                if existingSong and not force:
                    if not existingSong.mb_metadata:
                        augment_with_musicbrainz_metadata(existingSong)
                    L.info(
                        f"Song {existingSong} (path: {existingSong.path}, hash: {existingSong.hash}) already exists in database (new path: {path}), skipping"
                    )
                    continue
                metadata = TinyTag.get(str(path),
                                       tags=True,
                                       duration=True,
                                       image=True)
                if existingSong and existingSong.song_metadata:
                    song_metadata = dict(existingSong.song_metadata)
                else:
                    song_metadata = {}

                if not metadata.artist:
                    metadata.artist = metadata.albumartist

                # Undo the mess TinyTag causes when unknown encoding happens
                artistStr = tryFixText(metadata.artist)
                albumStr = tryFixText(metadata.album)
                titleStr = tryFixText(metadata.title)

                song_metadata['file_metadata'] = metadata.as_dict()

                artist = session.query(Artist).filter_by(
                    name=artistStr).one_or_none()
                if not artist and metadata.artist:
                    artist = Artist(name=artistStr)
                    L.debug(f"Creating artist {artist}")
                album = session.query(Album).filter_by(
                    name=albumStr).one_or_none()
                if not album and metadata.album:
                    album = Album(name=albumStr)
                    L.debug(f"Creating album {album}")

                if existingSong:
                    existingSong.title = titleStr
                    existingSong.path = str(path)
                    existingSong.duration = int(metadata.duration)
                    existingSong.hash = hash
                    existingSong.artist = artist
                    existingSong.album = album
                    if not existingSong.audio_hash:
                        try:
                            audio_hash, crc32 = compute_audio_hash(path)
                            existingSong.audio_hash = audio_hash
                            song_metadata['audio_crc'] = crc32
                        except Exception as e:
                            L.info(f"Failed to hash audio data {path}: {e}")
                    existingSong.song_metadata = song_metadata
                    song = existingSong
                else:
                    audio_hash = None
                    crc32 = None
                    try:
                        audio_hash, crc32 = compute_audio_hash(path)
                        song_metadata['audio_crc'] = crc32
                    except Exception as e:
                        L.info(f"Failed to hash audio data {path}: {e}")
                    song = Song(title=titleStr,
                                path=str(path),
                                duration=int(metadata.duration),
                                hash=hash,
                                status=SongStatus.active,
                                artist=artist,
                                album=album,
                                audio_hash=audio_hash,
                                song_metadata=song_metadata)

                if not song.mb_metadata:
                    augment_with_musicbrainz_metadata(song)

                cover = None
                image_type = None
                image_data = metadata.get_image()
                if image_data:
                    image_type = "embedded"

                if not image_type:
                    for filename in os.listdir(os.path.dirname(path)):
                        fullPath = os.path.join(os.path.dirname(path),
                                                filename)
                        if re.search(
                                r'^(cover|folder|front|front[_\- ]cover|cd|disc)[ _0-9]*\.(jpe?g|png)$',
                                filename,
                                re.IGNORECASE) and os.path.isfile(fullPath):
                            try:
                                image_data = open(fullPath, 'rb').read()
                                image_type = "file"
                                break
                            except Exception as e:
                                L.info(
                                    f"Failed to open cover file {fullPath}: {e}"
                                )

                # if not image_type and song.mb_metadata:
                # TODO: fetch covers from MusicBrainz

                if image_type and image_data:
                    L.info(f"Found {image_type} type image!")

                    image_hash = compute_hash_string(image_data)
                    cover = session.query(Cover).filter_by(
                        hash=image_hash).one_or_none()
                    if not cover:
                        L.info(f"Creating new cover")
                        try:
                            image_mime = magic.from_buffer(image_data,
                                                           mime=True)
                            L.info(f"Cover mime type: {image_mime}")
                        except Exception as e:
                            L.info(f"Could not guess mime type: {e}")
                            image_mime = "image/jpeg"

                        try:
                            im = Image.open(BytesIO(image_data))
                            thumb_small = im.copy()
                            thumb_small.thumbnail((55, 55), Image.LANCZOS)
                            small = BytesIO()
                            thumb_small.save(small,
                                             format=image_format,
                                             quality=80,
                                             method=6,
                                             optimize=True,
                                             progressive=True)
                            thumb_large = im.copy()
                            thumb_large.thumbnail((800, 800), Image.LANCZOS)
                            large = BytesIO()
                            thumb_large.save(large,
                                             format=image_format,
                                             quality=95,
                                             method=6,
                                             optimize=True,
                                             progressive=True)
                            cover = Cover(hash=image_hash,
                                          type=image_type,
                                          mime=image_mime,
                                          data=image_data,
                                          thumb_small=small.getvalue(),
                                          thumb_large=large.getvalue())
                        except Exception as e:
                            L.info(f"Failed to create thumbnails: {e}")
                            cover = Cover(hash=image_hash,
                                          type=image_type,
                                          mime=image_mime,
                                          data=image_data)

                        song.cover = cover
                    else:
                        L.info(f"Found existing cover")
                        song.cover = cover

                if existingSong:
                    existingSong = song
                    session.flush()
                    L.info(f"Updated song {existingSong}")
                    songs.append(existingSong)
                else:
                    session.add(song)
                    session.flush()
                    L.info(f"Added song {song}")
                    songs.append(song)

            except Exception as e:
                L.exception(f"Error adding track")
    return songs
Beispiel #16
0
    print("-"*68)
    print("Pillow", Image.PILLOW_VERSION, "TEST SUMMARY ")
    print("-"*68)
    print("Python modules loaded from", os.path.dirname(Image.__file__))
    print("Binary modules loaded from", os.path.dirname(Image.core.__file__))
    print("-"*68)
    for name, feature in [
        ("pil", "PIL CORE"),
        ("tkinter", "TKINTER"),
        ("freetype2", "FREETYPE2"),
        ("littlecms2", "LITTLECMS2"),
        ("webp", "WEBP"),
        ("transp_webp", "Transparent WEBP")
    ]:
        supported = features.check_module(name)

        if supported is None:
            # A method was being tested, but the module required
            # for the method could not be correctly imported
            pass
        elif supported:
            print("---", feature, "support ok")
        else:
            print("***", feature, "support not installed")
    for name, feature in [
        ("jpg", "JPEG"),
        ("jpg_2000", "OPENJPEG (JPEG2000)"),
        ("zlib", "ZLIB (PNG/ZIP)"),
        ("libtiff", "LIBTIFF")
    ]:
Beispiel #17
0
if __name__ == "__main__":
    # check build sanity

    exit_status = 0

    print("-" * 68)
    print("Pillow", Image.PILLOW_VERSION, "TEST SUMMARY ")
    print("-" * 68)
    print("Python modules loaded from", os.path.dirname(Image.__file__))
    print("Binary modules loaded from", os.path.dirname(Image.core.__file__))
    print("-" * 68)
    for name, feature in [("pil", "PIL CORE"), ("tkinter", "TKINTER"),
                          ("freetype2", "FREETYPE2"),
                          ("littlecms2", "LITTLECMS2"), ("webp", "WEBP"),
                          ("transp_webp", "Transparent WEBP")]:
        supported = features.check_module(name)

        if supported is None:
            # A method was being tested, but the module required
            # for the method could not be correctly imported
            pass
        elif supported:
            print("---", feature, "support ok")
        else:
            print("***", feature, "support not installed")
    for name, feature in [("jpg", "JPEG"), ("jpg_2000", "OPENJPEG (JPEG2000)"),
                          ("zlib", "ZLIB (PNG/ZIP)"), ("libtiff", "LIBTIFF")]:
        if features.check_codec(name):
            print("---", feature, "support ok")
        else:
            print("***", feature, "support not installed")
Beispiel #18
0
from PIL import Image
from PIL import features
import os


def convert(inputFile, outputFile):
    im = Image.open(inputFile).convert("RGB")
    im.save(outputFile, "jpeg")

    print("Converted {} to {}".format(inputFile, outputFile))


def main():
    for f in os.listdir("."):
        if f.upper().endswith(".WEBP"):
            outputFile = f[:-5] + ".jpg"

            if os.path.exists(outputFile):
                print("Warning! {} not converted, because {} already exsits".format(f, outputFile))
            else:
                convert(f, outputFile)


if __name__ == "__main__":
    print("WEBP:", features.check_module("webp"))
    main()