File size: 2,403 Bytes
c3a1897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51f8a02
7712f1b
51f8a02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3a1897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from PIL import Image, ImageDraw, ImageFont
import cv2
import os
import textwrap
import nltk
nltk.download('punkt', quiet=True)
nltk.download('averaged_perceptron_tagger', quiet=True)
from nltk.tokenize import word_tokenize
from nltk import pos_tag


def read_image_width_height(image_path):
    image = Image.open(image_path)
    width, height = image.size
    return width, height


def resize_long_edge(image, target_size=384):
    # Calculate the aspect ratio
    width, height = image.size
    aspect_ratio = float(width) / float(height)

    # Determine the new dimensions
    if width > height:
        new_width = target_size
        new_height = int(target_size / aspect_ratio)
    else:
        new_width = int(target_size * aspect_ratio)
        new_height = target_size

    # Resize the image
    resized_image = image.resize((new_width, new_height), Image.ANTIALIAS)
    return resized_image

def display_images_and_text(source_image_path, generated_image, generated_paragraph, outfile_name):
    source_image = Image.open(source_image_path)
    # Create a new image that can fit the images and the text
    width = source_image.width + generated_image.width
    height = max(source_image.height, generated_image.height)
    new_image = Image.new("RGB", (width, height + 150), "white")

    # Paste the source image and the generated image onto the new image
    new_image.paste(source_image, (0, 0))
    new_image.paste(generated_image, (source_image.width, 0))

    # Write the generated paragraph onto the new image
    draw = ImageDraw.Draw(new_image)
    # font_size = 12
    # font = ImageFont.load_default().font_variant(size=font_size)
    font_path = os.path.join(cv2.__path__[0],'qt','fonts','DejaVuSans.ttf')
    font = ImageFont.truetype(font_path, size=14)

    # Wrap the text for better display
    wrapped_text = textwrap.wrap(generated_paragraph, width=170)
    # Draw each line of wrapped text
    line_spacing = 18
    y_offset = 0
    for line in wrapped_text:
        draw.text((0, height + y_offset), line, font=font, fill="black")
        y_offset += line_spacing

    # Show the final image
    # new_image.show()
    new_image.save(outfile_name)
    return 1


def extract_nouns_nltk(paragraph):
    words = word_tokenize(paragraph)
    pos_tags = pos_tag(words)
    nouns = [word for word, tag in pos_tags if tag in ('NN', 'NNS', 'NNP', 'NNPS')]
    return nouns