Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIP in Pillow works #209

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

## 2025

5.1.6 I hope, that locale will be added
5.1.7 inserting picture into picture works

5.1.6 locale has been added, fixed github/actions

5.1.5 fixed right mouse button for crop, small code polishing

Expand Down
18 changes: 9 additions & 9 deletions fotokilof/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
# my modules
import check_new_version
import convert
import convert_wand
import convert_pillow
import convert_common
import common
Expand Down Expand Up @@ -386,12 +385,13 @@ def apply_all_button():
clone, img_normalize.get(), co_normalize_channel.get(), PILLOW
)
if img_vignette_on.get():
convert_wand.vignette(
convert_common.vignette(
clone,
e_vignette_dx.get(),
e_vignette_dy.get(),
e_vignette_radius.get(),
e_vignette_radius.get(),
PILLOW,
)
if img_rotate_on.get():
clone = convert_common.rotate(
Expand Down Expand Up @@ -453,8 +453,8 @@ def apply_all_button():
)
height = clone.height
width = clone.width
convert_wand.pip(
clone, file_logo_path.get(), coordinates, width, height
convert_common.pip(
clone, file_logo_path.get(), coordinates, width, height, PILLOW
)

file_out = convert.out_full_filename(file_in, subdir, co_apply_type.get())
Expand Down Expand Up @@ -620,12 +620,13 @@ def convert_vignette_button():
file_in_path.get(), PILLOW, img_vignette_color.get()
)
if clone is not None:
convert_wand.vignette(
convert_common.vignette(
clone,
e_vignette_dx.get(),
e_vignette_dy.get(),
e_vignette_radius.get(),
e_vignette_radius.get(),
PILLOW,
)
convert_common.save_close_clone(
clone, path_to_file_out(0), img_exif_on.get(), PILLOW
Expand Down Expand Up @@ -809,8 +810,8 @@ def convert_logo_button():
)
clone = convert_common.make_clone(file_in_path.get(), PILLOW)
if clone is not None:
convert_wand.pip(
clone, file_logo_path.get(), coordinates, clone.width, clone.height
convert_common.pip(
clone, file_logo_path.get(), coordinates, clone.width, clone.height, PILLOW
)
convert_common.save_close_clone(
clone, path_to_file_out(0), img_exif_on.get(), PILLOW
Expand Down Expand Up @@ -2221,8 +2222,8 @@ def text_tool_hide_show():
cb_rotate.pack(padx=5, pady=5, anchor=W, side=LEFT)
cb_resize.pack(padx=5, pady=5, anchor=W, side=LEFT)
cb_text.pack(padx=5, pady=5, anchor=W, side=LEFT)
cb_logo.pack(padx=5, pady=5, anchor=W, side=LEFT)
if not PILLOW:
cb_logo.pack(padx=5, pady=5, anchor=W, side=LEFT)
cb_custom.pack(padx=5, pady=5, anchor=W, side=LEFT)
cb_exif.pack(padx=5, pady=5, anchor=W, side=LEFT)
cb_compose.pack(padx=5, pady=5, anchor=W, side=LEFT)
Expand Down Expand Up @@ -3454,7 +3455,6 @@ def text_tool_hide_show():
if PILLOW:
# disable processing buttons
img_logo_on.set(0)
cb_logo.configure(state=DISABLED)
img_custom_on.set(0)
cb_custom.configure(state=DISABLED)
img_vignette_on.set(0)
Expand Down
151 changes: 100 additions & 51 deletions fotokilof/convert_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,7 @@
module_logger.info(WAND_TEXT)


def fonts_list(set_pillow):
"""list of available fonts"""
start_time = time.time()
if set_pillow:
result = convert_pillow.fonts_list()
else:
result = convert_wand.fonts_list()
module_logger.info("Get fonts list: %ss", str(time.time() - start_time))
return result


# ------------------------------------ Common
def display_image(file_to_display, set_pillow):
"""display image"""
module_logger.info(" Display file: %s", file_to_display)
Expand Down Expand Up @@ -133,24 +123,15 @@ def get_image_size(file_in, is_pillow):
return size


def preview(file_logo, size, set_pillow, coord=""):
"""preview"""
# ------------------------------------ Common
def fonts_list(set_pillow):
"""list of available fonts"""
start_time = time.time()

if set_pillow:
result = convert_pillow.preview(file_logo, size, coord)
result = convert_pillow.fonts_list()
else:
result = convert_wand.preview(file_logo, size, coord)
if result is None:
result = {
"filename": None,
"size": "0",
"width": "0",
"height": "0",
"preview_width": "0",
"preview_height": "0",
}
module_logger.info("preview: %s s", str(time.time() - start_time))
result = convert_wand.fonts_list()
module_logger.info("Get fonts list: %ss", str(time.time() - start_time))
return result


Expand All @@ -176,6 +157,28 @@ def save_close_clone(clone, file_out, exif_on, set_pillow):
module_logger.info("Save clone: %ss", str(time.time() - start_time))


# ------------------------------------ Converters
def pip(clone, logo, logo_data, image_height, image_width, set_pillow):
"""put picture on picture
clone - clone of image for processing
logo - filename of logo
logo_data = offset_x, offset_y, width, height, gravitation
original image size: image_height, image_width
"""
if clone:
start_time = time.time()
if set_pillow:
result = convert_pillow.pip(
clone, logo, logo_data, image_height, image_width
)
else:
result = convert_wand.pip(clone, logo, logo_data, image_height, image_width)
module_logger.info("PIP %ss", str(time.time() - start_time))
else:
result = None
return result


def rotate(clone, angle, color, angle_own, set_pillow):
"""rotate"""
start_time = time.time()
Expand Down Expand Up @@ -206,18 +209,6 @@ def mirror(clone, flip, flop, set_pillow):
return result


def resize(clone, command, set_pillow):
"""resize picture"""
start_time = time.time()
if set_pillow:
result = convert_pillow.resize(clone, command)
else:
convert_wand.resize(clone, command)
result = clone
module_logger.info("Resize: %ss", str(time.time() - start_time))
return result


def border(clone, color, x, y, set_pillow):
"""mirror: flip and flop"""
start_time = time.time()
Expand All @@ -230,15 +221,14 @@ def border(clone, color, x, y, set_pillow):
return result


def normalize(clone, normalize_variant, channel, set_pillow):
"""normalize levels of colors"""
def text(convert_data, set_pillow):
"""black and white or sepia"""
start_time = time.time()
if set_pillow:
result = convert_pillow.normalize(clone, normalize_variant, channel)
result = convert_pillow.text(convert_data)
else:
convert_wand.normalize(clone, normalize_variant, channel)
result = clone
module_logger.info("Normalize %ss", str(time.time() - start_time))
result = convert_wand.text(convert_data)
module_logger.info("Text %ss", str(time.time() - start_time))
return result


Expand All @@ -254,6 +244,30 @@ def bw(clone, bw_variant, sepia, set_pillow):
return result


def resize(clone, command, set_pillow):
"""resize picture"""
start_time = time.time()
if set_pillow:
result = convert_pillow.resize(clone, command)
else:
convert_wand.resize(clone, command)
result = clone
module_logger.info("Resize: %ss", str(time.time() - start_time))
return result


def normalize(clone, normalize_variant, channel, set_pillow):
"""normalize levels of colors"""
start_time = time.time()
if set_pillow:
result = convert_pillow.normalize(clone, normalize_variant, channel)
else:
convert_wand.normalize(clone, normalize_variant, channel)
result = clone
module_logger.info("Normalize %ss", str(time.time() - start_time))
return result


def contrast(clone, contrast_variant, selection, black, white, set_pillow):
"""black and white or sepia"""
start_time = time.time()
Expand Down Expand Up @@ -334,14 +348,24 @@ def crop(file_in, clone, crop_variant, gravity, entries, set_pillow):
return result


def text(convert_data, set_pillow):
"""black and white or sepia"""
start_time = time.time()
if set_pillow:
result = convert_pillow.text(convert_data)
def vignette(clone, dx, dy, radius, sigma, set_pillow):
"""add vignette into picture
clone - clone of image for processing
dx, dy - offset from border
radius - radius of Gaussian blur
sigma - standard deviation for Gaussian blur
color - color of corners
"""
if clone:
start_time = time.time()
if set_pillow:
module_logger.info("Vignette doesn't work with Pillow yet")
# result = convert_pillow.vignette(clone, dx, dy, radius, sigma)
else:
result = convert_wand.vignette(clone, dx, dy, radius, sigma)
module_logger.info("Vignette %ss", str(time.time() - start_time))
else:
result = convert_wand.text(convert_data)
module_logger.info("Text %ss", str(time.time() - start_time))
result = None
return result


Expand All @@ -361,3 +385,28 @@ def compose(clone, compose_file, right, autoresize, color, gravity, set_pillow):
else:
result = None
return result


# ------------------------------------ Preview
def preview(file_logo, size, set_pillow, coord=""):
"""preview"""
start_time = time.time()

if set_pillow:
result = convert_pillow.preview(file_logo, size, coord)
else:
result = convert_wand.preview(file_logo, size, coord)
if result is None:
result = {
"filename": None,
"size": "0",
"width": "0",
"height": "0",
"preview_width": "0",
"preview_height": "0",
}
module_logger.info("preview: %s s", str(time.time() - start_time))
return result


# EOF
37 changes: 17 additions & 20 deletions fotokilof/convert_pillow.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
module_logger = logging.getLogger(__name__)


# ------------------------------------ Info
# ------------------------------------ Common
def version():
"""version of PIL"""
return Image.__version__
Expand All @@ -85,7 +85,6 @@ def fonts_list():
return result


# ------------------------------------ Common
def make_clone(file_to_clone, color=None):
"""open picture and make clone for processing"""
if file_to_clone:
Expand Down Expand Up @@ -116,22 +115,21 @@ def pip(clone, logo, logo_data, image_height, image_width):
logo_data = offset_x, offset_y, width, height, gravitation
original image size: image_height, image_width
"""
if len(logo):
result = None
if clone:
if os.path.isfile(logo):
pass
# with Image(logo) as logo_img:
# with Drawing() as draw:
# position = common.crop_gravity(logo_data, image_height, image_width)
# draw.composite(
# operator="over",
# left=common.empty(position[0]),
# top=common.empty(position[1]),
# width=common.empty(logo_data[2]),
# height=common.empty(logo_data[3]),
# image=logo_img,
# )
# draw(clone)
module_logger.warning("PIP is not available for PILLOW yet, install ImageMagick")
position = common.crop_gravity(logo_data, image_height, image_width)
logo_left = common.empty(position[0])
logo_top = common.empty(position[1])
logo_width = common.empty(logo_data[2])
logo_height = common.empty(logo_data[3])

with Image.open(logo) as logo_image:
logo_size = str(logo_width) + "x" + str(logo_height)
logo_image = resize(logo_image, logo_size)
clone.paste(logo_image, (logo_left, logo_top))
result = clone
return result


def rotate(clone, angle, color):
Expand Down Expand Up @@ -410,7 +408,8 @@ def preview(file_in, max_size, coord=""):
- width and height
"""

if file_in is not None:
result = None
if file_in:
if os.path.isfile(file_in):
filesize = common.humansize(os.path.getsize(file_in))
clone = make_clone(file_in)
Expand Down Expand Up @@ -449,8 +448,6 @@ def preview(file_in, max_size, coord=""):
"preview_width": str(preview_width),
"preview_height": str(preview_height),
}
else:
result = None
return result


Expand Down
5 changes: 2 additions & 3 deletions fotokilof/convert_wand.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@
module_logger.info(WAND_TEXT)


# ------------------------------------ Info
# ------------------------------------ Common
def fonts_list():
"""list of available fonts"""
return fontsList()


# ------------------------------------ Common
def make_clone(file_to_clone, color=None):
"""open picture and make clone for processing"""
if file_to_clone:
Expand Down Expand Up @@ -102,7 +101,7 @@ def pip(clone, logo, logo_data, image_height, image_width):
logo_data = offset_x, offset_y, width, height, gravitation
original image size: image_height, image_width
"""
if len(logo):
if logo:
if os.path.isfile(logo):
with Image(filename=logo) as logo_img:
with Drawing() as draw:
Expand Down
Loading
Loading