Beispiel #1
0
def test_gmic_class_void_parameters_instantation_and_simple_hello_world_run(
        capfd):
    gmic_instance = gmic.Gmic()
    gmic_instance.run('echo_stdout "hello world"')
    outerr = capfd.readouterr()
    assert "hello world\n" == outerr.out

    # No variable assignment
    gmic.Gmic().run('echo_stdout "hello world2"')
    outerr = capfd.readouterr()
    assert "hello world2\n" == outerr.out
Beispiel #2
0
def make_map(filename_without_ext,
             addresses,
             gmic_effect="fx_freaky_bw 90,20,0,0,0,0",
             no_display=False,
             prefix="LETTER"):
    m = StaticMap(800, 600, 80)
    g = gmic.Gmic("update")
    locator = Nominatim(user_agent="BerlinWallBreaker Agent")
    print("geocoding..")
    for address in addresses:
        loc = locator.geocode(address)
        icon_flag = IconMarker((loc.longitude, loc.latitude),
                               './samples/icon-flag.png', 12, 32)
        m.add_marker(icon_flag)
        print(loc)

    image = m.render()
    output_filename1 = prefix + filename_without_ext + "_original.png"
    output_filename2 = prefix + filename_without_ext + ".png"
    image.save(output_filename1)
    print(output_filename1)
    if not no_display:
        g.run(output_filename1 + " display")
    if "_fx_stylize" in gmic_effect:
        gmic.run(output_filename1 + " " + gmic_effect + " output[2] " +
                 output_filename2 + (" display[2]" if not no_display else ""))
    else:
        gmic.run(output_filename1 + " " + gmic_effect + " output " +
                 output_filename2 + (" display" if not no_display else ""))
    print(output_filename2)

    return output_filename1, output_filename2
Beispiel #3
0
def test_gmic_user_file_autoload_and_use(gmic_instance_run, request, capfd):
    # Testing gmic user file auto-load for any type of G'MIC run technique
    # 1. Hack the file to auto-load, injecting a custom command into it
    gmic.run("echo_stdout $_path_user")
    gmic_home_user_file = capfd.readouterr().out.strip()
    assert gmic_home_user_file is not ""

    with gmic_any_user_file(gmic_home_user_file) as gmicpy_testing_command:
        # If gmic_instance_run is the current fixture (ie. run() method of a same instance singleton)
        # reinitialize it, it was created too early in the suite and is not fresh with autoloaded conf
        if inspect.isbuiltin(
                gmic_instance_run) and gmic_instance_run is not gmic.run:
            g = gmic.Gmic()
            gmic_instance_run = g.run

        # 2. Check that our custom command was auto-loaded as expected
        gmic_instance_run("sp leno " + gmicpy_testing_command)
Beispiel #4
0
def test_gmic_module_run_vs_single_instance_run_benchmark():
    from time import time

    testing_command = "sp lena blur 10 blur 30 blur 4"
    testing_iterations_max = 100
    expected_speed_improvement_by_single_instance_runs = 1.1  # at least 10%

    time_before_module_runs = time()
    for a in range(testing_iterations_max):
        gmic.run(testing_command)
    time_after_module_runs = time()

    time_before_instance_runs = time()
    gmic_instance = gmic.Gmic()
    for a in range(testing_iterations_max):
        gmic_instance.run(testing_command)
    time_after_instance_runs = time()

    assert (time_after_instance_runs - time_before_instance_runs
            ) * expected_speed_improvement_by_single_instance_runs < (
                time_after_module_runs - time_before_module_runs)
Beispiel #5
0
def test_freeing_gmic_interpreter(p, gmicpylog, capfd):
    # even though PyGmic.tp_dealloc gets called, it seems impossible to measure freed memory here
    import gmic

    pp = p.memory_percent()
    a = gmic.Gmic()

    outerr = capfd.readouterr()
    # assert "PyGmic_alloc\n" == outerr.out

    assert sys.getrefcount(a) == 2
    pp2 = p.memory_percent()
    assert (pp2 - pp) / pp > 0.005 and (
        pp2 - pp) / pp < 0.2  # >0.5 % <20 % memory variation
    del a

    outerr = capfd.readouterr()
    # assert "PyGmic_dealloc\n" == outerr.out

    pp3 = p.memory_percent()
    print(pp, pp2, pp3)
    assert pp3 - pp < 0.25  # < +25 percent points memory variation
Beispiel #6
0
def test_gmic_class_direct_run_remains_usable_instance():
    gmic_instance = gmic.Gmic('echo_stdout "single instance"')
    assert type(gmic_instance) == gmic.Gmic
    gmic_instance.run('echo_stdout "other run"')
Beispiel #7
0
import inspect
import os
import pathlib
import re
from math import floor

import gmic
import pytest
import PIL
from PIL import Image

# Tests parametrization: run calls to gmic.run(), gmic.Gmic() and gmic.Gmic().run() should have the same behaviour!
gmic_instance_types = {
    "argnames":
    "gmic_instance_run",
    "argvalues": [gmic.run, gmic.Gmic, gmic.Gmic().run],
    "ids":
    ["gmic_module_run", "gmic_instance_constructor_run", "gmic_instance_run"],
}

FLOAT_SIZE_IN_BYTES = 4
GMIC_VERSION_INT = int(gmic.__version__.replace(".", ""))


def assert_non_empty_file_exists(filename):
    """Assert filename corresponds to non-empty file and return is pathlib.Path object"""
    if type(filename) != pathlib.Path:
        a_file = pathlib.Path(filename)
    else:
        a_file = filename
    assert a_file.exists()
Beispiel #8
0
import gmic
import shutil

# Paper flipbook example from a GIF file
# ----------------------------------
g = gmic.Gmic()
images_list = []
GIF_FILENAME = "moonphases.gif"

# If 'convert' is installed
if shutil.which("convert"):
    g.run(GIF_FILENAME, images_list)
else:
    # If convert is absent
    # PIL and numpy must be installed for this to work
    import numpy
    from PIL import Image, ImageSequence

    im = Image.open(GIF_FILENAME)

    for frame in ImageSequence.Iterator(im):
        images_list.append(gmic.GmicImage.from_PIL(frame))

# Skip first green frame
images_list = images_list[1:]
# The G'MIC equivalent would be using 'remove' or 'keep'
# remove: https://gmic.eu/reference/remove.html
# keep: https://gmic.eu/reference/keep.html
# g.run("remove[0]", images_list)
# or
# g.run("keep[1--1]", images_list)
Beispiel #9
0
import gmic

# The G'MIC module - for debugging mostly
print(dir(gmic))

print(gmic.__spec__)
print(gmic.__version__)
print(gmic.__build__)

help(gmic)

# The G'MIC interpreter
gmic.run("sp apples rodilius 3 display")
gmic.run("sp earth blur 5 display")

g1 = gmic.Gmic()
g1.run("sp apples rodilius 3 display")
del g1
g2 = gmic.Gmic()
g2.run("sp earth blur 5 display")
del g2

g = gmic.Gmic()
g.run("sp apples rodilius 3")
g.run("sp apples blur 5")

# The G'MIC image
im = gmic.GmicImage()
im_ = gmic.GmicImage(width=640, height=480, spectrum=3)
help(gmic.GmicImage)
Beispiel #10
0
]
GMIC_FILTERS_WITH_SPECIAL_INPUTS = {
    "afre_halfhalf": {
        "input_images_count": 2
    },
    "rep_z_render": {
        "input_images_count": 3
    },
    "gui_rep_regm": {
        "input_images_count": 2
    },
    "fx_stylize": {
        "input_images_count": 2
    },
}
gmic_instance = gmic.Gmic()
gmic_instance.run("update")  # allows more filter to work
os.system("gmic update")  # same


def get_gmic_filters_json_str():
    gmic_version_suffix = gmic.__version__.replace(".", "")
    json_filename = GMIC_FILTERS_FILENAME_PATTERN.format(gmic_version_suffix)

    # Use filters JSON description file if exists, else download it first from gmic.eu
    if not os.path.exists(json_filename):
        print("downloading json file")
        json_url = GMIC_FILTERS_URL_PATTERN.format(gmic_version_suffix)
        with urllib.request.urlopen(json_url) as response:
            json_str = response.read()
            with open(json_filename, "wb") as json_file: