pygui_simple 0.3.1__tar.gz → 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/PKG-INFO +1 -2
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/pyproject.toml +1 -2
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/src/pygui_simple/tkmatplot.py +33 -9
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/src/pygui_simple/tkwin.py +169 -19
- pygui_simple-0.3.1/src/pygui_simple/cv2_utilities.py +0 -162
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/LICENSE +0 -0
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/README.md +0 -0
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/src/pygui_simple/__init__.py +0 -0
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/src/pygui_simple/py.typed +0 -0
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/src/pygui_simple/tkcalendar.py +0 -0
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/src/pygui_simple/tkcontrol.py +0 -0
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/src/pygui_simple/tkscrollpicker.py +0 -0
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/src/pygui_simple/tkslideswitch.py +0 -0
- {pygui_simple-0.3.1 → pygui_simple-0.5.0}/src/pygui_simple/winbasic.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pygui_simple
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: A simple gui library
|
|
5
5
|
Author: Youchee
|
|
6
6
|
Author-email: Youchee <youchi.xu@live.com>
|
|
@@ -9,7 +9,6 @@ License-File: LICENSE
|
|
|
9
9
|
Classifier: Programming Language :: Python :: 3
|
|
10
10
|
Classifier: Operating System :: OS Independent
|
|
11
11
|
Requires-Dist: matplotlib>=3.10.8
|
|
12
|
-
Requires-Dist: opencv-python-headless>=4.13.0.92
|
|
13
12
|
Requires-Python: >=3.13
|
|
14
13
|
Project-URL: Homepage, https://github.com/YoucheeXu/pygui
|
|
15
14
|
Project-URL: Issues, https://github.com/YoucheeXu/pygui/issues
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "pygui_simple"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.5.0"
|
|
4
4
|
description = "A simple gui library"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [
|
|
@@ -9,7 +9,6 @@ authors = [
|
|
|
9
9
|
requires-python = ">=3.13"
|
|
10
10
|
dependencies = [
|
|
11
11
|
"matplotlib>=3.10.8",
|
|
12
|
-
"opencv-python-headless>=4.13.0.92",
|
|
13
12
|
]
|
|
14
13
|
classifiers = [
|
|
15
14
|
"Programming Language :: Python :: 3",
|
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
from dataclasses import dataclass, field
|
|
4
4
|
import tkinter as tk
|
|
5
5
|
from typing import cast, Any
|
|
6
|
+
from typing import TypeAlias
|
|
7
|
+
from collections.abc import Callable
|
|
6
8
|
|
|
9
|
+
from matplotlib import backend_bases
|
|
7
10
|
from matplotlib.figure import Figure
|
|
8
11
|
from matplotlib.lines import Line2D
|
|
9
12
|
from matplotlib.container import Container
|
|
@@ -16,7 +19,7 @@ import numpy as np
|
|
|
16
19
|
from pygui_simple.tkcontrol import tkControl
|
|
17
20
|
|
|
18
21
|
|
|
19
|
-
__version__ = "2.
|
|
22
|
+
__version__ = "2.5.0"
|
|
20
23
|
|
|
21
24
|
|
|
22
25
|
font = {'family' : 'SimHei',
|
|
@@ -34,6 +37,7 @@ class LineData:
|
|
|
34
37
|
visible: bool = True
|
|
35
38
|
line: Line2D | Container | None = None
|
|
36
39
|
|
|
40
|
+
Event_Callback: TypeAlias = Callable[[backend_bases.Event], object]
|
|
37
41
|
|
|
38
42
|
class MatPlotCtrl(tkControl):
|
|
39
43
|
def __init__(
|
|
@@ -61,11 +65,11 @@ class MatPlotCtrl(tkControl):
|
|
|
61
65
|
self._xlabel: str = xlabel
|
|
62
66
|
self._ylabel: str = ylabel
|
|
63
67
|
|
|
64
|
-
|
|
65
|
-
self._canvas: FigureCanvasTkAgg = FigureCanvasTkAgg(
|
|
68
|
+
self._fig: Figure = Figure(figsize=self._size, dpi=self._dpi)
|
|
69
|
+
self._canvas: FigureCanvasTkAgg = FigureCanvasTkAgg(self._fig, parent)
|
|
66
70
|
super().__init__(parent, title, idself, self._canvas.get_tk_widget())
|
|
67
|
-
self._ax: Axes =
|
|
68
|
-
|
|
71
|
+
self._ax: Axes = self._fig.add_subplot()
|
|
72
|
+
self._fig.subplots_adjust(bottom=0.25)
|
|
69
73
|
self._create()
|
|
70
74
|
|
|
71
75
|
def _create(self):
|
|
@@ -84,12 +88,32 @@ class MatPlotCtrl(tkControl):
|
|
|
84
88
|
def xdata(self, xdata: ArrayLike):
|
|
85
89
|
self._xdata = xdata
|
|
86
90
|
|
|
91
|
+
@property
|
|
92
|
+
def ax(self):
|
|
93
|
+
return self._ax
|
|
94
|
+
|
|
95
|
+
@property
|
|
96
|
+
def canvas(self):
|
|
97
|
+
return self._canvas
|
|
98
|
+
|
|
99
|
+
def add_tooltip(self, x: float,
|
|
100
|
+
y: float,
|
|
101
|
+
s: str,
|
|
102
|
+
fontdict: dict[str, Any] | None = None,
|
|
103
|
+
**kwargs: Any):
|
|
104
|
+
return self._ax.text(x, y, s, fontdict, **kwargs)
|
|
105
|
+
|
|
106
|
+
def event_callback(self, s: str, func: Event_Callback):
|
|
107
|
+
_ = self._fig.canvas.mpl_connect(s, func)
|
|
108
|
+
|
|
87
109
|
def _bar(self, xdata: ArrayLike, ydata: ArrayLike, **style_dict: Any):
|
|
88
110
|
bar = self._ax.bar(xdata, ydata, **style_dict)
|
|
89
|
-
if isinstance(bar, list):
|
|
90
|
-
|
|
91
|
-
else:
|
|
92
|
-
|
|
111
|
+
# if isinstance(bar, list):
|
|
112
|
+
# return bar[0]
|
|
113
|
+
# else:
|
|
114
|
+
# return bar
|
|
115
|
+
|
|
116
|
+
return bar
|
|
93
117
|
|
|
94
118
|
def _plot(self, xdata: ArrayLike, ydata: ArrayLike, **style_dict: Any):
|
|
95
119
|
line = self._ax.plot(xdata, ydata, **style_dict)
|
|
@@ -9,7 +9,7 @@ import platform
|
|
|
9
9
|
import ctypes
|
|
10
10
|
from functools import partial
|
|
11
11
|
# from collections import OrderedDict
|
|
12
|
-
from typing import Literal, Any, override, cast,
|
|
12
|
+
from typing import Literal, Any, override, cast, TypeAlias
|
|
13
13
|
from typing import Protocol, TypeVar, Generic
|
|
14
14
|
# from typing import get_args, get_origin
|
|
15
15
|
# from collections.abc import Callable
|
|
@@ -21,9 +21,7 @@ from tkinter import scrolledtext
|
|
|
21
21
|
import xml.etree.ElementTree as et
|
|
22
22
|
from ast import literal_eval
|
|
23
23
|
|
|
24
|
-
import
|
|
25
|
-
# from PIL import Image
|
|
26
|
-
from PIL import ImageTk
|
|
24
|
+
from PIL import Image, ImageTk
|
|
27
25
|
|
|
28
26
|
from idlelib.statusbar import MultiStatusBar
|
|
29
27
|
from idlelib.tooltip import Hovertip
|
|
@@ -32,7 +30,6 @@ from pygui_simple.winbasic import EventHanlder, Widget, Container, Dialog, WinBa
|
|
|
32
30
|
from pygui_simple.tkcontrol import tkControl
|
|
33
31
|
from pygui_simple.tkmatplot import MatPlotCtrl
|
|
34
32
|
from pygui_simple.tkslideswitch import SlideSwitchCtrl
|
|
35
|
-
import pygui_simple.cv2_utilities as cv2u
|
|
36
33
|
from pygui_simple.tkcalendar import CalendarCtrl
|
|
37
34
|
from pygui_simple.tkscrollpicker import ScrollPickerCtrl, TimeScrollPickerCtrl
|
|
38
35
|
|
|
@@ -87,19 +84,16 @@ class ImagePanelCtrl(tkControl):
|
|
|
87
84
|
_ = ctrl.configure(text="", anchor=tk.CENTER, **options)
|
|
88
85
|
|
|
89
86
|
def _read_image(self, imagepath: str, w: int, h: int):
|
|
90
|
-
|
|
87
|
+
img_raw = Image.open(imagepath).convert("RGBA")
|
|
91
88
|
if w:
|
|
92
89
|
# pv(w)
|
|
93
90
|
# pv(h)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return image2
|
|
91
|
+
img_raw = img_raw.resize((w, h))
|
|
92
|
+
return ImageTk.PhotoImage(img_raw)
|
|
97
93
|
|
|
98
|
-
def display_image(self, image:
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
_ = cast(tk.Label, super().control).configure(image=image2)
|
|
102
|
-
self._image = image2
|
|
94
|
+
def display_image(self, image: ImageTk.PhotoImage):
|
|
95
|
+
_ = cast(tk.Label, super().control).configure(image=image)
|
|
96
|
+
self._image = image
|
|
103
97
|
|
|
104
98
|
|
|
105
99
|
class ButtonCtrl(tkControl):
|
|
@@ -148,12 +142,11 @@ class ImageBtttonCtrl(tkControl):
|
|
|
148
142
|
|
|
149
143
|
def _read_image(self, imagepath: str, w: int, h: int):
|
|
150
144
|
# image = cv2.imread(imagepath, cv2.IMREAD_UNCHANGED)
|
|
151
|
-
|
|
145
|
+
img_raw = Image.open(fp=imagepath).convert("RGBA")
|
|
152
146
|
if w:
|
|
153
147
|
# image = cv2.resize(image, (w, h), interpolation=cv2.INTER_CUBIC)
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
return image2
|
|
148
|
+
img_raw = img_raw.resize(size=(w, h))
|
|
149
|
+
return ImageTk.PhotoImage(image=img_raw)
|
|
157
150
|
|
|
158
151
|
# TODO: wait to test
|
|
159
152
|
def change_image(self, imagefile: str, w: int = 0, h: int = 0):
|
|
@@ -635,6 +628,162 @@ class ToolbarCtrl(tkControl):
|
|
|
635
628
|
super().destroy()
|
|
636
629
|
|
|
637
630
|
|
|
631
|
+
_FontDescription: TypeAlias = str | tuple[str, int] | tuple[str, int, str]
|
|
632
|
+
|
|
633
|
+
class CanvasCtrl(tkControl):
|
|
634
|
+
def __init__(self, parent: tk.Misc, idself: str, res_path: str = "", **options: Any):
|
|
635
|
+
ctrl = tk.Canvas(parent, **options)
|
|
636
|
+
tkControl.__init__(self, parent, "", idself, ctrl)
|
|
637
|
+
self._res_path: str = res_path
|
|
638
|
+
self._img_cache: list[ImageTk.PhotoImage | None] = []
|
|
639
|
+
|
|
640
|
+
def create_text(
|
|
641
|
+
self,
|
|
642
|
+
x: float,
|
|
643
|
+
y: float,
|
|
644
|
+
/,
|
|
645
|
+
*,
|
|
646
|
+
activefill: str = "",
|
|
647
|
+
activestipple: str = "",
|
|
648
|
+
anchor: Literal['nw', 'n', 'ne', 'w', 'center', 'e', 'sw', 's', 'se'] = "center",
|
|
649
|
+
angle: float | str = 0,
|
|
650
|
+
disabledfill: str = "",
|
|
651
|
+
disabledstipple: str = "",
|
|
652
|
+
fill: str = "black",
|
|
653
|
+
font: _FontDescription = "",
|
|
654
|
+
justify: Literal['left', 'center', 'right'] = "center",
|
|
655
|
+
offset: float | str = "n",
|
|
656
|
+
state: Literal['normal', 'hidden', 'disabled'] = "normal",
|
|
657
|
+
stipple: str = "",
|
|
658
|
+
tags: str | list[str] | tuple[str, ...] = "",
|
|
659
|
+
text: float | str = "",
|
|
660
|
+
width: float | str = 0
|
|
661
|
+
) -> int:
|
|
662
|
+
"""Typed wrapper for tk.Canvas.create_text with positional-only x/y and keyword-only remaining args.
|
|
663
|
+
|
|
664
|
+
Args:
|
|
665
|
+
x: Horizontal coordinate for text anchor point, positional-only parameter.
|
|
666
|
+
y: Vertical coordinate for text anchor point, positional-only parameter.
|
|
667
|
+
activefill: Fill color when item is in active state.
|
|
668
|
+
activestipple: Stipple pattern for active fill color.
|
|
669
|
+
anchor: Anchor position aligning text to (x,y) coordinate.
|
|
670
|
+
angle: Rotation angle of text; None uses Tk internal default.
|
|
671
|
+
disabledfill: Fill color when item is disabled.
|
|
672
|
+
disabledstipple: Stipple pattern for disabled fill color.
|
|
673
|
+
fill: Normal text foreground color, default set to black for visibility.
|
|
674
|
+
font: Tk font definition (string or font tuple format), None uses default system font.
|
|
675
|
+
justify: Multi-line text alignment mode.
|
|
676
|
+
offset: Text drawing offset value, None uses default.
|
|
677
|
+
state: Display state of text item(normal/hidden/disabled).
|
|
678
|
+
stipple: Stipple pattern applied to text fill.
|
|
679
|
+
tags: Tag label or tag list bound to this canvas item.
|
|
680
|
+
text: Content string or numeric value to render as text.
|
|
681
|
+
width: Max wrap width for multi-line text, None disables auto line wrap.
|
|
682
|
+
|
|
683
|
+
Returns:
|
|
684
|
+
int: Unique integer canvas item ID returned by native Tk canvas.
|
|
685
|
+
"""
|
|
686
|
+
canvas = cast(tk.Canvas, self._tkctrl)
|
|
687
|
+
item_id = canvas.create_text(
|
|
688
|
+
x, y,
|
|
689
|
+
activefill=activefill,
|
|
690
|
+
activestipple=activestipple,
|
|
691
|
+
anchor=anchor,
|
|
692
|
+
angle=angle,
|
|
693
|
+
disabledfill=disabledfill,
|
|
694
|
+
disabledstipple=disabledstipple,
|
|
695
|
+
fill=fill,
|
|
696
|
+
font=font,
|
|
697
|
+
justify=justify,
|
|
698
|
+
offset=offset,
|
|
699
|
+
state=state,
|
|
700
|
+
stipple=stipple,
|
|
701
|
+
tags=tags,
|
|
702
|
+
text=text,
|
|
703
|
+
width=width
|
|
704
|
+
)
|
|
705
|
+
return item_id
|
|
706
|
+
|
|
707
|
+
def create_rectangle(self,
|
|
708
|
+
x1: float,
|
|
709
|
+
y1: float,
|
|
710
|
+
x2: float,
|
|
711
|
+
y2: float,
|
|
712
|
+
/,
|
|
713
|
+
*,
|
|
714
|
+
fill: str = "",
|
|
715
|
+
outline: str = "",
|
|
716
|
+
width: float | str = 1,
|
|
717
|
+
dash: str | int | list[int] | tuple[int, ...] = None,
|
|
718
|
+
stipple: str = "",
|
|
719
|
+
activefill: str = "",
|
|
720
|
+
activeoutline: str = "",
|
|
721
|
+
activedash: str | int | list[int] | tuple[int, ...] = None,
|
|
722
|
+
disabledfill: str = "",
|
|
723
|
+
disabledoutline: str = "",
|
|
724
|
+
state: Literal['normal', 'hidden', 'disabled'] = "normal",
|
|
725
|
+
tags: str | list[str] | tuple[str, ...] = ""
|
|
726
|
+
) -> int:
|
|
727
|
+
canvas = cast(tk.Canvas, self._tkctrl)
|
|
728
|
+
item_id = canvas.create_rectangle(
|
|
729
|
+
x1, y1, x2, y2,
|
|
730
|
+
fill=fill,
|
|
731
|
+
outline=outline,
|
|
732
|
+
width=width,
|
|
733
|
+
dash=dash,
|
|
734
|
+
stipple=stipple,
|
|
735
|
+
activefill=activefill,
|
|
736
|
+
activeoutline=activeoutline,
|
|
737
|
+
activedash=activedash,
|
|
738
|
+
disabledfill=disabledfill,
|
|
739
|
+
disabledoutline=disabledoutline,
|
|
740
|
+
state=state,
|
|
741
|
+
tags=tags
|
|
742
|
+
)
|
|
743
|
+
return item_id
|
|
744
|
+
|
|
745
|
+
def create_image(
|
|
746
|
+
self,
|
|
747
|
+
x: float,
|
|
748
|
+
y: float,
|
|
749
|
+
/,
|
|
750
|
+
*,
|
|
751
|
+
file_path: str,
|
|
752
|
+
target_w: int,
|
|
753
|
+
target_h: int,
|
|
754
|
+
anchor: Literal['nw', 'n', 'ne', 'w', 'center', 'e', 'sw', 's', 'se'] = "center",
|
|
755
|
+
fallback_fill: str = "#cccccc",
|
|
756
|
+
tags: str | list[str] | tuple[str, ...] | None = None
|
|
757
|
+
) -> int:
|
|
758
|
+
"""Load local image file, auto resize then draw on canvas; auto fallback to colored rectangle on load failure.
|
|
759
|
+
All loaded PhotoImage cached inside instance list to avoid Python GC destroy image resource.
|
|
760
|
+
|
|
761
|
+
Args:
|
|
762
|
+
x: Anchor X coordinate for image placement, positional-only param.
|
|
763
|
+
y: Anchor Y coordinate for image placement, positional-only param.
|
|
764
|
+
file_path: Relative/absolute local path of target image file(png/jpg supported).
|
|
765
|
+
target_w: Fixed pixel width to resize source image.
|
|
766
|
+
target_h: Fixed pixel height to resize source image.
|
|
767
|
+
anchor: Align rule of image against input (x,y) coordinate.
|
|
768
|
+
fallback_fill: Fill color for substitute rectangle when image file missing/corrupted.
|
|
769
|
+
tags: Tag labels bind to generated canvas image item.
|
|
770
|
+
|
|
771
|
+
Returns:
|
|
772
|
+
int: Canvas item ID of either loaded image or fallback substitute rectangle.
|
|
773
|
+
"""
|
|
774
|
+
canvas = cast(tk.Canvas, self._tkctrl)
|
|
775
|
+
# Open source image and convert to RGBA to support transparent alpha channel
|
|
776
|
+
image_path = os.path.join(self._res_path, file_path)
|
|
777
|
+
raw_img = Image.open(image_path).convert("RGBA")
|
|
778
|
+
# High quality down/up scale via LANCZOS resample algorithm
|
|
779
|
+
resized_img = raw_img.resize((target_w, target_h), Image.Resampling.LANCZOS)
|
|
780
|
+
photo_obj = ImageTk.PhotoImage(resized_img)
|
|
781
|
+
# Persist image reference into internal cache to prevent garbage collection
|
|
782
|
+
self._img_cache.append(photo_obj)
|
|
783
|
+
item_id = canvas.create_image(x, y, image=photo_obj, anchor=anchor, tags=tags)
|
|
784
|
+
return item_id
|
|
785
|
+
|
|
786
|
+
|
|
638
787
|
T = TypeVar("T", bound=tk.Tk | tk.Toplevel)
|
|
639
788
|
class tkWM(Generic[T], Widget):
|
|
640
789
|
def __init__(self, wm: T):
|
|
@@ -1021,7 +1170,8 @@ class tkWin(WinBasic):
|
|
|
1021
1170
|
ctrl = ButtonCtrl(master, owner, idctrl, text, **options)
|
|
1022
1171
|
# TODO
|
|
1023
1172
|
case "Canvas":
|
|
1024
|
-
ctrl = tk.Canvas(master, **options)
|
|
1173
|
+
# ctrl = tk.Canvas(master, **options)
|
|
1174
|
+
ctrl = CanvasCtrl(master, idctrl, self._res_path, **options)
|
|
1025
1175
|
case "Combobox":
|
|
1026
1176
|
ctrl = ComboboxCtrl(master, owner,
|
|
1027
1177
|
idself=idctrl,
|
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/python3
|
|
2
|
-
# -*- coding: UTF-8 -*-
|
|
3
|
-
import inspect
|
|
4
|
-
import re
|
|
5
|
-
from typing import cast
|
|
6
|
-
|
|
7
|
-
import numpy as np
|
|
8
|
-
import cv2
|
|
9
|
-
# import PIL
|
|
10
|
-
from PIL import Image, ImageTk
|
|
11
|
-
|
|
12
|
-
def show_image(image: cv2.typing.MatLike):
|
|
13
|
-
for line in inspect.getframeinfo(inspect.currentframe().f_back)[3]:
|
|
14
|
-
# print(line)
|
|
15
|
-
m = re.search(r'\bimgShow\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)', line)
|
|
16
|
-
if m:
|
|
17
|
-
title = m.group(1)
|
|
18
|
-
|
|
19
|
-
cv2.imshow(title, image)
|
|
20
|
-
_ = cv2.waitKey(0)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def read_image(impath: str, flags: int = cv2.IMREAD_UNCHANGED):
|
|
24
|
-
# load the image from disk
|
|
25
|
-
# impath_gbk = impath.encode('gbk') # unicode转gbk,字符串变为字节数组
|
|
26
|
-
# return cv2.imread(impath_gbk.decode(), cv2.IMREAD_UNCHANGED) # 字节数组直接转字符串,不解码
|
|
27
|
-
return cv2.imdecode(np.fromfile(impath, dtype=np.uint8), flags)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
def scale_image(image: cv2.typing.MatLike, w: int, h: int, interp: int = cv2.INTER_AREA):
|
|
31
|
-
"""
|
|
32
|
-
Args:
|
|
33
|
-
interp: cv.INTER_NEAREST 最近邻插值 cv.INTER_LINEAR 双线性插值
|
|
34
|
-
cv.INTER_CUBIC 三次样条插值 cv.INTER_AREA 使用像素区域关系重新采样。
|
|
35
|
-
"""
|
|
36
|
-
return cv2.resize(image, (w, h), interpolation=interp)
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
def rotate_image(image: cv2.typing.MatLike, degree: float):
|
|
40
|
-
# Shape of image in terms of pixels.
|
|
41
|
-
h, w = cast(tuple[int, int], image.shape[:2])
|
|
42
|
-
|
|
43
|
-
ll = max(w, h)
|
|
44
|
-
cx, cy = (ll // 2, ll // 2)
|
|
45
|
-
padding_w = (ll - w) // 2 # 指定零填充的宽度
|
|
46
|
-
padding_h = (ll - h) // 2 # 指定零填充的高度
|
|
47
|
-
|
|
48
|
-
# 在原图像做对称的零填充,使得图片由矩形变为方形
|
|
49
|
-
image_padded: cv2.typing.MatLike = np.zeros(shape=(ll, ll, 3), dtype=np.uint8)
|
|
50
|
-
image_padded[padding_h : padding_h + h, padding_w : padding_w + w, :] = image
|
|
51
|
-
|
|
52
|
-
# getRotationMatrix2D creates a matrix needed for transformation.
|
|
53
|
-
# - (cX, cY): 旋转的中心点坐标
|
|
54
|
-
# - degree: 旋转的度数,正度数表示逆时针旋转,而负度数表示顺时针旋转。
|
|
55
|
-
# - 1.0:旋转后图像的大小,1.0原图,2.0变成原来的2倍,0.5变成原来的0.5倍
|
|
56
|
-
rm = cv2.getRotationMatrix2D((cx, cy), degree, 1)
|
|
57
|
-
rotated_padded = cv2.warpAffine(image_padded, rm, (ll, ll))
|
|
58
|
-
|
|
59
|
-
image = rotated_padded[
|
|
60
|
-
padding_w : padding_w + w, padding_h : padding_h + h, :
|
|
61
|
-
]
|
|
62
|
-
return image
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
def image2photo(image: cv2.typing.MatLike):
|
|
66
|
-
""" OpenCV represents images in BGR order however PIL represents
|
|
67
|
-
images in RGB order, so we need to swap the channels
|
|
68
|
-
"""
|
|
69
|
-
image1 = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
|
|
70
|
-
|
|
71
|
-
# convert the images to PIL format...
|
|
72
|
-
image2 = Image.fromarray(image1)
|
|
73
|
-
# ...and then to ImageTk format
|
|
74
|
-
image3 = ImageTk.PhotoImage(image2)
|
|
75
|
-
return image3
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
def read_exif(fname: str): #定义获取图片exif的方法
|
|
79
|
-
""" Get embedded EXIF data from image file."""
|
|
80
|
-
ret = {} #创建一个字典对象存储exif的条目如相机品牌:相应品牌这样的数据
|
|
81
|
-
try:
|
|
82
|
-
# image = PIL.Image.open(fname) # 创建图像对象
|
|
83
|
-
image = Image.open(fname) # 创建图像对象
|
|
84
|
-
if hasattr(image, '_getexif'): # 检查图像对象有无_getexif属性,发现也有getexif属性,内容好像差不多
|
|
85
|
-
exif_info = image._getexif() # 取出img的_getexif属性,这是一个字典对象
|
|
86
|
-
if exif_info is not None:
|
|
87
|
-
for tag, value in exif_info.items():
|
|
88
|
-
decoded = PIL.ExifTags.TAGS.get(tag, tag)
|
|
89
|
-
ret[decoded] = value
|
|
90
|
-
except IOError:
|
|
91
|
-
print('IOERROR: ' + fname)
|
|
92
|
-
return ret
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
def decode_gpsinfo(gps_info):
|
|
96
|
-
"""
|
|
97
|
-
00 VersionID int8u[4]
|
|
98
|
-
(tags 0x0001-0x0006 used for camera location according to MWG 2.0.)
|
|
99
|
-
01 LatitudeRef string[2] 'N' = North 'S' = South
|
|
100
|
-
02 Latitude float64u[3]
|
|
101
|
-
03 LongitudeRef string[2] 'E' = East 'W' = West
|
|
102
|
-
04 Longitude float64u[3]
|
|
103
|
-
05 AltitudeRef int8u 0 = Above Sea Level 1 = Below Sea Level
|
|
104
|
-
2 = Positive Sea Level (sea-level ref) 3 = Negative Sea Level (sea-level ref)
|
|
105
|
-
06 Altitude float64u
|
|
106
|
-
07 TimeStamp float64u[3] (UTC time of GPS fix. When writing,
|
|
107
|
-
date is stripped off if present, and time is adjusted to UTC if it includes a timezone)
|
|
108
|
-
08 Satellites string
|
|
109
|
-
09 Status string[2] 'A' = Measurement Active 'V' = Measurement Void
|
|
110
|
-
10 MeasureMode string[2] 2 = 2-Dimensional Measurement 3 = 3-Dimensional Measurement
|
|
111
|
-
11 DOP float64u
|
|
112
|
-
12 SpeedRef string[2] 'K' = km/h 'M' = mph 'N' = knots
|
|
113
|
-
13 Speed float64u
|
|
114
|
-
14 TrackRef string[2] 'M' = Magnetic North 'T' = True North
|
|
115
|
-
15 Track float64u
|
|
116
|
-
16 ImgDirectionRef string[2] 'M' = Magnetic North 'T' = True North
|
|
117
|
-
17 ImgDirection float64u
|
|
118
|
-
18 MapDatum string
|
|
119
|
-
(tags 0x0013-0x001a used for subject location according to MWG 2.0)
|
|
120
|
-
19 DestLatitudeRef string[2] 'N' = North 'S' = South
|
|
121
|
-
20 DestLatitude float64u[3]
|
|
122
|
-
21 DestLongitudeRef string[2] 'E' = East 'W' = West
|
|
123
|
-
22 DestLongitude float64u[3]
|
|
124
|
-
23 DestBearingRef string[2] 'M' = Magnetic North 'T' = True North
|
|
125
|
-
24 DestBearing float64u
|
|
126
|
-
25 DestDistanceRef string[2] 'K' = Kilometers 'M' = Miles 'N' = Nautical Miles
|
|
127
|
-
26 DestDistance float64u
|
|
128
|
-
27 ProcessingMethod undef (values of "GPS", "CELLID", "WLAN" or "MANUAL" by the EXIF spec.)
|
|
129
|
-
28 AreaInformation undef
|
|
130
|
-
29 DateStamp string[11] (when writing, time is stripped off if present,
|
|
131
|
-
after adjusting date/time to UTC if time includes a timezone. Format is YYYY:mm:dd)
|
|
132
|
-
30 Differential int16u 0 = No Correction 1 = Differential Corrected
|
|
133
|
-
31 HPositioningError float64u
|
|
134
|
-
"""
|
|
135
|
-
gpsitem_dict = {1: "LatitudeRef", 2: "Latitude", 3: "LongitudeRef", 4: "Longitude",
|
|
136
|
-
5: "AltitudeRef", 6: "Altitude", 7: "TimeStamp", 8: "Satellites", 9: "Status",
|
|
137
|
-
10: "MeasureMode", 11: "DOP", 12: "SpeedRef", 13: "Speed", 14: "TrackRef",
|
|
138
|
-
15: "Track", 16: "ImgDirectionRef", 17: "ImgDirection", 18: "MapDatum",
|
|
139
|
-
19: "DestLatitudeRef", 20: "DestLatitude", 21: "DestLongitudeRef",
|
|
140
|
-
22: "DestLongitude", 23: "DestBearingRef", 24: "DestBearing",
|
|
141
|
-
25: "DestDistanceRef", 26: "DestDistance", 27: "ProcessingMethod",
|
|
142
|
-
28: "AreaInformation", 29: "DateStamp", 30: "Differential", 31: "HPositioningError"}
|
|
143
|
-
gpsinfo_decoded = {}
|
|
144
|
-
for tag, val in gps_info.items():
|
|
145
|
-
key = gpsitem_dict[tag]
|
|
146
|
-
val_decoded = val
|
|
147
|
-
match key:
|
|
148
|
-
case "LatitudeRef":
|
|
149
|
-
val_decoded = "North" if val == "N" else "South"
|
|
150
|
-
case "LongitudeRef":
|
|
151
|
-
val_decoded = "East" if val == "E" else "West"
|
|
152
|
-
case "SpeedRef":
|
|
153
|
-
val_decoded = {"K": "km/h", "M": "mph", "N": "knots"}.get(val, "Unknown")
|
|
154
|
-
case "ImgDirectionRef":
|
|
155
|
-
val_decoded = {"M": "Magnetic North", "T": "True North"}.get(val, "Unknown")
|
|
156
|
-
case "DestBearingRef":
|
|
157
|
-
val_decoded = {"M": "Magnetic North", "T": "True North"}.get(val, "Unknown")
|
|
158
|
-
case _:
|
|
159
|
-
pass
|
|
160
|
-
gpsinfo_decoded[key] = val_decoded
|
|
161
|
-
return gpsinfo_decoded
|
|
162
|
-
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|