Exemple #1
0
def convFile(filepath):
    try:
        with open(filepath, 'rb') as f:
            data = f.read()
            fmt = whatimage.identify_image(data)

             

            if fmt == 'heic':
                if filepath.rfind('/') == -1:
                    pathSep = '\\'
                else:
                    pathSep = '/'

                file = filepath[filepath.rfind(pathSep)+1: filepath.rfind('.')]
                path = filepath[:filepath.rfind(pathSep)+1]

                if os.path.isfile(path + file +".jpg"):
                    return 2
                    
                heif_file = pyheif.read_heif(data)
                image = Image.frombytes(mode=heif_file.mode, size=heif_file.size, data=heif_file.data)
                image.save( path + file +".jpg", "JPEG")

                return 1
    except:
        return 0
def resize_image(filename):
    basewidth = 300

    filepath = upload_dir + filename

    with open(filepath, 'rb') as f:
        data = f.read()

    fmt = whatimage.identify_image(data)

    if fmt in ['heic', 'avif']:
        i = pyheif.read_heif(filepath)

        img = Image.frombytes(
            i.mode,
            i.size,
            i.data,
            "raw",
            i.mode,
            i.stride,
        )

    else:
        img = Image.open(filepath)

    new_size = calculate_size(img.size, base)
    img = img.resize(new_size, Image.ANTIALIAS).convert('RGB')

    img.save(website_dir + create_new_filename(filename), quality=quality)
Exemple #3
0
def decodeImage(nomfichier, repertoire, bytesIo):
    print(nomfichier)
    os.path.join(repertoire)
    fmt = whatimage.identify_image(bytesIo)
    if fmt in ['heic', 'avif']:
        i = pyheif.read_heif(bytesIo)
        pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
        pi.save(nomfichier[:-4] + 'jpg', format="jpeg")
Exemple #4
0
def checkFile(filepath):
    with open(filepath, 'rb') as f:
        data = f.read()
        fmt = whatimage.identify_image(data)
        if fmt == 'heic':
            return True
        else:
            return False
def decodeImage(bytesIo, save_name):
    try:
        fmt = whatimage.identify_image(bytesIo)
        if fmt in ['heic']:
            i = pyheif.read_heif(bytesIo)
            pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)

            pi.save(save_name, format="jpeg")
    except:
        traceback.print_exc()
def change_fmt(img_path, out_dir, to_fmt='JPEG', suffix='.jpg'):
    basename, _ = os.path.splitext(os.path.basename(img_path))
    out_path = os.path.join(out_dir, basename + suffix)

    img_file = open(img_path, 'rb').read()
    fmt = whatimage.identify_image(img_file)
    if fmt in ['heic']:
        img_file = read_image_heif(img_file)
        with open(out_path, 'wb') as fo:
            fo.write(img_file)
Exemple #7
0
def test_detect():
    for fmt in [
            'pbm', 'pgm', 'ppm', 'bmp', 'tiff', 'gif', 'png', 'jpeg', 'webp',
            'heic', 'avif'
    ]:
        for fn in glob.glob(f'tests/images/{fmt}/*'):
            print(fn)
            with open(fn, 'rb') as f:
                data = f.read()

            assert (fmt == whatimage.identify_image(data))
Exemple #8
0
def heic_to_png(img_path: str, save_path: str):
    with open(img_path, 'rb') as f:
        img_bytes = f.read()
        img_type = whatimage.identify_image(img_bytes)
        # print(f"img type: {img_type}")
        if img_type in ['heic', 'avif']:  # avif
            hei_f = pyheif.read(img_bytes)
            img = Image.frombytes(mode=hei_f.mode,
                                  size=hei_f.size,
                                  data=hei_f.data)
            img.save(save_path, format='png')
Exemple #9
0
def decodeImage(bytesIo, index, compress, sub_file):
    with open(bytesIo, 'rb') as f:
        data = f.read()
    fmt = whatimage.identify_image(data)
    if fmt in ['heic', 'avif']:
        i = pyheif.read_heif(data)
        pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
        width = pi.size[0]  # 获取宽度
        height = pi.size[1]  # 获取高度
        out = pi.resize((int(width * compress), int(height * compress)), Image.ANTIALIAS)
        out.save(source + "/../" + sub_file + "/" + "new" + str(index) + ".jpg", format="jpeg")
Exemple #10
0
 def decodeImage(bytesIo, save_path):
     try:
         fmt = whatimage.identify_image(bytesIo)
         # print('fmt = ', fmt)
         if fmt in ['heic']:
             i = pyheif.read_heif(bytesIo)
             # print('i = ', i)
             # print('i.metadata = ', i.metadata)
             pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
             # print('pi = ', pi)
             pi.save(save_path, format="jpeg")
     except:
         traceback.print_exc()
Exemple #11
0
def decode_image(input_path, output_path):
    bytesIo = open(input_path, 'rb').read()
    try:
        fmt = whatimage.identify_image(bytesIo)
        # print('fmt = ', fmt)
        if fmt in ['heic', 'avif']:
            i = pyheif.read_heif(bytesIo)
            # print('i = ', i)
            # print('i.metadata = ', i.metadata)
            pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
            # print('pi = ', pi)
            pi.save(output_path, format="jpeg")
    except:
        traceback.print_exc()
Exemple #12
0
def decodeHEICImage(bytesIo):
    fmt = whatimage.identify_image(bytesIo.read())
    if fmt in ['heic', 'avif']:
        i = pyheif.read_heif(bytesIo.read())
        # Extract metadata etc

        #for metadata in i.metadata or []:
        #    if metadata['type']=='Exif':
        #        # do whatever

        # Convert to other file format like jpeg
        s = BytesIO()
        pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
        pi.save(s, format="jpeg")
        return s
Exemple #13
0
def heic_to_jpg(file_path):
    # read_image_file_rb
    with open(file_path, 'rb') as f:
        bytesIo = f.read()
        try:

            fmt = whatimage.identify_image(bytesIo)
            print('fmt = ', fmt)
            if fmt in ['heic']:
                i = pyheif.read_heif(bytesIo)
                # print('i = ', i)
                # print('i.metadata = ', i.metadata)
                pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
                # print('pi = ', pi)
                pi.save('heeh.jpg', format="jpeg")
        except:
            traceback.print_exc()
Exemple #14
0
def downloadImage(imageList,
                  image_dir=settings.IMAGE_DIR,
                  threshold=800,
                  modify=False,
                  cookie=''):
    res = []
    header.set_cookie(cookie)
    for image in imageList:
        if isinstance(image, dict):
            image_url = image.get('img', 'no')
        else:
            image_url = image
        if not image_url:
            continue
        try:
            response = requests.get(image_url, headers=header.get_header())
            img = response.content
        except Exception as e:
            log.error(e)
            image['img'] = ''
            continue
        ext = os.path.splitext(image_url)[-1]
        if not ext:
            ext = whatimage.identify_image(img)
        if not ext:
            image['img'] = ''
            continue
        if ext[0] != '.':
            ext = '.' + ext
        image_name = str(hash(image_url)) + ext
        image_path = os.path.join(image_dir, image_name)
        with open(image_path, 'wb') as f:
            f.write(img)
        log.info("下载成功" + image_path)
        res.append(image_path)
        if modify:
            image['img'] = image_path
    return res
Exemple #15
0
def load_image(path):
    """
    Safe method for loading PIL.Image instances with additional support for HEIF files.
    """

    # Check to see if we need to do special-case HEIF handling.
    # If we do, then we convert the file to an in-memory JPEG, that can then be opened using PIL.
    with open(path, 'rb') as fh:
        image_data = fh.read()
        format = whatimage.identify_image(image_data)
        if format in ['heic', 'avif']:
            heif_image = pyheif.read_heif(image_data)
            pi = Image.frombytes(mode=heif_image.mode,
                                 size=heif_image.size,
                                 data=heif_image.data)
            exif = None
            for metadata in heif_image.metadata or []:
                if metadata['type'] == 'Exif':
                    exif = metadata['data']
            stream = io.BytesIO()
            pi.save(stream, format="jpeg", exif=exif)
            return Image.open(stream)

    return Image.open(path)
Exemple #16
0
def rename_media_files(data_path: Path):
    if not data_path.is_dir():
        return

    for i, f in enumerate(data_path.iterdir()):
        if f.name[0] == '.':
            continue
        if f.is_dir():
            continue

        what = whatimage.identify_image(f.open('rb').read())
        if what:
            exif_data = exifread.process_file(f.open('rb'))
            idf_img_dt: exifread.classes.IfdTag = exif_data.get(
                'Image DateTime', None)
            if idf_img_dt:
                s_date, s_time = idf_img_dt.values.split(' ')
                s_date = ''.join(s_date.split(':'))
                if s_date in f.name:
                    continue
                s_time = ''.join(s_time.split(':'))
                img_make = exif_data.get('Image Make', '')
                make_code = MAKE_CODES.get(str(img_make),
                                           str(img_make) or 'NA')
                s_f_name = ''.join([
                    s_date,
                    '_',
                    s_time,
                    '000',
                    '_',
                    make_code,
                    f.suffix,
                ])
                new_path = data_path / s_f_name
                f.rename(new_path)
        else:
            try:
                parser = createParser(str(f.absolute()))
            except input.NullStreamError:
                parser = None

            if not parser:
                # print("Unable to parse file", file=stderr)
                # Try get_mov_timestamps
                try:
                    created, modified = get_mov_timestamps(f)
                except Exception as e:
                    # Try TinyTag / Implementation to be done
                    try:
                        print(what, f.name)
                        media = TinyTag.get(f)
                        pprint(media)
                    except TinyTagException:
                        continue
                    continue
                created: DateTime
                s_date = created.strftime('%Y%m%d')
                if s_date in f.name:
                    continue
                s_f_name = ''.join([
                    created.strftime('%Y%m%d_%H%M%S000_mov'),
                    f.suffix,
                ])
                new_path = data_path / s_f_name
                f.rename(new_path)
                continue

            with parser:
                try:
                    metadata: Metadata = extractMetadata(parser)
                except Exception as err:
                    # print("Metadata extraction error: %s" % err, file=stderr)
                    metadata = None

            if not metadata:
                # print("Unable to extract metadata", file=stderr)
                continue

            d_meta = metadata.exportDictionary(human=False)
            for k in ['Common', 'Metadata']:
                created = ''
                producer = ''
                try:
                    created: str = d_meta[k]['creation_date']
                except KeyError:
                    # print(f'Key not found: {k}', file=stderr)
                    continue
                try:
                    producer: str = d_meta[k]['producer'] or 'mov'
                except KeyError:
                    producer = 'mov'

                if bool(created) and bool(producer):
                    break

            s_date, s_time = created.split(' ')
            s_date: str = ''.join(s_date.split('-'))
            if s_date in f.name:
                continue
            s_time: str = ''.join(s_time.split(':'))
            s_f_name = ''.join([
                s_date,
                '_',
                s_time,
                '000',
                '_',
                producer,
                f.suffix,
            ])
            new_path = data_path / s_f_name
            f.rename(new_path)
            continue
    def image2text(self, image, data_type):
        '''
        Extract information from ID card images
        '''
        # Total processing time
        start_time = time.time()

        # Image's name with time-stamp
        try:
            if data_type == "url":
                print('image: ', image)
                filename = str(
                    datetime.datetime.now().isoformat()) + '_' + 'url.jpg'
                try:
                    with time_limit(5):
                        r = requests.get(image, allow_redirects=True)
                except TimeoutException as e:
                    logging.error("Downloading file timed out!")
                    self.result['error'] = 'Downloading file timed out'
                    return self.result
                except Exception as e:
                    logging.error('Exception: %s', e)
                    self.result['error'] = 'Failed to open the URL!'
                    return self.result
            elif data_type == "image":
                # Support receive pdf file
                if image.filename.split('.')[-1] == 'pdf':
                    # Convert pdf to image
                    img_pdf = convert_from_bytes(image.file.read())[0]
                    filename = str(
                        datetime.datetime.now().isoformat()) + '_' + ''.join(
                            image.filename.split('.')[:-1]) + '.jpg'
                else:
                    filename = str(datetime.datetime.now().isoformat()
                                   ) + '_' + image.filename
            elif data_type == "masked":
                filename = str(
                    datetime.datetime.now().isoformat()) + '_masked' + '.jpg'
            else:
                filename = str(
                    datetime.datetime.now().isoformat()) + '_base64.jpg'
        except:
            logging.error(traceback.format_exc())
            self.result['error'] = 'Bad data'
            return self.result

        # Check if upload path exits
        path_original = os.path.join(UPLOADED_DIR,
                                     datetime.date.today().isoformat())
        if not os.path.exists(path_original):
            os.makedirs(path_original, exist_ok=True)
        # Path to the original image
        self.upload_path = os.path.join(path_original, filename)
        print(self.upload_path)

        # Save original image
        try:
            if data_type == "masked":
                cv2.imwrite(self.upload_path, image)
            else:
                with open(self.upload_path, "wb") as f:
                    if data_type == "url":
                        f.write(r.content)
                    elif data_type == "image":
                        if image.filename.split('.')[-1] == 'pdf':
                            img_pdf.save(f)
                        else:
                            f.write(image.file.read())
                    else:
                        imgdata = base64.b64decode(image)
                        f.write(imgdata)
        except:
            logging.error(traceback.format_exc())

        # Verify that the uploaded image is valid
        try:
            if imghdr.what(self.upload_path) is None:
                with open(self.upload_path, 'rb') as f:
                    data = f.read()
                image_type = whatimage.identify_image(data)
                if image_type is not None and image_type != 'heic':
                    pass
                elif image_type == 'heic' or image_type == 'HEIC':
                    destination = self.upload_path + '.jpg'
                    subprocess.call([
                        WORKING_DIR + '/src/libs/tifig/tifig', '-p',
                        self.upload_path, destination
                    ])
                    self.upload_path = destination
                else:  # if not, terminate the process
                    logging.info(
                        '{} is not a valid image file'.format(filename))
                    self.result['error'] = 'Invalid image file'
                    return self.result
        except IOError:
            logging.error(traceback.format_exc())
            logging.error('Cannot open {}'.format(self.upload_path))
            return self.result

        # If the uploaded image is valid, keep going
        if filename.split('.')[-1].lower() not in [
                'jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif', 'ppg', 'pgm'
        ]:
            filename = filename + '.jpg'
        try:
            img_raw = cv2.imread(self.upload_path)
            img_rgb = cv2.cvtColor(img_raw, cv2.COLOR_BGR2RGB)
            img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
        except Exception as e:
            print(e)
            print('{} is not a valid image file'.format(filename))
            self.result['error'] = 'Invalid image file'
            return self.result

        if 'error' in self.result:
            logging.info(username + ' Image ' + filename + ': ' +
                         self.result['error'])

        return self.result