未识别图像错误: 无法识别图像文件(Python,.heic 转 jpg 转换器)

huangapple go评论65阅读模式
英文:

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

问题

The error message you provided appears to be related to a Python script that converts HEIC images to JPG format using the Pillow library. If you have specific questions or need assistance with this code, please let me know, and I'll do my best to help.

英文:

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

得分: 2

我解决了这个问题。我现在很开心。

你可以在我的GitHub项目中找到我的项目: https://github.com/hasanbarisgok/heic_to_jpg

我使用了pillow_heif import register_heif_opener库的额外部分。

如果你不加这个命令,会再次出现错误。

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!")`
英文:

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!")`

huangapple
  • 本文由 发表于 2023年4月17日 02:36:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定