parent
1bf9b34c9b
commit
1424b3edd5
@ -0,0 +1,41 @@
|
||||
from PIL import Image, ImageDraw,ImageFont
|
||||
import textwrap
|
||||
import re
|
||||
|
||||
def draw_multiple_line_text(image, text, font, text_color,padding):
|
||||
'''
|
||||
Draw multiline text over given image
|
||||
'''
|
||||
draw = ImageDraw.Draw(image)
|
||||
Fontperm= font.getsize(text)
|
||||
image_width, image_height = image.size
|
||||
lines = textwrap.wrap(text, width=50)
|
||||
y=(image_height/2)-(((Fontperm[1]+(len(lines)*padding)/len(lines))*len(lines))/2) #dont touch it
|
||||
for line in lines:
|
||||
line_width, line_height = font.getsize(line)
|
||||
draw.text(((image_width - line_width) / 2, y),
|
||||
line, font=font, fill=text_color)
|
||||
y += line_height + padding
|
||||
|
||||
|
||||
def imagemaker(text,
|
||||
reddit_obj,
|
||||
idx='NoName',
|
||||
padding=5
|
||||
):
|
||||
id = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"])
|
||||
font=ImageFont.truetype("arial.ttf", 20)
|
||||
Fontperm= font.getsize(text)
|
||||
# print(Fontperm[1])
|
||||
size=(500,176)
|
||||
|
||||
textcolor=(240,240,240)
|
||||
|
||||
image =Image.new('RGBA',size,(33,33,36,255))
|
||||
|
||||
draw = ImageDraw.Draw(image)
|
||||
if len(text)>50:
|
||||
draw_multiple_line_text(image, text,font, textcolor,padding)
|
||||
else:
|
||||
draw.text(((image.size[0]-Fontperm[0])/2,(image.size[1]-Fontperm[1])/2),font=font,text=text,align='center') #(image.size[1]/2)-(Fontperm[1]/2)
|
||||
image.save(f'assets/temp/{id}/png/img{idx}.png')
|
@ -0,0 +1,46 @@
|
||||
|
||||
MAX_CHARACTER= 200
|
||||
|
||||
#working good
|
||||
def posttextparser(obj):
|
||||
text=obj#["thread_post"]
|
||||
newtext=[]
|
||||
# for text in text:
|
||||
if len(text)>MAX_CHARACTER:
|
||||
text2=text.split("\n")
|
||||
for dot in text2:
|
||||
if len(dot)>MAX_CHARACTER:
|
||||
text3=dot.split(".")
|
||||
for comma in text3:
|
||||
if len(comma)> MAX_CHARACTER:
|
||||
text4=comma.split(',')
|
||||
newtext.extend(text4)
|
||||
else:
|
||||
newtext.append(comma)
|
||||
else:
|
||||
newtext.append(dot)
|
||||
else:
|
||||
newtext.append(text)
|
||||
return remover(newtext)
|
||||
|
||||
def remover(List):
|
||||
reg=['',' ','.','\n',')',"''",'"',"'",'"','""'] #add if any any unwant value found
|
||||
lines=List
|
||||
lines1=[]
|
||||
lines2=[]
|
||||
|
||||
for item in lines:
|
||||
for r in reg :
|
||||
if item==r:
|
||||
break
|
||||
else:
|
||||
continue
|
||||
else:
|
||||
lines1.append(item)
|
||||
|
||||
for a in lines1: #Double check
|
||||
if a!='':
|
||||
aa=a.strip()
|
||||
lines2.append(aa)
|
||||
# print(f'"{a}"')
|
||||
return lines2
|
Loading…
Reference in new issue