Python Forum
Python PIL cutting off text.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python PIL cutting off text.
#1
I have created a little script that generates a small image in PIL. It has a gradient background and two sets of text written on it, that is gotten from a file. Here's the code:
from PIL import Image, ImageDraw, ImageFont, ImageOps

withoutMultiplier = []

with open("r_values.txt",'r') as r:
    vals = r.readlines()

for v in vals:
    new = v.replace('\n', '')
    vals[vals.index(v)] = new 
    withoutMultiplier.append(new.replace('K','').replace('R','').replace('M', ''))

def interpolate(f_co, t_co, interval):
    det_co =[(t - f) / interval for f , t in zip(f_co, t_co)]
    for i in range(interval):
        yield [round(f + det * i) for f, det in zip(f_co, det_co)]

for j in range(38, 60):
    gradient = Image.new('RGBA', (877,1240), color=0)
    newImg = Image.new('RGBA', (877,1240), color=0)
    draw = ImageDraw.Draw(gradient)

    if vals[j].find("R") > -1:  
        f_co = (152, 214, 148)
        t_co = (105, 186, 132)
    elif vals[j].find("K") > -1:  
        f_co = (235, 122, 34)
        t_co = (185, 98, 34)
    elif vals[j].find("M") > -1:
        f_co = (125, 174, 225)
        t_co = (89, 162, 225)
        
    for i, color in enumerate(interpolate(f_co, t_co, newImg.height * 2)):
        draw.line([(i, 0), (0, i)], tuple(color), width=1)

        im_composite = Image.alpha_composite(newImg, gradient)

    text_big = vals[j]
    text_small = vals[j]
    
    fnt_l = ImageFont.truetype('/Library/Fonts/DIN Alternate Bold.ttf', int(im_composite.height/3))
    fnt_s = ImageFont.truetype('/Library/Fonts/DIN Alternate Bold.ttf', int(im_composite.width/3))
    
    txt_l=Image.new('L', (im_composite.width,im_composite.height))
    txt_s=Image.new('L', (im_composite.width,im_composite.height))
    
    d = ImageDraw.Draw(txt_l)
    fw, fh = d.textsize(text_big, font=fnt_l)
    d.text( ( ((im_composite.width - fw)/2) - fh / 4, fh / 3), text_big,  font=fnt_l, fill=255)
    w=txt_l.rotate(90)

    r = ImageDraw.Draw(txt_s)
    fw, fh = r.textsize(text_small, font=fnt_s)
    r.text( ((im_composite.width / 2) - fw / 4, 0), text_small,  font=fnt_s, fill=255)
    u=txt_s.rotate(0)

    im_composite.paste(ImageOps.colorize(w, (255,255,255), (255,255,255)), (0,0),  w)
    im_composite.paste(ImageOps.colorize(u, (255,255,255), (255,255,255)), (0,0),  u)
    im_composite.save('images/'+vals[j]+'.png')
It runs fine with no errors and almost produced what I want it to. For instance, take these two images:
https://imgur.com/a/awH0dTO
They are perfect, no problems. However, have a look at these two:
https://imgur.com/a/LnzcrAW
The text is pretty severely cut off at the bottom. I could reduce set size, but then I have to go through and screw around with the positions, and it means the original ones will be too small, and that's not what I want as they are the perfect size right now.
I did google this but couldn't find anything. How can I get rid of this the 'clipping'?

Thanks,
Dream
Reply
#2
looks like you are controlling size, not PIL, so I suspect that's why text is being cut
Reply
#3
(Oct-25-2019, 12:24 AM)Larz60+ Wrote: looks like you are controlling size, not PIL, so I suspect that's why text is being cut

What do you mean? Like the size of the text?
Reply
#4
I have decided to use PyGame instead. It's a bit easier but mainly just a lot faster.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyttsx3 cutting off last word Extra 3 2,937 Feb-28-2022, 11:20 PM
Last Post: BashBedlam
  Input function cutting off commands at spaces. throwaway34 3 2,221 May-12-2021, 06:40 AM
Last Post: throwaway34

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020