UnidentifiedImageError: cannot identify image file (Python, .heic to jpg converter)

1.1k Views Asked by At

The error is:

name='IMG_0001.HEIC', type='application/octet-stream', size=1318226) Traceback: File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 565, in _run_script exec(code, module.__dict__) File "C:\Users\Lenovo\Desktop\heic_to_img\bulk.py", line 77, in <module> abc = heic_to_img_converter() File "C:\Users\Lenovo\Desktop\heic_to_img\bulk.py", line 15, in __init__ self.upload_file() File "C:\Users\Lenovo\Desktop\heic_to_img\bulk.py", line 37, in upload_file self.convert_file() File "C:\Users\Lenovo\Desktop\heic_to_img\bulk.py", line 59, in convert_file with Image.open(uploaded_file) as im: File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 3298, in open raise UnidentifiedImageError(msg) converter. But the program giving

My Code:

`import streamlit as st import os from PIL import Image

class heic_to_img_converter():

def __init__(self) -> None:
    
    self.uploaded_files = ""
    
    self.bytes_data = ""
    
    self.upload_file()
    

def new_folder(self):
    pass

        
def upload_file(self):
    
    self.uploaded_files = st.file_uploader("Choose a file.", accept_multiple_files= True)
        
    if self.uploaded_files:
        
        if not os.path.exists('converted_images'):
            os.makedirs('converted_images')
        
        st.write("You have selected files:")
        for uploaded_file in self.uploaded_files:
            self.bytes_data = uploaded_file.read()    
            st.write("filename:", uploaded_file.name)
        
        if st.button("Convert Images HEIC to JPG"):
            self.convert_file()
        
    else:
        st.write("Please select a file.") 
            
        
def convert_file(self): 
    
    for uploaded_file in self.uploaded_files:
        
        self.file_name = uploaded_file.name.lower()
        
        print(self.file_name)
        
        
        
        if self.file_name.endswith('.heic'):
            
            print(self.file_name)
            
          
            
            with Image.open(uploaded_file) as im:
                
                with Image.open(uploaded_file) as im:
                    
                    self.new_file_name = os.path.basename(self.file_name).replace('.heic','.jpg')
                    
                    self.file_path = os.path.abspath(os.path.join('converted_images', self.new_file_name))
                    
                    im.save(os.path.join(self.file_path, self.new_file_name))
                
                im.save(os.path.join(self.file_path, self.new_file_name))
                
    
    st.write("All job is done!")

abc = heic_to_img_converter()`

Hi. I've writed a program to convert heic images to jpg type. But the program is giving an error. I've tried pillow plugins and pyheif, but I couldn't setup these packages.

Please help.

1

There are 1 best solutions below

0
Baris On

I solved the problem. I'm happy now.

You can reach my project on github: https://github.com/hasanbarisgok/heic_to_jpg

I've used the pillow_heif import register_heif_opener library extra.

You have to add this command, if you won't you will have an error again.

register_heif_opener()

    def __init__(self) -> None:
        register_heif_opener()
        
        self.uploaded_files = ""
        
        self.bytes_data = ""
        
        self.upload_file()

 
def convert_file(self):
            
        for uploaded_file in self.uploaded_files:
            self.file_name = uploaded_file.name.lower()
            

            if self.file_name.endswith('.heic'):
                

                with Image.open(uploaded_file) as heif_file:
                    
                    self.new_file_name = os.path.basename(self.file_name).replace('.heic','.jpg')
                    
                    self.new_file_path = os.path.abspath(os.path.join('converted_images', self.new_file_name))
                    heif_file.save(self.new_file_path, format='JPEG')
        
        st.write("All job is done!")`