Example #1
0
def store_file(uploaded_file):

    filename = secure_filename(uploaded_file.filename)
    directory = folder_path()
    if not os.path.exists(directory):
        os.makedirs(directory)
    file_bytes = BytesIO(uploaded_file.read())
    if os.path.splitext(filename)[1] == ".HEIC":
        heif_file = pyheif.read(file_bytes)
        image = Image.frombytes(
            heif_file.mode,
            heif_file.size,
            heif_file.data,
            "raw",
            heif_file.mode,
            heif_file.stride,
        )
    else:
        image = Image.open(file_bytes)
    newfilename = f"{uuid.uuid1()}.jpg"
    image.convert("RGB").save(file_path(newfilename),
                              format="JPEG",
                              optimize=True,
                              quality=10)
    return newfilename
Example #2
0
def _process_file(
    fh: BinaryIO,
    stop_tag: str = exifread.DEFAULT_STOP_TAG,
    details: bool = True,
    strict: bool = False,
    debug: bool = False,
    truncate_tags: bool = True,
):
    """
    ExifRead claims to handle HEIC (HEIF) images, but it can't handle mine. This is a
    wrapper that intercepts HEIC images and uses pyheif to extract the exif data, but
    otherwise hands over directly to ExifRead.
    """
    header = fh.read(12)
    if header[4:12] == b"ftypheic":
        fh.seek(0)
        heif_file = pyheif.read(fh)
        exif_data = next(item["data"] for item in heif_file.metadata
                         if item["type"] == "Exif")
        fh = io.BytesIO(exif_data[len(b"Exif\x00\x00"):])
    return exifread.process_file(
        fh,
        stop_tag=stop_tag,
        details=details,
        strict=strict,
        debug=debug,
        truncate_tags=truncate_tags,
    )
    def _open(self):
        data = self.fp.read(16)

        if not check_heif_magic(data):
            raise SyntaxError('not a HEIF file')

        self.fp.seek(0)
        try:
            heif_file = pyheif.read(self.fp)
        except HeifError as e:
            raise SyntaxError(str(e))

        # size in pixels (width, height)
        self._size = heif_file.size

        # mode setting
        self.mode = heif_file.mode

        # Add Exif
        if heif_file.metadata:
            for data in heif_file.metadata:
                if data['type'] == 'Exif':
                    self.info['exif'] = data['data']
                    break

        offset = self.fp.tell()
        self.tile = [('heif', (0, 0) + self.size, offset, (heif_file, ))]
Example #4
0
    def __check_heif_then_open(self, fname):
        ext = os.path.splitext(fname)[1].lower()
        if ext in ('.heif', '.heic'):
            try:
                import pyheif

                heif_file = pyheif.read(fname)
                image = Image.frombytes(heif_file.mode, heif_file.size,
                                        heif_file.data, "raw", heif_file.mode,
                                        heif_file.stride)
                if image.mode not in ("RGB", "RGBA"):
                    image = image.convert("RGB")
                return image
            except:
                self.__logger.warning(
                    "Failed attempt to convert %s \n** Have you installed pyheif? **",
                    fname)
        else:
            try:
                image = Image.open(fname)
                if image.mode not in ("RGB",
                                      "RGBA"):  # mat system needs RGB or more
                    image = image.convert("RGB")
            except:  # for whatever reason
                image = None
            return image
Example #5
0
 def _fill_image_from_data(self):
     fh = io.BytesIO(self._data)
     if self.content_type == 'image/heic':
         try:
             heif_file = pyheif.read(fh)
         except (ValueError, pyheif.error.HeifError) as err:
             raise PostException(
                 f'Unable to read HEIC file for post `{self.post_id}`: {err}'
             ) from err
         self._image = PIL.Image.frombytes(heif_file.mode, heif_file.size,
                                           heif_file.data, 'raw',
                                           heif_file.mode, heif_file.stride)
     elif self.content_type == 'image/jpeg':
         file_type = imghdr.what(fh)
         if file_type is None:
             raise PostException(
                 f'Unable to recognize file type of uploaded file for post `{self.post_id}`'
             )
         if file_type != 'jpeg' and file_type != 'png':
             raise PostException(
                 f'File of type `{file_type}` for uploaded jpeg image post `{self.post_id}`'
             )
         try:
             self._image = PIL.ImageOps.exif_transpose(PIL.Image.open(fh))
         except Exception as err:
             raise PostException(
                 f'Unable to decode jpeg data for post `{self.post_id}`: {err}'
             ) from err
     else:
         raise PostException(
             f'Unrecognized content-type `{self.content_type}`')
Example #6
0
def converter(src_format, source, target, cache):

    source = os.getcwd() + '/{}/'.format(source)
    target = os.getcwd() + '/{}/'.format(target)

    _, _, filenames = next(walk(source))

    if cache is True:
        filenames = compute_files_to_convert(target, filenames)

    with click.progressbar(filenames,
                       label='Converting photos',
                       length=len(filenames)) as photos:

        for f in photos:
            if src_format in f:
                heif_file = pyheif.read(source + f)
                image = Image.frombytes(
                    heif_file.mode, 
                    heif_file.size, 
                    heif_file.data,
                    "raw",
                    heif_file.mode,
                    heif_file.stride,
                    )

                image.save(target + f.split('.')[0] + '.jpg', "JPEG")
def convert_heic_to_jpg(image_path):
    """tries to convert the heic image to jpg with pyheif and pillow (does not works on windows)"""
    try:
        # pyheif not works on windows so we try to import it
        import pyheif

        # create new path name
        file_name, _ = os.path.splitext(image_path)
        new_path = file_name + ".jpg"

        # convert and save the image
        heif_file = pyheif.read(image_path)
        image = Image.frombytes(
            heif_file.mode,
            heif_file.size,
            heif_file.data,
            "raw",
            heif_file.mode,
            heif_file.stride,
        )

        image.save(new_path, "JPEG")

        # remove old extension image
        os.remove(image_path)
        return 1, new_path
    except Exception as e:
        print(e)
        return 0, image_path
Example #8
0
def load_image(bs):
    ImageFile.LOAD_TRUNCATED_IMAGES = True
    try:
        image = Image.open(io.BytesIO(bs))
    except UnidentifiedImageError as err1:
        if USE_PYHEIF:
            try:
                heif = pyheif.read(bs)
            except ValueError as err2:
                raise err1 from err2
            image = Image.frombytes(heif.mode, heif.size, heif.data, "raw",
                                    heif.mode, heif.stride)
        else:
            raise

    if image.mode in ('P', 'RGBA'):
        image = image.convert('RGBA')
        background = Image.new('RGBA', image.size, (255, 255, 255))
        image = Image.alpha_composite(background, image)

    image = image.convert('RGB')

    array = np.asarray(image)
    if len(array.shape) == 2:
        array = array[:, :, None]
    if array.shape[2] == 1:
        array = np.concatenate([array] * 3, axis=2)
    return array
Example #9
0
def convert_to_jpeg(user, my_dir):
    way = my_dir
    extension = os.path.splitext(my_dir)[-1].lower()
    if extension == ".heic":
        heif_file = pyheif.read(way)
        image = Image.frombytes(
            heif_file.mode,
            heif_file.size,
            heif_file.data,
            "raw",
            heif_file.mode,
            heif_file.stride,
        )
        os.remove(way)
        image.save("data/" + user + "_" + str(datetime.now()) + '.jpg', "JPEG")
        return "Готово"
    else:
        try:
            im = Image.open(way)
            rgb_im = im.convert('RGB')
            rgb_im.save("data/" + user + "_" + str(datetime.now()) + '.jpg')
            os.remove(way)
            return "Готово"
        except:
            return "Ты точно отправил фото?"
Example #10
0
def convert_image(chat_id, input_type, output_type):
    """
	The function converts image of one type to another.
	Args:
		chat_id: unique identification for image
		input_type: video input type
		output_type: video output type
	"""
    if (input_type == "heif"):
        heif_file = pyheif.read('./input_media/{}.{}'.format(
            chat_id, input_type))
        img = Image.frombytes(heif_file.mode, heif_file.size, heif_file.data,
                              "raw", heif_file.mode, heif_file.stride)
    else:
        img = Image.open('./input_media/{}.{}'.format(chat_id, input_type))
    if output_type == "jpg" or ((input_type == "tiff" or input_type == "png")
                                and output_type == "pdf"):
        img = img.convert('RGB')
    elif output_type == "ico":
        icon_size = [(32, 32)]
        img.save('./output_media/{}.{}'.format(chat_id, output_type),
                 sizes=icon_size)
        return None
    img.save('./output_media/{}.{}'.format(chat_id, output_type),
             quality=95,
             optimize=True)
    return None
Example #11
0
def test_read_paths():
    for fn in Path().glob("tests/images/**/*.heic"):
        heif_file = pyheif.read(fn)
        assert heif_file is not None
        width, height = heif_file.size
        assert width > 0
        assert height > 0
        assert len(heif_file.data) > 0
Example #12
0
def convert_heif(fname):
  try:
    import pyheif
    from PIL import Image
    heif_file = pyheif.read(fname)
    image = Image.frombytes(heif_file.mode, heif_file.size, heif_file.data, "raw", heif_file.mode, heif_file.stride)
    return image
  except:
    logging.warning("Could't convert HEIF. Have you installed pyheif?")
Example #13
0
def test_read_icc_color_profile():
    for fn in Path().glob("tests/images/**/*.heic"):
        heif_file = pyheif.read(fn)
        if heif_file.color_profile and heif_file.color_profile["type"] in [
                "prof",
                "rICC",
        ]:
            profile = io.BytesIO(heif_file.color_profile["data"])
            cms = ImageCms.getOpenProfile(profile)
Example #14
0
def test_read_file_objects():
    for fn in Path().glob("tests/images/**/*.heic"):
        with open(fn, "rb") as f:
            heif_file = pyheif.read(f)
            assert heif_file is not None
            width, height = heif_file.size
            assert width > 0
            assert height > 0
            assert len(heif_file.data) > 0
Example #15
0
def test_read_bytes():
    for fn in glob("tests/images/**/*.heic"):
        with open(fn, "rb") as f:
            d = f.read()
            heif_file = pyheif.read(d)
            assert heif_file is not None
            width, height = heif_file.size
            assert width > 0
            assert height > 0
            assert len(heif_file.data) > 0
Example #16
0
def test_read_exif_metadata():
    for fn in Path().glob("tests/images/**/*.heic"):
        heif_file = pyheif.read(fn)
        for m in heif_file.metadata or []:
            if m["type"] == "Exif":
                exif_dict = piexif.load(m["data"])
                assert "0th" in exif_dict
                assert len(exif_dict["0th"]) > 0
                assert "Exif" in exif_dict
                assert len(exif_dict["Exif"]) > 0
Example #17
0
def get_fileInfo_from_image(fullname):
    fullname_el = fullname.split(".")
    if fullname_el[-1].lower() == "heic" :
        # Open the file
        import pyheif
        import piexif
        heif_file = pyheif.read(fullname)
    
        # Retrive the metadata
        for metadata in heif_file.metadata or []:
            if metadata['type'] == 'Exif':
                exif_dict = piexif.load(metadata['data'])
        image_datetime = exif_dict['0th'][306].decode('utf-8').replace(':','')
        image_datetime = image_datetime.replace(' ','-')
        camera_company = exif_dict['0th'][271].decode('utf-8')
        camera_model = exif_dict['0th'][272].decode('utf-8').replace(' ','-')
        image_Software = "-"
    
    elif fullname_el[-1].lower() == "cr2" or fullname_el[-1].lower() == "jpg":
        import exifread
        f = open(fullname, 'rb')
        tags = exifread.process_file(f)
        f.close()
        ##########################################################
        if 'EXIF DateTimeOriginal' in tags :
            image_datetime = tags['EXIF DateTimeOriginal'].values.replace(':','')
            image_datetime = image_datetime.replace(' ','-')
            
        elif 'Image DateTime' in tags :
            image_datetime = tags['Image DateTime'].values.replace(':','')
            image_datetime = image_datetime.replace(' ','-')
        else : 
            image_datetime = "00000000-000000"
        ##########################################################
        
        
        
        if 'Image Make' in tags and 'Image Model' in tags : 
            camera_model = tags['Image Make'].printable.replace(' ','')\
                +tags['Image Model'].printable.replace(' ','')
        elif 'MakerNote ModelID' in tags : 
            camera_model = tags['MakerNote ModelID'].printable.replace(' ','')        
        else : 
            camera_model = '-'
        ##########################################################    
        if 'Image Software' in tags : 
            image_Software = tags['Image Software'].printable.replace(' ','')
        else : 
            image_Software = '-'
        if 'ACDSystems' in image_Software : 
            image_Software = 'ACDSystems'

        image_Software = image_Software.replace('DigitalPhotoProfessional', 'DPP')
        
    return image_datetime, camera_company, camera_model, image_Software
Example #18
0
def test_read_10_bit():
    for fn in glob("tests/images/**/*.HIF"):
        heif_file = pyheif.read(fn)
        image = Image.frombytes(
            heif_file.mode,
            heif_file.size,
            heif_file.data,
            "raw",
            heif_file.mode,
            heif_file.stride,
        )
Example #19
0
def cnvrt_heic_to_img(image_path, save_path, fmt=None):
    heif_file = pyheif.read(image_path)
    data = Image.frombytes(
        heif_file.mode,
        heif_file.size,
        heif_file.data,
        "raw",
        heif_file.mode,
        heif_file.stride,
    )
    data.save(save_path, fmt=None)
Example #20
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')
Example #21
0
def test_read_pillow_frombytes():
    for fn in Path().glob("tests/images/**/*.heic"):
        heif_file = pyheif.read(fn)
        image = Image.frombytes(
            heif_file.mode,
            heif_file.size,
            heif_file.data,
            "raw",
            heif_file.mode,
            heif_file.stride,
        )
Example #22
0
def convert_heif(fname):
    try:
        import pyheif
        from PIL import Image

        heif_file = pyheif.read(fname)
        image = Image.frombytes(heif_file.mode, heif_file.size, heif_file.data,
                                "raw", heif_file.mode, heif_file.stride)
        return image
    except:
        print("have you installed pyheif?")
Example #23
0
def read_heic_img(path):
    heif_file = pyheif.read(path)
    image = Image.frombytes(
        heif_file.mode,
        heif_file.size,
        heif_file.data,
        "raw",
        heif_file.mode,
        heif_file.stride,
    )
    return image
Example #24
0
def read_image_heif(img_file, to_fmt='JPEG'):
    heif_file = pyheif.read(img_file)
    image = Image.frombytes(heif_file.mode,
                            heif_file.size,
                            heif_file.data,
                            "raw",
                            heif_file.mode,
                            heif_file.stride,
                            )
    with BytesIO() as out:
        image.save(out, format=to_fmt)
        img_file = out.getvalue()
    return img_file
Example #25
0
def create_image_file(image):
    """
    Take an image in BASE64 format and return a NamedTemporaryFile containing the image.
    Will handle PNG, JPEG and GIF without any changes, as FPDF will handle those files
    without problem. For PDFs we use pdf2image to convert each page to an image. For HEIC
    pictures we use pyheif to convert it to a jpeg.
    """

    if not "image/" in image and not "application/pdf" in image:
        raise UnsupportedFileException(image[:30])
    parts = image.split(";base64,")
    decoded = base64.b64decode(parts[1])
    suffix = "pdf" if "application/pdf" in image else parts[0].split("image/")[1]
    suffix = suffix.lower()
    f = tempfile.NamedTemporaryFile(suffix=f".{suffix}")
    f.write(decoded)
    f.flush()

    """
    FPDF does not support pdf files as input, therefore convert file:pdf to array[image:jpg]
    """
    if suffix == "pdf":
        files = []
        pil_images = convert_from_path(f.name, fmt="jpeg")
        for img in pil_images:
            f = tempfile.NamedTemporaryFile(suffix=f".{suffix}")
            f.write(image_to_byte_array(img))
            files.append({"file": f, "type": "jpeg"})
            f.flush()
        return files

    """
    FPDF does not support heic files as input, therefore we covert a image:heic image:jpg
    """
    if suffix == "heic":
        fmt = "JPEG"
        heif_file = pyheif.read(f.name)
        img = Image.frombytes(
            heif_file.mode,
            heif_file.size,
            heif_file.data,
            "raw",
            heif_file.mode,
            heif_file.stride,
        )
        f = tempfile.NamedTemporaryFile(suffix=f".{fmt}")
        f.write(image_to_byte_array(img, fmt))
        f.flush()
        return [{"file": f, "type": fmt}]

    return [{"file": f, "type": suffix.upper()}]
Example #26
0
def conv(image_path):
    split_path = image_path.split(".")
    split_path[-1] = split_path[-1].replace("heic", "png")
    new_name = ".".join(split_path)
    heif_file = pyheif.read(image_path)
    data = Image.frombytes(
        heif_file.mode,
        heif_file.size,
        heif_file.data,
        "raw",
        heif_file.mode,
        heif_file.stride,
    )
    data.save(new_name, "PNG")
Example #27
0
def get_fileInfo_from_heifexif(fullname):
    # Open the file
    import pyheif
    import piexif
    heif_file = pyheif.read(fullname)

    # Retrive the metadata
    for metadata in heif_file.metadata or []:
        if metadata['type'] == 'Exif':
            exif_dict = piexif.load(metadata['data'])
    image_datetime = exif_dict['0th'][306].decode('utf-8').replace(':','')
    image_datetime = image_datetime.replace(' ','-')
    camera_company = exif_dict['0th'][271].decode('utf-8')
    camera_model = exif_dict['0th'][272].decode('utf-8').replace(' ','-')
    image_Software = "-"
    return image_datetime, camera_company, camera_model, image_Software
Example #28
0
def export_heic_to_jpg(file_destination):
    heic_ext = [".HEIC", ".heic"]
    all_folders = os.listdir(file_destination)
    for subfolder in all_folders:
        whole_path = os.path.join(file_destination, subfolder)
        all_files = os.listdir(whole_path)
        for i in all_files:
            file_path = os.path.join(whole_path, i)
            file_name, file_extension = os.path.splitext(i)
            if file_extension in heic_ext:
                print("Exporting {} to jpg".format(file_path))
                new_name = file_name + "_EXPORTED_BY_SCRIPT"
                heif_file = pyheif.read(file_path)
                image = Image.frombytes(heif_file.mode, heif_file.size, heif_file.data, "raw", heif_file.mode, heif_file.stride)
                out_path_new = os.path.join(whole_path, new_name)
                image.save(out_path_new + ".jpg", "JPEG")
Example #29
0
def image_to_pil(file_storage):
    splited_mimetype = file_storage.mimetype.split('/')
    if not splited_mimetype[0] == 'image':
        return

    if splited_mimetype[1] == 'heic':
        heif_file = pyheif.read(file_storage)
        image = Image.frombytes(
            heif_file.mode, 
            heif_file.size, 
            heif_file.data,
            "raw",
            heif_file.mode,
            heif_file.stride,
            )
        return image
    return Image.open(file_storage)
Example #30
0
def heif_to_jpg(fname):
    try:
        import pyheif
        from PIL import Image

        heif_file = pyheif.read(fname)
        image = Image.frombytes(heif_file.mode, heif_file.size, heif_file.data,
                                "raw", heif_file.mode, heif_file.stride)
        if image.mode not in ("RGB", "RGBA"):
            image = image.convert("RGB")
        image.save("/dev/shm/temp.jpg")  # default 75% quality
        return "/dev/shm/temp.jpg"
    except:
        self.__logger.warning(
            "Failed attempt to convert %s \n** Have you installed pyheif? **",
            fname)
        return ""  # this will not render as a page and will generate error TODO serve specific page with explicit error