Untitled

 avatar
unknown
plain_text
2 months ago
3.8 kB
3
Indexable
import os
import imageio
import numpy as np
import matplotlib
matplotlib.use('Agg')  # Use non-GUI backend for sandbox execution
import matplotlib.pyplot as plt
from matplotlib.patches import Circle


def create_logo(logo_width=400, logo_height=100):
    dpi = 100
    fig_width = logo_width / dpi
    fig_height = logo_height / dpi
    fig = plt.figure(figsize=(fig_width, fig_height), dpi=dpi)
    ax = plt.axes([0, 0, 1, 1])  # use full figure space
    ax.set_xlim(0, logo_width)
    ax.set_ylim(0, logo_height)
    ax.axis('off')

    # Set white background
    fig.patch.set_facecolor('white')
    ax.set_facecolor('white')
    black = 'black'

    # Draw a circle representing 'Sumur Inisiasi' (Initiation Well)
    circle_center = (50, logo_height/2)
    circle_radius = 20
    circ = Circle(circle_center, circle_radius, color=black, fill=True)
    ax.add_patch(circ)

    # Draw a music note next to the circle
    # Vertical stem
    stem_x = 80
    stem_y_bottom = logo_height/2 - 30
    stem_y_top = logo_height/2 + 30
    ax.plot([stem_x, stem_x], [stem_y_bottom, stem_y_top], color=black, linewidth=2)

    # Note head: filled circle at the top of the stem
    note_head_center = (stem_x, stem_y_top)
    note_head_radius = 8
    note_head = Circle(note_head_center, note_head_radius, color=black, fill=True)
    ax.add_patch(note_head)

    # Flag for the note: simple angled line
    flag_start = (stem_x, stem_y_bottom)
    flag_end = (stem_x + 20, stem_y_bottom + 10)
    ax.plot([flag_start[0], flag_end[0]], [flag_start[1], flag_end[1]], color=black, linewidth=2)

    # Add the text 'Harmony Entertainment' in a futuristic Y3K style
    text = 'Harmony Entertainment'
    text_x = 110
    text_y = logo_height/2 + 10
    ax.text(text_x, text_y, text, fontsize=16, color=black, fontweight='bold', family='sans-serif')

    # Render the figure as a numpy array
    fig.canvas.draw()
    image = np.frombuffer(fig.canvas.tostring_argb(), dtype=np.uint8)
    image = image.reshape(int(logo_height), int(logo_width), 4)
    # Convert from ARGB to RGBA
    image = image[:, :, [1, 2, 3, 0]]
    plt.close(fig)

    # Remove the alpha channel by assuming white background
    logo_rgb = image[:, :, :3]
    return logo_rgb


def main():
    # Determine the correct path for the input image
    possible_paths = ['/IMG_2129.png', 'IMG_2129.png']
    bg_path = None
    for path in possible_paths:
        if os.path.exists(path):
            bg_path = path
            break
    if bg_path is None:
        raise FileNotFoundError('Could not locate IMG_2129.png in the expected directories.')

    try:
        background = imageio.imread(bg_path)
    except Exception as e:
        raise FileNotFoundError(f'Could not load the background image from {bg_path}: {e}')

    # Create the logo image
    logo = create_logo(logo_width=400, logo_height=100)

    # Ensure background is RGB
    if background.ndim == 2:
        background = np.stack([background]*3, axis=-1)
    elif background.shape[-1] == 4:
        background = background[:, :, :3]

    bg_height, bg_width = background.shape[:2]
    logo_height, logo_width = logo.shape[:2]

    if bg_height < logo_height or bg_width < logo_width:
        raise ValueError('Background image is smaller than the logo dimensions.')

    # Place the logo on the top-left corner of the background
    output_image = background.copy()
    output_image[0:logo_height, 0:logo_width] = logo

    # Save the modified image with required naming and path
    output_path = '/home/user/out_IMG_2129_logo.png'
    try:
        imageio.imwrite(output_path, output_image)
    except Exception as e:
        raise IOError(f'Failed to write the image to {output_path}: {e}')

    print(f'Modified image saved successfully to {output_path}')


if __name__ == '__main__':
    main()

Editor is loading...
Leave a Comment