|
| 1 | +import colorsys |
1 | 2 | import os |
2 | 3 | import pytest |
3 | 4 | import random |
| 5 | +from collections import namedtuple |
4 | 6 |
|
5 | 7 | from django.conf import settings |
6 | 8 | from django.core.management import call_command |
|
9 | 11 |
|
10 | 12 | from finder.models.folder import FolderModel |
11 | 13 |
|
12 | | -from .utils import create_random_image |
| 14 | +from PIL import Image, ImageDraw, ImageFont |
13 | 15 |
|
14 | 16 | os.environ.setdefault('DJANGO_ALLOW_ASYNC_UNSAFE', 'true') |
15 | 17 |
|
16 | 18 |
|
| 19 | +class ColorRGBA(namedtuple('ColorRGBA', ['red', 'green', 'blue', 'alpha'])): |
| 20 | + def __new__(cls, red=0, green=0, blue=0, alpha=255): |
| 21 | + return super(ColorRGBA, cls).__new__(cls, red, green, blue, alpha) |
| 22 | + |
| 23 | + def rotate_hue(self, degrees: float): |
| 24 | + hue, lum, sat = colorsys.rgb_to_hls(self.red, self.blue, self.green) |
| 25 | + hue = (hue + degrees / 360.0) % 1.0 |
| 26 | + lum = 255.0 - lum |
| 27 | + red, green, blue = map(int, colorsys.hls_to_rgb(hue, lum, sat)) |
| 28 | + return self._replace(red=red, green=green, blue=blue) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(autouse=True, scope='session') |
| 32 | +def random_image() -> Image: |
| 33 | + background_color = ColorRGBA(red=255, green=255, blue=255) |
| 34 | + image = Image.new('RGB', (4000, 3000), color=background_color) |
| 35 | + drawing = ImageDraw.Draw(image) |
| 36 | + color = ColorRGBA(red=255) |
| 37 | + drawing.rectangle([(10, 10), (130, 130)], fill=color) |
| 38 | + color = color.rotate_hue(25) |
| 39 | + drawing.rectangle([(150, 10), (270, 130)], fill=color) |
| 40 | + |
| 41 | + # foreground_color = rotate_hue(background_color, 180) |
| 42 | + # font = ImageFont.truetype(Path(__file__).parent / 'fonts/Courier.ttf', 20) |
| 43 | + # drawing.text((10, 90), faker.text(15), fill=foreground_color, font=font) |
| 44 | + image.save(settings.BASE_DIR / 'workdir/assets/demo_image.png') |
| 45 | + |
| 46 | + |
17 | 47 | @pytest.fixture(autouse=True, scope='session') |
18 | 48 | def create_assets(): |
19 | 49 | os.makedirs(settings.BASE_DIR / 'workdir/assets', exist_ok=True) |
20 | 50 | with open(settings.BASE_DIR / 'workdir/assets/small_file.bin', 'wb') as handle: |
21 | 51 | handle.write(random.randbytes(1000)) |
22 | 52 | with open(settings.BASE_DIR / 'workdir/assets/huge_file.bin', 'wb') as handle: |
23 | 53 | handle.write(random.randbytes(100000)) |
24 | | - image = create_random_image() |
25 | | - image.save(settings.BASE_DIR / 'workdir/assets/demo_image.png') |
| 54 | + # image = create_random_image() |
| 55 | + # image.save(settings.BASE_DIR / 'workdir/assets/demo_image.png') |
26 | 56 |
|
27 | 57 |
|
28 | 58 | @pytest.fixture(scope='session') |
|
0 commit comments