简体   繁体   中英

Imagefont truetype cannot open resource

Cannot open. ttf file from media folder But it's work Localserver But now the project hosting to pythonanywhere


class SmartRCBack(models.Model):
    reg_number = models.CharField(max_length=10,primary_key=True)
    month_year = models.CharField(max_length=20)
    type = models.CharField(max_length=20)
    wheelbase = models.CharField(max_length=20)
    cubic = models.CharField(max_length=20)
    cylinder = models.CharField(max_length=20)
    ledan_unledan = models.CharField(max_length=50)
    maker = models.CharField(max_length=50)
    model = models.CharField(max_length=50)
    color = models.CharField(max_length=50)
    body_type = models.CharField(max_length=50)
    seating = models.CharField(max_length=6)
    rto = models.CharField(max_length=50)
    finance = models.CharField(max_length=50,blank=True,null=True)

    now = models.DateField(auto_now_add=True,auto_created=True)
    image = models.ImageField(upload_to='SmartRc/',blank=True, null=True)
    class Meta:
        ordering = ('-now',)
    def save(self, *args, **kwargs):
        img = Image.open('media/back.png' )
        # fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
        d = ImageDraw.Draw(img)
        bold = ImageFont.truetype("media/arial-BoldMT.ttf",size=15,)
        font = ImageFont.truetype("media/Arial-BoldMT.ttf",size=16,)
        d.text((226,48), self.type, fill=(14, 15, 15),font=bold, stroke_fill="black")
        d.text((40,93), self.reg_number, fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((40,134), self.month_year, fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((40,174), self.wheelbase, fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((40,213), self.cubic, fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((40,253), self.cylinder, fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((40,315), self.ledan_unledan, fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((196,93), self.maker, fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((196,134), self.model, fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((196,174), self.color, fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((196,213), self.body_type, fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((196,253), self.seating, fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((196,298), "LIFE TIME", fill=(14, 15, 15),font=font, stroke_fill="black")
        d.text((451,399), self.rto, fill=(14, 15, 15),font=font, stroke_fill="black")
        if self.finance:
            textwrapped = textwrap.wrap(self.finance, width=20)
            d.text((455,259), '\n'.join(textwrapped), fill=(14, 15, 15),font=font, stroke_fill="black")
        img.save(f'{self.reg_number} back.png')
        destination_file = open(f'{self.reg_number} back.png', 'rb')
        self.image.save(f'{self.reg_number} back.png', File(destination_file), save=False)

        super(SmartRCBack, self).save(*args, **kwargs)


    def __str__(self):
        return f'{self.rto} {self.reg_number}'

I got a error this line so i try 3 ways to solve the problem This 3 ways

font = ImageFont.truetype("arial-BoldMT.ttf", size =16) 
font = ImageFont.truetype("fullpath of the file/arial-BoldMT.ttf", size =16) 
font = ImageFont.truetype("settings.MEDIA_ROOT/arial-BoldMT.ttf", size =16) 
STATIC_URL = "/static/"
MEDIA_URL = "/media/"
MEDIA_ROOT = "/home/Amar2022/rto/media"
STATIC_ROOT = "/home/Amar2022/rto/static"
urlpatterns = [
    path('admin/', admin.site.urls),
    path('rc/',include('rc.urls'))
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

pythonanywhere web config path image

Error

Environment:


Request Method: POST
Request URL: https://shinewithanvika2022.pythonanywhere.com/admin/banner/smartrcback/add/

Django Version: 4.0.4
Python Version: 3.9.5
Installed Applications:
['jazzmin',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'rest_framework.authtoken',
 'rest_framework_simplejwt',
 'mocktest',
 'student',
 'widget_tweaks',
 'banner']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/contrib/admin/options.py", line 683, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 242, in inner
    return view(request, *args, **kwargs)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/contrib/admin/options.py", line 1885, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/utils/decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/utils/decorators.py", line 133, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/contrib/admin/options.py", line 1745, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/contrib/admin/options.py", line 1797, in _changeform_view
    self.save_model(request, new_object, form, not add)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/django/contrib/admin/options.py", line 1220, in save_model
    obj.save()
  File "/home/shinewithanvika2022/examproject/banner/models.py", line 129, in save
    bold = ImageFont.truetype("arial-BoldMT.ttf",size=15,)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/PIL/ImageFont.py", line 878, in truetype
    return freetype(font)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/PIL/ImageFont.py", line 875, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/home/shinewithanvika2022/env/lib/python3.9/site-packages/PIL/ImageFont.py", line 226, in __init__
    self.font = core.getfont(

Exception Type: OSError at /admin/banner/smartrcback/add/
Exception Value: cannot open resource

But it's work ImageFont.load_default() I want 3 different fonts arialbold, arialbt, arialblack, arial So it's work local machine If any package available to install the fonts

Thanks to read the question

You need to actually serve the files: https://help.pythonanywhere.com/pages/DjangoStaticFiles/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM