Esempio n. 1
0
def GenerateNumPyImageFromPILImage(pil_image: PILImage.Image) -> numpy.array:

    (w, h) = pil_image.size

    try:

        s = pil_image.tobytes()

    except OSError as e:  # e.g. OSError: unrecognized data stream contents when reading image file

        raise HydrusExceptions.UnsupportedFileException(str(e))

    depth = len(s) // (w * h)

    return numpy.fromstring(s, dtype='uint8').reshape((h, w, depth))
Esempio n. 2
0
def GetFileInfo(path, mime=None, ok_to_look_for_hydrus_updates=False):

    size = os.path.getsize(path)

    if size == 0:

        raise HydrusExceptions.ZeroSizeFileException('File is of zero length!')

    if mime is None:

        mime = GetMime(
            path, ok_to_look_for_hydrus_updates=ok_to_look_for_hydrus_updates)

    if mime not in HC.ALLOWED_MIMES:

        if mime == HC.TEXT_HTML:

            raise HydrusExceptions.UnsupportedFileException(
                'Looks like HTML -- maybe the client needs to be taught how to parse this?'
            )

        elif mime == HC.APPLICATION_UNKNOWN:

            raise HydrusExceptions.UnsupportedFileException(
                'Unknown filetype!')

        else:

            raise HydrusExceptions.UnsupportedFileException(
                'Filetype is not permitted!')

    width = None
    height = None
    duration = None
    num_frames = None
    num_words = None

    if mime in HC.MIMES_THAT_DEFINITELY_HAVE_AUDIO:

        has_audio = True

    else:

        has_audio = False

    if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF, HC.IMAGE_WEBP,
                HC.IMAGE_TIFF, HC.IMAGE_ICON):

        ((width, height), duration,
         num_frames) = HydrusImageHandling.GetImageProperties(path, mime)

    elif mime == HC.APPLICATION_CLIP:

        ((width, height), duration,
         num_frames) = HydrusClipHandling.GetClipProperties(path)

    elif mime == HC.APPLICATION_FLASH:

        ((width, height), duration,
         num_frames) = HydrusFlashHandling.GetFlashProperties(path)

    elif mime == HC.IMAGE_APNG:

        ((width, height), duration, num_frames,
         has_audio) = HydrusVideoHandling.GetFFMPEGAPNGProperties(path)

    elif mime == HC.APPLICATION_PDF:

        num_words = HydrusDocumentHandling.GetPDFNumWords(
            path)  # this now give None until a better solution can be found

    elif mime == HC.APPLICATION_PSD:

        (width, height) = HydrusImageHandling.GetPSDResolution(path)

    elif mime in HC.VIDEO:

        ((width, height), duration, num_frames,
         has_audio) = HydrusVideoHandling.GetFFMPEGVideoProperties(path)

    elif mime in HC.AUDIO:

        ffmpeg_lines = HydrusVideoHandling.GetFFMPEGInfoLines(path)

        (file_duration_in_s, stream_duration_in_s
         ) = HydrusVideoHandling.ParseFFMPEGDuration(ffmpeg_lines)

        duration = int(file_duration_in_s * 1000)

    if width is not None and width < 0:

        width *= -1

    if height is not None and height < 0:

        width *= -1

    if duration is not None and duration < 0:

        duration *= -1

    if num_frames is not None and num_frames < 0:

        num_frames *= -1

    if num_words is not None and num_words < 0:

        num_words *= -1

    return (size, mime, width, height, duration, num_frames, has_audio,
            num_words)
Esempio n. 3
0
def GetFileInfo(path, mime=None, ok_to_look_for_hydrus_updates=False):

    size = os.path.getsize(path)

    if size == 0:

        raise HydrusExceptions.FileSizeException('File is of zero length!')

    if mime is None:

        mime = GetMime(
            path, ok_to_look_for_hydrus_updates=ok_to_look_for_hydrus_updates)

    if mime not in HC.ALLOWED_MIMES:

        if mime == HC.TEXT_HTML:

            raise HydrusExceptions.UnsupportedFileException(
                'Looks like HTML -- maybe the client needs to be taught how to parse this?'
            )

        elif mime == HC.APPLICATION_UNKNOWN:

            raise HydrusExceptions.UnsupportedFileException(
                'Unknown filetype!')

        else:

            raise HydrusExceptions.UnsupportedFileException(
                'Filetype is not permitted!')

    width = None
    height = None
    duration = None
    num_frames = None
    num_words = None

    if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF, HC.IMAGE_WEBP,
                HC.IMAGE_TIFF, HC.IMAGE_ICON):
        ((width, height), duration,
         num_frames) = HydrusImageHandling.GetImageProperties(path, mime)
    elif mime == HC.APPLICATION_ZIP:
        temp_dir_path = HydrusPaths.GetTempDir()
        try:
            subprocess.call(["unzip", path, '-d', temp_dir_path])
            cover = sorted(list(Path(temp_dir_path).rglob("*.jpg")) +
                           list(Path(temp_dir_path).rglob("*.png")),
                           key=lambda p: p.name)[0].as_posix()
            ((width, height), duration,
             num_frames) = HydrusImageHandling.GetImageProperties(
                 cover, HC.IMAGE_PNG)
            # TODO: delete dir
        except Exception as e:
            (width, height, duration, num_frames) = (300, 300, 0, 0)
    elif mime == HC.APPLICATION_RAR:
        temp_dir_path = HydrusPaths.GetTempDir()
        try:
            subprocess.call(["unrar", path, temp_dir_path])
            cover = sorted(list(Path(temp_dir_path).rglob("*.jpg")) +
                           list(Path(temp_dir_path).rglob("*.png")),
                           key=lambda p: p.name)[0].as_posix()
            ((width, height), duration,
             num_frames) = HydrusImageHandling.GetImageProperties(
                 cover, HC.IMAGE_PNG)
        except Exception as e:
            (width, height, duration, num_frames) = (300, 300, 0, 0)

    elif mime == HC.APPLICATION_FLASH:

        ((width, height), duration,
         num_frames) = HydrusFlashHandling.GetFlashProperties(path)

    elif mime in (HC.IMAGE_APNG, HC.VIDEO_AVI, HC.VIDEO_FLV, HC.VIDEO_WMV,
                  HC.VIDEO_MOV, HC.VIDEO_MP4, HC.VIDEO_MKV, HC.VIDEO_REALMEDIA,
                  HC.VIDEO_WEBM, HC.VIDEO_MPEG):

        ((width, height), duration,
         num_frames) = HydrusVideoHandling.GetFFMPEGVideoProperties(path)

    elif mime == HC.APPLICATION_PDF:

        num_words = HydrusDocumentHandling.GetPDFNumWords(
            path)  # this now give None until a better solution can be found

    elif mime == HC.APPLICATION_PSD:

        (width, height) = HydrusImageHandling.GetPSDResolution(path)

    elif mime in HC.AUDIO:

        ffmpeg_lines = HydrusVideoHandling.GetFFMPEGInfoLines(path)

        (file_duration_in_s, stream_duration_in_s
         ) = HydrusVideoHandling.ParseFFMPEGDuration(ffmpeg_lines)

        duration = int(file_duration_in_s * 1000)

    if mime in HC.MIMES_THAT_DEFINITELY_HAVE_AUDIO:

        has_audio = True

    elif mime in HC.MIMES_THAT_MAY_HAVE_AUDIO:

        has_audio = HydrusAudioHandling.VideoHasAudio(path)

    else:

        has_audio = False

    if width is not None and width < 0:

        width *= -1

    if height is not None and height < 0:

        width *= -1

    if duration is not None and duration < 0:

        duration *= -1

    if num_frames is not None and num_frames < 0:

        num_frames *= -1

    if num_words is not None and num_words < 0:

        num_words *= -1

    return (size, mime, width, height, duration, num_frames, has_audio,
            num_words)
Esempio n. 4
0
def GenerateNumPyImage(path, mime, force_pil=False) -> numpy.array:

    if HG.media_load_report_mode:

        HydrusData.ShowText('Loading media: ' + path)

    if not OPENCV_OK:

        force_pil = True

    if not force_pil:

        try:

            pil_image = RawOpenPILImage(path)

            try:

                pil_image.verify()

            except:

                raise HydrusExceptions.UnsupportedFileException()

            # I and F are some sort of 32-bit monochrome or whatever, doesn't seem to work in PIL well, with or without ICC
            if pil_image.mode not in ('I', 'F'):

                if pil_image.mode == 'LAB':

                    force_pil = True

                if HasICCProfile(pil_image):

                    if HG.media_load_report_mode:

                        HydrusData.ShowText(
                            'Image has ICC, so switching to PIL')

                    force_pil = True

        except HydrusExceptions.UnsupportedFileException:

            # pil had trouble, let's cross our fingers cv can do it
            pass

    if mime in PIL_ONLY_MIMETYPES or force_pil:

        if HG.media_load_report_mode:

            HydrusData.ShowText('Loading with PIL')

        pil_image = GeneratePILImage(path)

        numpy_image = GenerateNumPyImageFromPILImage(pil_image)

    else:

        if HG.media_load_report_mode:

            HydrusData.ShowText('Loading with OpenCV')

        if mime in (HC.IMAGE_JPEG, HC.IMAGE_TIFF):

            flags = CV_IMREAD_FLAGS_JPEG

        elif mime == HC.IMAGE_PNG:

            flags = CV_IMREAD_FLAGS_PNG

        else:

            flags = CV_IMREAD_FLAGS_WEIRD

        numpy_image = cv2.imread(path, flags=flags)

        if numpy_image is None:  # doesn't support some random stuff

            if HG.media_load_report_mode:

                HydrusData.ShowText('OpenCV Failed, loading with PIL')

            pil_image = GeneratePILImage(path)

            numpy_image = GenerateNumPyImageFromPILImage(pil_image)

        else:

            numpy_image = DequantizeNumPyImage(numpy_image)

    if NumPyImageHasOpaqueAlphaChannel(numpy_image):

        convert = cv2.COLOR_RGBA2RGB

        numpy_image = cv2.cvtColor(numpy_image, convert)

    return numpy_image