M.M
4 ماه پیش توسط M.M مطرح شد
2 پاسخ

باگ در استفاده از Pillow در پایتون

ببینید بچه ها دارم نابود میشم سر ای باگ هر کاری میکنم نمیشه . از چت جی پی تی هم میپرسم کار رو بد تر کرده .

این کده :

from PIL import Image, ImageDraw, ImageFont

def generate_image(text, font_path):
    # Set font properties using a TrueType font
    font_size = 20
    font = ImageFont.truetype(font_address, font_size)

    # Create a blank image with a large enough initial size
    initial_width, initial_height = 400, 200
    image = Image.new('RGB', (initial_width, initial_height), color='white')
    draw = ImageDraw.Draw(image)

    # Get text size with the given font
    text_width, text_height = draw.textsize(text, font=font)

    # Calculate a suitable image size based on the text size
    width = text_width + 20
    height = text_height + 20

    # Create a new image with the appropriate dimensions
    image = Image.new('RGB', (width, height), color='white')
    draw = ImageDraw.Draw(image)

    # Draw text on the image
    draw.text((10, 10), text, fill='black', font=font)

    # Save the image
    image.save("generated_image.png")

# Replace with your text and font file path (.ttf)
user_text = "a banna eating another banna"
font_address = "C:/Windows/Fonts/Yekan-Bakh-FaNum-08-Fat (2).woff"  # Replace with the correct .ttf font file path

# Generate the image
generate_image(user_text, font_address)

الان ارور اینه که میگه no attribute name textsize نمیتونم بفهمم چیکار کنم لطفا راهنمایی کنید (فونت آدرس هم آدرس فونتیه که توی وی اس کد گذاشتمش و آدرسش توی سیستممو گفتم)


ثبت پرسش جدید
dargadege
تخصص : فرانت اند کار ، بک اند کار ، ب...
@darg11adege 3 ماه پیش مطرح شد
0

داخل خط ۱۴ روی متغیر text_width مقدار draw.textsize رو ریختی
میگه این اتریبیوت textsize اصلا وجود نداره

حالا من زیاد با پیلو کار نکردم ولی مشکل کارت اینجاس

این کد رو chatgpt نوشته؟
میتونم کد خودت رو ببینم؟


حسین عظیمی
تخصص : Python
@azimi2087 3 ماه پیش مطرح شد
0

این کد یک تصویر از متن دلخواه کاربر با استفاده از یک فونت خاص ایجاد می‌کند. اما اشکالات زیر وجود دارد:

  1. اگر فونتی با آدرس مورد نظر موجود نباشد یا آدرس آن اشتباه باشد، کد خطا خواهد داد.
  2. بار دوباره ایجاد تصویر با اندازه مناسب نیاز نیست، زیرا این کار در خطوط ۷ تا ۱۴ انجام شده است و قبل و بعد از آن هم انجام شده.

اما برای بهینه‌سازی کد، می‌توانید از کد زیر استفاده کنید:

from PIL import Image, ImageDraw, ImageFont

def generate_image(text, font_path):
    font_size = 20
    font = ImageFont.truetype(font_path, font_size)

    # Get text size with the given font
    text_width, text_height = font.getsize(text)

    # Create a new image with the appropriate dimensions
    width = text_width + 20
    height = text_height + 20
    image = Image.new('RGB', (width, height), color='white')
    draw = ImageDraw.Draw(image)

    # Draw text on the image
    draw.text((10, 10), text, fill='black', font=font)

    # Save the image
    image.save("generated_image.png")

# Replace with your text and font file path (.ttf)
user_text = "a banna eating another banna"
font_address = "C:/Windows/Fonts/Yekan-Bakh-FaNum-08-Fat (2).woff"  # Replace with the correct .ttf font file path

# Generate the image
generate_image(user_text, font_address)

این کد شامل تغییراتی است که اشکالات مذکور را برطرف کرده و بهینه‌ترین شکل از کد است.


برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام