Skip to content

Commit

Permalink
More
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Sep 8, 2023
1 parent 658edd1 commit fb0d1a5
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/faker_file/contrib/pdf_file/pil_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def add_picture(

# Create a BytesIO object outside the 'with' statement
output_stream = BytesIO()
pil_image.save(output_stream, format="JPEG")
pil_image.save(output_stream, format="PNG")
output_stream.seek(0) # Move to the start of the stream

# X, Y coordinates where the text will be placed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def __init__(self, **kwargs):
self.pages = []
self.img = None
self.draw = None
self.image_mode = "RGB"

@classmethod
def find_max_fit_for_multi_line_text(
Expand Down Expand Up @@ -119,10 +120,12 @@ def handle_kwargs(self: "PilPdfGenerator", **kwargs) -> None:
self.line_height = kwargs["line_height"]
if "spacing" in kwargs:
self.spacing = kwargs["spacing"]
if "image_mode" in kwargs:
self.image_mode = kwargs["image_mode"]

def create_image_instance(self) -> Image:
return Image.new(
"RGB",
self.image_mode,
(self.page_width, self.page_height),
(255, 255, 255),
)
Expand Down
81 changes: 80 additions & 1 deletion src/faker_file/tests/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
from copy import deepcopy
from functools import partial
from importlib import import_module, reload
from io import BytesIO
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union

import pytest
from faker import Faker
from parametrize import parametrize
from pathy import use_fs
from PIL import Image, ImageDraw

from ..base import DEFAULT_REL_PATH, DynamicTemplate, pystr_format_func
from ..constants import (
Expand Down Expand Up @@ -278,6 +280,36 @@
logging.getLogger("fontTools").setLevel(logging.WARNING)


def generate_image_bytes(
width: int = 100, height: int = 100, image_mode: str = "RGBA"
) -> bytes:
"""Generate an image.
Usage example:
.. code-block:: python
image_bytes = generate_rgba_image_bytes()
"""
# Create a new image with RGBA mode
image_with_alpha = Image.new(
image_mode,
(width, height),
(255, 255, 255, 255),
)

# Draw a red rectangle with partial transparency
draw = ImageDraw.Draw(image_with_alpha)
draw.rectangle(((20.0, 20.0), (80.0, 80.0)), fill=(255, 0, 0, 128))

# Save to a BytesIO object
stream = BytesIO()
image_with_alpha.save(stream, "PNG")

# Get the byte array of the image
return stream.getvalue()


def create_test_files():
"""Create test files for `random_file_from_dir` and `file_from_path`.
Expand Down Expand Up @@ -1052,22 +1084,69 @@ class ProvidersTestCase(unittest.TestCase):
"content": DynamicTemplate(
[
(pdf_pil_add_h1_heading, {}),
(
pdf_pil_add_picture,
{"image_width": 801, "image_height": 1201},
),
(pdf_pil_add_paragraph, {"max_nb_chars": 5_000}),
(pdf_pil_add_h2_heading, {}),
(pdf_pil_add_h3_heading, {}),
(pdf_pil_add_h4_heading, {}),
(pdf_pil_add_h5_heading, {}),
(pdf_pil_add_h6_heading, {}),
(pdf_pil_add_picture, {}),
(pdf_pil_add_paragraph, {"content": TEXT_PDF}),
(
pdf_pil_add_paragraph,
{"content": TEXT_PDF, "margin": (1, 1, 1, 1)},
),
(pdf_pil_add_page_break, {}),
(pdf_pil_add_h6_heading, {}),
(
pdf_pil_add_picture,
{
"image_bytes": generate_image_bytes(
image_mode="RGBA"
)
},
),
(pdf_pil_add_table, {}),
(pdf_pil_add_non_existing_heading, {}),
]
),
},
None,
),
(
FAKER,
PdfFileProvider,
"pdf_file",
{
"pdf_generator_cls": PIL_PDF_GENERATOR,
"pdf_generator_kwargs": {
"encoding": "utf8",
"font_size": 14,
"page_width": 800,
"page_height": 1200,
"line_height": 16,
"spacing": 5,
"image_mode": "RGBA",
},
"content": DynamicTemplate(
[
(pdf_pil_add_h1_heading, {}),
(
pdf_pil_add_picture,
{
"image_bytes": generate_image_bytes(
image_mode="RGB"
)
},
),
]
),
},
None,
),
(
FAKER,
PdfFileProvider,
Expand Down

0 comments on commit fb0d1a5

Please sign in to comment.