2

screenshot of output ALL of the data in the picture is taken from an excel sheet, size of the image is 220×320, I cant increase the image size. But I want Line 4 data that is exiting the image to go down below row 4.

import pandas as pd
df = pd.read_excel('vetements.xlsx')
from PIL import Image, ImageDraw, ImageFont
def prepare_row(row):
    row = row.tolist()
    rec = []
    rec.append(row[0] + ' ' + str(row[1]) +' £')
    rec.append(row[2].replace('Tapez :', '').strip() + ' ' + str(row[3]))
    rec.append('Tailies: ' + row[5])
    rec.append('Couleurs: ' + row[4])
    return rec
def write_to_image(row):
    img = Image.open('sample_image.png')
    d1 = ImageDraw.Draw(img)
    font1 = ImageFont.truetype("arial.ttf", 17)
    font2 = ImageFont.truetype("arial.ttf", 30)
    d1.text((11, 22), row[0], font=font1 ,fill=(0, 0, 0))
    d1.text((11, 62), row[1], font=font2 ,fill=(0, 0, 0))
    d1.text((11, 112), row[2], font=font1 ,fill=(0, 0, 0))
    d1.text((11, 162), row[3], font=font1 ,fill=(0, 0, 0))
    img.show()
write_to_image(prepare_row(df.loc[10]))
  • 1
    You will have to use d1.textsize to figure out if the text will fit, and if it does not, decide how to split it to another line. A classic word wrap algorithm. – Tim Roberts Mar 08 '21 at 06:52

0 Answers0