Oct-24-2019, 04:51 PM
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:
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
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