Skip to content

Commit

Permalink
Merge pull request #201 from TeaM-TL/clipboard
Browse files Browse the repository at this point in the history
Clipboard
  • Loading branch information
TeaM-TL authored Dec 20, 2024
2 parents 5ea5dff + 8ce0c26 commit 77ecc6f
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 90 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 2024

5.1.3 get screenshost from clipboard and copy result into clipboard. Linux and BSD users - please install xclip

5.1.2 fixed: compose and crop, updated italian translation, less duplicates, require Python 3.10+

5.1.1 add arrow into text if needed. Pillow works
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ If ImageMagick or Wand are unavailable, Pillow is in use. There some limitation,
- processing JPG, PNG, SVG and TIFF images,
- processing picture in the fly, originals are safe,
- processing single file or whole directory,
- take screenshot (Linux) or get picture from clipboard (Windows and MacOS) and use it as source picture,
- after processing results is copied into clipboard (Windows and MacOS),
- get picture from clipboard and use it as source picture,
- after processing results is copied into clipboard,
- display selected tools,
- tools selection,
- preview orignal and result,
Expand Down Expand Up @@ -140,13 +140,13 @@ Download and install requirements:
- [ImageMagick](https://imagemagick.org/script/download.php#windows) - add path into `%PATH%` environment variable, enable install libraries!

```bash
python -m pip install pywin32 fotokilof
python -m pip install fotokilof
```

#### MacOS
Install requirements:
```bash
brew install imagemagick python@3.12 python-tk@3.12
brew install imagemagick python@3.13 python-tk@3.13
```

For Apple Silicon (M1, M2, M3) based Macs, the default Homebrew install directory differs from that on Homebrew Intel installs. The following environment variable allows FotoKilof to correctly locate the Homebrew-installed ImageMagick on Apple Silicon Macs:
Expand Down Expand Up @@ -215,5 +215,6 @@ Press F2 to toggle from Light to Dark or opposite.
![Python powered](python-powered.png)
[Imagemagick](https://github.com/ImageMagick/ImageMagick)
[Wand](https://github.com/emcconville/wand)
[ttkbootstrap](https://github.com/israel-dryer/ttkbootstrap)

[ttkbootstrap](https://github.com/israel-dryer/ttkbootstrap)
[FindSystemFontsFilename](https://github.com/moi15moi/FindSystemFontsFilename)
[Pillow](https://github.com/python-pillow/Pillow)
63 changes: 30 additions & 33 deletions fotokilof/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
TkVersion,
)

from PIL import ImageGrab

import ttkbootstrap as ttk
from ttkbootstrap.scrolled import ScrolledText, ScrolledFrame
from ttkbootstrap.tooltip import ToolTip
Expand All @@ -71,6 +73,19 @@
HORIZONTAL,
)

# my modules
import check_new_version
import convert
import convert_wand
import convert_pillow
import convert_common
import common
import gui
import ini_read
import ini_save
import magick
import version

try:
from wand.version import VERSION

Expand All @@ -89,19 +104,6 @@
IMAGEMAGICK_WAND_VERSION += ", IM - missing"
PILLOW = 1

# my modules
import check_new_version
import convert
import convert_wand
import convert_pillow
import convert_common
import common
import gui
import ini_read
import ini_save
import magick
import version

# logger = logging.getLogger(__name__)
logging.basicConfig(
filename=os.path.join(os.path.expanduser("~"), ".fotokilof.log"),
Expand All @@ -123,10 +125,7 @@
else:
OS = "UNIX"


if OS != "UNIX":
from PIL import ImageGrab
# set locale and clipboard for Windows
# set locale for Windows
if OS == "Windows":
import locale

Expand Down Expand Up @@ -978,19 +977,19 @@ def open_screenshot():
filename = now.strftime("%F_%H-%M-%S_%f") + ".png"
out_file = os.path.normpath(os.path.join(today_dir, filename))
do_it = 1
if OS == "UNIX":
try:
magick.magick(" ", "-quiet", out_file, "import", OS)
except:
logging.error("open_screenshot(), error in make screeshot ")
do_it = 0
else:
try:
screenshot = ImageGrab.grabclipboard()
try:
screenshot.save(out_file, "PNG")
except:
logging.error("open_screenshot(), error save from clipboards")
do_it = 0
except:
if OS == "UNIX":
message = _(
"Sorry, xclip (X11) or wl-clipboard (Wayland) is not installed\n Install xclip or wl-clipboard and try again!"
)
Messagebox.show_error(message, title=_("Missing package"))
try:
screenshot.save(out_file, "PNG")
except:
logging.error("open_screenshot(), error save from clipboards")
do_it = 0
if do_it:
open_file_common(today_dir, filename)

Expand Down Expand Up @@ -1995,10 +1994,8 @@ def text_tool_hide_show():
)

b_file_select_screenshot = ttk.Button(
frame_file_select, text=_("Screenshot"), command=open_screenshot, bootstyle="info"
frame_file_select, text=_("Clipboard"), command=open_screenshot, bootstyle="info"
)
if OS != "UNIX":
b_file_select_screenshot.configure(text=_("Clipboard"))

b_file_select_first = ttk.Button(
frame_file_select,
Expand Down Expand Up @@ -3170,7 +3167,7 @@ def text_tool_hide_show():
ToolTip(
b_file_select_screenshot,
text=_(
"MacOS and Windows: take image from clipboard.\nLinux: make screenshot, click window or select area.\nGrabbed image is saved into %TEMP%/today directory ad load for processing."
"Get image from clipboard.\nGrabbed image is saved into %TEMP%/today or $TMP/today directory and load for processing.\nLinux or BSD - install xclip"
),
)
ToolTip(
Expand Down
17 changes: 9 additions & 8 deletions fotokilof/convert_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@

import logging
import os.path
import os
import time
import PIL
from PIL import Image as PIL_Image

try:
from wand.image import Image
from wand.image import Image as Wand_Image
from wand.display import display

WAND_TEXT = "Wand found"
Expand Down Expand Up @@ -85,11 +86,12 @@ def display_image(file_to_display, set_pillow):
if os.path.isfile(file_to_display):
try:
if set_pillow:
with PIL.Image.open(file_to_display) as image:
with PIL_Image.open(file_to_display) as image:
image.show()
else:
with Image(filename=file_to_display) as image:
display(image)
x11 = os.getenv("DISPLAY")
with Wand_Image(filename=file_to_display) as image:
display(image, server_name=x11)
result = "OK"
except:
module_logger.error(
Expand All @@ -116,10 +118,10 @@ def get_image_size(file_in, is_pillow):
if os.path.isfile(file_in):
try:
if is_pillow:
with PIL.Image.open(file_in) as image:
with PIL_Image.open(file_in) as image:
size = image.size
else:
with Image(filename=file_in) as image:
with Wand_Image(filename=file_in) as image:
size = image.size
except:
module_logger.error("get_image_size: error read file: %s", file_in)
Expand Down Expand Up @@ -341,7 +343,6 @@ def compose(clone, compose_file, right, autoresize, color, gravity, set_pillow):
clone, compose_file, right, autoresize, color, gravity
)
module_logger.info("Compose %ss", str(time.time() - start_time))

else:
result = None
return result
81 changes: 49 additions & 32 deletions fotokilof/gui.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""
Copyright (c) 2019-2023 Tomasz Łuczak, TeaM-TL
Copyright (c) 2019-2024 Tomasz Łuczak, TeaM-TL
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -36,7 +36,7 @@

if platform.system() == "Windows":
import win32clipboard
elif platform.system() == "Darwin":
else:
import subprocess


Expand All @@ -51,36 +51,53 @@ def copy_to_clipboard(file_in, operating_system):
https://stackoverflow.com/questions/54008175/copy-an-image-to-macos-clipboard-using-python?rq=4
debug needed!
"""
if operating_system == "Windows":
# Create an in-memory file-like object
image_buffer = BytesIO()
image = Image.open(common.spacja(file_in, operating_system))
image.convert("RGB").save(image_buffer, "BMP")
data = image_buffer.getvalue()[14:]

win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
win32clipboard.CloseClipboard()
image_buffer.close()
elif operating_system == "MACOS":
try:
subprocess.run(
[
"osascript",
"-e",
'set the clipboard to (read (POSIX file "'
+ file_in
+ '") as JPEG picture)',
]
)
module_logger.debug(
"Successful copied result into clipboard under MacOS: %s", file_in
)
except:
module_logger.debug(
"Failed copied result into clipboard under MacOS: %s", file_in
)
match operating_system:
case "Windows":
# Create an in-memory file-like object
image_buffer = BytesIO()
image = Image.open(common.spacja(file_in, operating_system))
image.convert("RGB").save(image_buffer, "BMP")
data = image_buffer.getvalue()[14:]

win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data)
win32clipboard.CloseClipboard()
image_buffer.close()
case "MACOS":
try:
subprocess.run(
[
"osascript",
"-e",
'set the clipboard to (read (POSIX file "'
+ file_in
+ '") as JPEG picture)',
]
)
module_logger.debug(
"Successful copied result into clipboard under MacOS: %s", file_in
)
except:
module_logger.debug(
"Failed copied result into clipboard under MacOS: %s", file_in
)
case "UNIX":
with Image.open(file_in) as image:
image_buffer = BytesIO()
image.save(image_buffer, format="png")
try:
output = subprocess.Popen(
("xclip", "-selection", "clipboard", "-t", "image/png", "-i"),
stdin=subprocess.PIPE,
)
# write image to stdin
output.stdin.write(image_buffer.getvalue())
output.stdin.close()
except:
module_logger.debug(
"Failed copied result into clipboard under Unix: %s. Did you install xclip?", file_in
)


def only_numbers(char):
Expand Down
2 changes: 1 addition & 1 deletion fotokilof/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
THE SOFTWARE.
"""

__version__ = "5.1.2"
__version__ = "5.1.3"
__author__ = "Tomasz Łuczak"
__email__ = "[email protected]"
__appname__ = "FotoKilof"
Expand Down
27 changes: 17 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
with open("fotokilof/version.py") as fp:
exec(fp.read(), pkg_vars)
setup(
name=pkg_vars['__appname__'],
version=pkg_vars['__version__'],
author=pkg_vars['__author__'],
author_email=pkg_vars['__email__'],
description=pkg_vars['__description__'],
keywords=pkg_vars['__keywords__'],
url=pkg_vars['__url__'],
name=pkg_vars["__appname__"],
version=pkg_vars["__version__"],
author=pkg_vars["__author__"],
author_email=pkg_vars["__email__"],
description=pkg_vars["__description__"],
keywords=pkg_vars["__keywords__"],
url=pkg_vars["__url__"],
long_description=long_description,
long_description_content_type="text/markdown",
license="MIT",
Expand All @@ -25,11 +25,18 @@
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.10',
python_requires=">=3.10",
include_package_data=True,
packages=find_packages(),
install_requires=['FindSystemFontsFilename','pillow','requests','ttkbootstrap','wand'],
entry_points = {
install_requires=[
"FindSystemFontsFilename",
"pillow",
"requests",
"ttkbootstrap",
"wand",
'pywin32;platform_system=="Windows"',
],
entry_points={
"gui_scripts": [
"fotokilof = fotokilof:__main__",
]
Expand Down

0 comments on commit 77ecc6f

Please sign in to comment.