Python PIL save text in separate images

Good day.

I am trying to create a for loop to read the lines of a file until a condition is met so it can write these lines in a image and i plan to do this with every line of the file, like the example below:

Number: 123456789
Connecting to Database

no rows selected

Disconnecting from Database

Number: 9876543211
Connecting to Database

1111;48446511911986;Helen;Thursday
2222;48498489489489;Helen;Friday
3333;84545221185986;Helen;Monday

Disconnecting from Database

Number: 963852741
Connecting to Database

1234;123456789456123;Clyde;Friday
4321;123456789456123;Clyde;Thuesday
1423;123456789456123;Clyde;Sunday
2341;123456789456123;Clyde;Friday

Disconnecting from Database

Number: 456987321
Connecting to Database

no rows selected

Disconnecting from Database

As you can see every time the word Database shows up for the second time the next line is about a new number information, so i tried using the word database as a parameter for the loop below.

import os
import PIL
import PIL.Image as Image
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont


img = Image.open("C:/Users/dir/image/black_background.png")
draw = ImageDraw.Draw(img)

fonts_dir = os.path.join(os.environ['WINDIR'], 'Fonts')
font_name = 'consolab.ttf'
font = ImageFont.truetype(os.path.join(fonts_dir, font_name), 15)
x = 2
y = 0
next_print_count = 0
filename = "info.txt"
Number = ""

for line in open(filename):

    if 'Number:' in line:
        Number= line.split(" ",1)[1].strip()

    if 'Testing ' in line:
        line = ""

    draw.text((x, y),line,(200,200,200),font=font)

    y += 15
    img.save(Number + ".png")

Problem is that every time it starts a new file it also prints the information from the previous lines as well. How do i avoid that?

I also tried to use NUMBER as a parameter as well but it didn't work.

Upvotes: 1

Views: 503

Answers (1)

plasmon360
plasmon360

Reputation: 4199

you need to delete the current img and draw object every time line is "Disconnecting from Database" and make new objects after deleting them. In your original code, you were also saving the image every line, which is not good either. See the code below.

import os
import PIL
import PIL.Image as Image
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont




fonts_dir = os.path.join(os.environ['WINDIR'], 'Fonts')
font_name = 'consolab.ttf'
font = ImageFont.truetype(os.path.join(fonts_dir, font_name), 15)
x = 2
y = 0
next_print_count = 0
filename = r'info.txt'
Number = ""

with open(filename) as f: 
    img = Image.open("C:\Users\Public\Pictures\Sample Pictures/Chrysanthemum.jpg")
    draw = ImageDraw.Draw(img)

    for line in f:
        if 'Number:' in line:
            Number= line.split(" ",1)[1].strip()

        if 'Testing ' in line:
            line = ""

        draw.text((x, y),line,(200,200,200),font=font)
        y += 15

        if 'Disconnecting from Database' in line:
            img.save(Number + ".png")
            del draw, img
            img = Image.open("C:\Users\Public\Pictures\Sample Pictures/Chrysanthemum.jpg")
            draw = ImageDraw.Draw(img)
            y=0

results in (only showing two samples images here, but 4 are created)

enter image description here enter image description here

Upvotes: 1

Related Questions