【问题标题】:How to open dynamically created files in django如何在 django 中打开动态创建的文件
【发布时间】:2021-06-28 15:39:23
【问题描述】:

好吧,我头疼,我创建了 2 个模型,一个用于图像,一个用于文件,每次我上传图像时,都会自动创建一个包含图像 OCR 结果的文件。 但是我的文件没有存储到文件夹中,我似乎无法打开它们。

这是我的代码,请帮助我,我一直不明白如何将文本传递给 fileField,以便它可以像正常的 fileField 上传一样处理它:

class Image(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title=models.CharField(max_length=50)
    image=models.ImageField(upload_to='media',unique=True)
    created_at = models.DateTimeField(auto_now_add=True)
    
    def save(self, *args, **kwargs):
        super(Image, self).save(*args, **kwargs)
        if self.id :
            File.objects.create(
                file=Create_Pdf(self.image))
            File.objects.update(title=self.title)
         
    def __str__(self):
        return str(self.id)

这是我的文件模型:

    from django.db.models.fields import related
    from django.db import models
    # Create your models here.


    class File(models.Model):
        label=models.CharField(max_length=50, default='none')
        title=models.CharField(max_length=200,default='test')
        file=models.FileField(upload_to='files')
        created_at = models.DateTimeField(auto_now_add=True)
        def __str__(self):
            return str(self.id)

and this is the use case :
import uuid
import os
import pytesseract
import PIL.Image
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
def Create_Pdf(image):
   filename = f"{uuid.uuid4()}.pdf"
   path=r'C:\Users\DELL\Desktop\PFE Rihab\Django j\OCRapplication\media\files'#i know this is wrong i just wanted to save the files and view them.
   image=PIL.Image.open(image)
   text=pytesseract.image_to_string(image)
   with open (os.path.join(path,filename),mode='w+')as f:
       f.write(text)
   return(text)#i m not sure i should be returning text since it just gives me the OCR result as a link

【问题讨论】:

    标签: python django python-tesseract


    【解决方案1】:

    这很正常,你需要用ContentFile包裹OCR的输出

    这里的文档:https://docs.djangoproject.com/en/3.2/ref/files/file/#the-contentfile-class

    ContentFile 类继承自 File,但与 File 不同,它操作 字符串内容(也支持字节),而不是实际文件。

    from django.core.files.base import ContentFile
    
    class Image(models.Model):
        id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
        title=models.CharField(max_length=50)
        image=models.ImageField(upload_to='media',unique=True)
        created_at = models.DateTimeField(auto_now_add=True)
        
        def save(self, *args, **kwargs):
            super(Image, self).save(*args, **kwargs)
            if self.id :
                File.objects.create(
                    file=ContentFile(Create_Pdf(self.image)),  # <--- Here
                    title=self.title
                )
             
        def __str__(self):
            return str(self.id)
    

    如果你想要我的 2cent,Image 模型中名为 resultTextField 字段更简单直接。

    【讨论】:

    • 我确实试过了,但遗憾的是它不起作用,它没有创建任何文件
    • 我真的想通了,这很简单实际上我必须为要存储的 contentFile 提供我正在创建的文件的名称大声笑,非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多