Compare commits

..
51 Commits
Author SHA1 Message Date
lm 3e42b24110 Refine texture metrics and update default weights for premium pattern detection 2026-03-31 01:44:45 +02:00
lm e13bef7f52 docs: use new config.toml with detailed comments and update readme 2026-03-24 23:23:00 +01:00
lm 46e1eb0925 docs: Update README and provide config.toml.example 2026-03-24 23:20:30 +01:00
lm dab226f55e UI polish: overlay color, debounce, and tooltips
- Added overlay color storage to settings import/export
- Removed generic keyboard shortcuts from menus and replaced them with localized tooltips
- Added QTimer debouncing to log history dialog to prevent status string spam
- Ensured export worker gracefully handles KeyErrors with standard return tuple
2026-03-24 23:17:05 +01:00
lm 9ff56ce7ef Finalize documentation: Simplified layout and GPLv3 License 2026-03-23 21:28:14 +01:00
lm 5a9cbfc943 Update default alpha value 2026-03-23 21:03:49 +01:00
lm ef648cc676 Implement grouping score, customizable export weights, and fix color selection bug 2026-03-23 21:00:16 +01:00
lm 1c48a53c19 Implement background exclusion and refactor folder structure
- Added configurable background exclusion (#1f2937) with tolerance
- Implemented alpha thresholding (>= 128) to eliminate edge artifacts
- Refactored folder structure into analyses/[slug]/images, settings, and results
- Updated pattern puller to skip existing images and handle network errors
- Updated .gitignore and automated tests for path integrity
2026-03-22 20:09:05 +01:00
lm ff9dec0eff Add saturation max slider to UI and image processing logic 2026-03-22 17:42:26 +01:00
lm daf226a80f del images 2026-03-13 18:42:42 +01:00
lm 7f219885bf Feature: Advanced Navigation & Composite Scoring
- Added direct pattern jump input field (Ctrl+J behavior without hotkey)

- Implemented Composite Score: 35% match, 55% excl match, 10% brightness

- Added 'Prefer darkness' toggle to invert brightness score

- Added checkmarks to 'Prefer darkness' and 'Toggle free-draw' menus

- Added dynamic Darkness/Brightness text parsing to UI and CSV export
2026-03-11 14:48:24 +01:00
lm acfcf99d15 Feature: Settings Import/Export and UI Polish
- Implemented JSON-based settings import/export with smart scaling

- Locked image overlay color to Red (#ff0000) permanently

- Decoupled analyzer target color from display mask color

- Added menu separators for better organization

- Fixed overlay synchronization and scaling bugs during import
2026-03-10 18:32:14 +01:00
lm c278ddf458 Optimize: export speed, fix scaling bug, and improve workflow
- Parallelized folder export for massive speedup

- Implemented exclusion mask caching

- Fixed statistics discrepancy by scaling exclusion coordinates

- Hardcoded CSV format to semicolon separator and comma decimal

- Defaulted file/folder dialogs to images/ directory

- Added unit test for coordinate scaling
2026-03-10 17:59:49 +01:00
lm 49b436a2f6 Update .gitignore 2026-03-10 17:33:42 +01:00
lm ac79d0e5dc Feature: add pattern scraper and optimize batch export
- Added Pull Pattern Images tool with parallel background downloading

- Optimized Export Folder Stats to run headlessly (massive speedup)

- Dynamically name exported CSVs based on source folder

- Fixed German CSV localization and UTF-8 BOM

- Updated README and walkthrough
2026-03-10 17:28:15 +01:00
lm 635b65b7e1 Docs: rewrite README.md for complete PySide6 transition 2026-03-10 16:56:01 +01:00
lm 551f5a6b8f Replace toolbar with categorized menu system, standardize 'color', and improve CSV export
- Migrated toolbar to QMenuBar to fix UI crowding

- Categorized actions into File, Edit, Tools, and View

- Added dynamic theming to QMenuBar and QMenu

- Localized Export CSV delimiter and decimals for German Excel

- Padded exported CSV values for clean plain-text alignment

- Globally standardized the spelling of 'color'

- Removed duplicate code and old Tkinter codebase
2026-03-10 16:54:23 +01:00
lm 95907d6314 Merge pyside6-migration into master 2026-03-10 16:09:23 +01:00
lm 78af24103c Improve performance, UX, and code quality
- Replace Python pixel loop with NumPy vectorized HSV processing (~50-100x faster overlay rebuilds)
- Add OverlayCanvas (QGraphicsView) to replace plain QLabel overlay panel
- Add editable QLineEdit value input to SliderControl (supports typing exact values)
- Implement eyedropper pick-from-image: left-click sets hue/sat/val sliders from pixel colour
- Add keyboard shortcuts: Ctrl+O, Ctrl+Shift+O, Ctrl+S, Ctrl+Z, Ctrl+R, arrow keys, Esc
- Add drag-and-drop support for image files and folders
- Persist and restore window geometry via QSettings
- Remove duplicate _CONFIG_DATA load in constants.py
- Remove dead constructor stylesheet from ToolbarButton
- Add numpy>=1.26 dependency
2026-03-10 16:07:52 +01:00
lm 2d4531013d Fix folder navigation and restyle navigation arrows 2025-10-19 21:16:55 +02:00
lm 6de4059291 Respect excluded regions in overlay rendering 2025-10-19 21:09:26 +02:00
lm 0fd527cd78 Render exclusion outlines on overlay preview 2025-10-19 20:05:11 +02:00
lm b882e5d751 Maximize on primary screen 2025-10-19 20:01:07 +02:00
lm fb9d6ebee6 Open maximized and refine toolbar layout 2025-10-19 19:59:18 +02:00
lm 36879dbb86 Fix toolbar action wiring 2025-10-19 19:55:55 +02:00
lm 57bb896545 Enable exclusion drawing in PySide6 UI 2025-10-19 19:52:41 +02:00
lm f46af5a735 Fix processor initialisation order 2025-10-19 19:38:51 +02:00
lm 213b35bf20 Reintroduce translations and theme handling 2025-10-19 19:31:50 +02:00
lm 91bdf37512 Propagate configuration into PySide6 launcher 2025-10-19 19:25:12 +02:00
lm 0ca5607fc7 Wire PySide6 UI to new image processor 2025-10-19 19:23:01 +02:00
lm 825bdcebe0 Recreate legacy controls in PySide6 window 2025-10-19 19:18:20 +02:00
lm 9ded332269 Introduce PySide6 desktop shell 2025-10-19 19:10:11 +02:00
lm f09da5018f Simplify borderless setup to fix input handling 2025-10-19 18:53:47 +02:00
lm 8053dc297a Keep custom title bar while preserving responsiveness 2025-10-19 18:51:58 +02:00
lm f65c37407c Reinstate always-borderless window setup 2025-10-19 18:48:49 +02:00
lm 3e6650eb2e Ensure Windows borderless window registers as taskbar app 2025-10-19 18:47:46 +02:00
lm 2bf9776076 Reapply Windows borderless styling without hiding taskbar entry 2025-10-19 18:44:49 +02:00
lm 1984145043 Force borderless window to appear in taskbar 2025-10-19 18:40:38 +02:00
lm 8ed1acc32d Restore custom title bar without native chrome 2025-10-19 18:38:13 +02:00
lm 183b769000 Revert "Restore native title bar with theme-aware styling"
This reverts commit 07d7679889.
2025-10-19 18:36:27 +02:00
lm 07d7679889 Restore native title bar with theme-aware styling 2025-10-19 18:34:28 +02:00
lm 01a9b2707c Implement native borderless window integration 2025-10-19 18:26:36 +02:00
lm f2db62036b Redo borderless window styling to keep taskbar entry 2025-10-19 18:21:17 +02:00
lm fb37da7e40 Ensure taskbar entry for borderless window 2025-10-19 18:18:45 +02:00
lm 50b9aa723c Improve maximize behaviour on multi-monitor setups 2025-10-19 18:14:40 +02:00
lm 27c0e55711 Upscale small previews to fill canvas 2025-10-19 18:10:24 +02:00
lm f467e0b2e5 Enhance window controls
Add minimize/maximize buttons, double-click maximize behaviour, and proper window state handling for the custom title bar.
2025-10-19 18:03:07 +02:00
lm 362383faf6 Sync README icons
Update the workflow emoji to match the toolbar’s new framed-picture icon for single-image loading.
2025-10-17 17:40:11 +02:00
lm a30eb997b1 Swap open-image icon
Replace the puzzle glyph with a framed picture to better represent single-image loading.
2025-10-17 17:39:23 +02:00
lm 98015a40d8 Update toolbar icons
Use a puzzle piece for single-image loading and reuse the open-folder glyph for folder loading.
2025-10-17 17:38:05 +02:00
lm 1aaef87790 Clarify activation note
Break out the macOS/Linux venv activation instruction onto its own line.
2025-10-17 17:36:50 +02:00
45 changed files with 4335 additions and 1814 deletions
+3
View File
@@ -156,3 +156,6 @@ uv/
*.merge_file_* *.merge_file_*
.git/modules/ .git/modules/
.git/worktrees/ .git/worktrees/
# ICRA specific
analyses/
+676
View File
@@ -0,0 +1,676 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The GNU General Public License is a free, copyleft license for
software and other kinds of works.
The licenses for most software and other practical works are designed
to take away your freedom to share and change the works. By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users. We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors. You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you can receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.
To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights. Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received. You must make sure that they, too, receive
or can get the source code. And you must show them these terms so they
know their rights.
Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.
For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software. For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.
Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so. This is fundamentally incompatible with the aim of
protecting users' freedom to change the software. The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable. Therefore, we
have designed this version of the GPL to prohibit the practice for those
products. If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.
Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary. To prevent this, the GPL assures that
patents cannot be used to render the program non-free.
The precise terms and conditions for copying, distribution and
modification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU General Public License.
"Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under this
License. Each licensee is addressed as "you". "Licensees" and
"recipients" may be individuals or organizations.
To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy. The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work based
on the Program.
To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy. Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables other
parties to make or receive copies. Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.
An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License. If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the work
for making modifications to it. "Object code" means any non-source
form of a work.
A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.
The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Component, but which is not part of that Component, and
(b) serves only to enable use of the work with that Component, or to
implement a Standard Interface for which an implementation is
available to the public in source code form. A "Component" in this
context means a major essential component (kernel, window system, and
so on) of the specific operating system (if any) on which the
executable work runs, or a compiler used to produce the work, or an
object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities. However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work. For example, the Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.
The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.
The Corresponding Source for a work in source code form is that
same work.
2. Basic Permissions.
All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met. This License explicitly affirms your unlimited
permission to run the unmodified Program. The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work. This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force. You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright. Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely under
the conditions stated below. Sublicensing is not allowed; section 10
makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.
When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accordance with section 7 apply to the
code; keep intact all notices of the absence of any warranty; and
give all recipients a copy of this License along with the Program.
You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.
5. Conveying Modified Source Versions.
You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:
a) The work must carry prominent notices stating that you modified
it, and giving a relevant date.
b) The work must carry prominent notices stating that it is
released under this License and any conditions added under
section 7. This notice modifies the requirement in section 4 to
"keep intact all notices".
c) You must license the entire work, as a whole, under this
License to anyone who comes into possession of a copy. This
License will therefore apply, along with any applicable section 7
additional terms, to the whole of the work, and all its parts,
regardless of how they are packaged. This License gives no
permission to license the work in any other way, but it does not
invalidate such permission if you have separately received it.
d) If the work has interactive user interfaces, each must display
Appropriate Legal Notices; however, if the Program has
interactive interfaces that do not display Appropriate Legal
Notices, your work need not make them do so.
A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit. Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:
a) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by the
Corresponding Source fixed on a durable physical medium
customarily used for software interchange.
b) Convey the object code in, or embodied in, a physical product
(including a physical distribution medium), accompanied by a
written offer, valid for at least three years and valid for as
long as you offer spare parts or customer support for that
product model, to give anyone who possesses the object code
either (1) a copy of the Corresponding Source for all the
software in the product that is covered by this License, on a
durable physical medium customarily used for software
interchange, for a price no more than your reasonable cost of
physically performing this conveying of source, or (2) access to
copy the Corresponding Source from a network server at no charge.
c) Convey individual copies of the object code with a copy of the
written offer to provide the Corresponding Source. This
alternative is allowed only occasionally and noncommercially, and
only if you received the object code with such an offer, in
accord with subsection 6b.
d) Convey the object code by offering access from a designated
place (gratis or for a fee), and offer equivalent access to the
Corresponding Source in the same way through a different server
at no charge. You need not require recipients to copy the
Corresponding Source along with the object code. If the place to
copy the object code is a network server, the Corresponding
Source may be on a different server (operated by you or a third
party) that supports equivalent copying facilities, provided you
maintain clear directions next to the object code saying where
the Corresponding Source is located. Regardless of what server
hosts the Corresponding Source, you remain obligated to ensure
that it is available for as long as needed to satisfy these
requirements.
e) Convey the object code using peer-to-peer transmission, provided
you inform other peers where the object code and Corresponding
Source of the work are being offered to the general public at no
charge under subsection 6d.
A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling. In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage. For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product. A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.
"Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source. The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.
If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information. But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).
The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed. Access to
a network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law. If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it. (Additional permissions may be written to require their own
removal in certain cases when you modify the work.) You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.
Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:
a) Disclaiming warranty or limiting liability differently from the
terms of sections 15 and 16 of this License; or
b) Requiring preservation of specified reasonable legal notices or
author attributions in that material or in the Appropriate Legal
Notices displayed by works containing it; or
c) Prohibiting misrepresentation of the origin of that material, or
requiring that modified versions of such material be marked in
reasonable ways as different from the original version; or
d) Limiting the use for publicity purposes of names of licensors or
authors of the material; or
e) Declining to grant rights under trademark law for use of some
trade names, trademarks, or service marks; or
f) Requiring indemnification of licensors and authors of that
material by anyone who conveys the material (or modified versions of
it) with contractual assumptions of liability to the recipient, for
any liability that these contractual assumptions directly impose on
those licensors and authors.
All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10. If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term. If a license document contains
a further restriction but permits relicensing or propagation under
this License, you may add to a covered work material governed by the
terms of that license document, provided that the further restriction
does not survive such relicensing or propagation.
If you add terms to a covered work in accordance with this section,
you must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply in either case.
8. Termination.
You may not propagate or modify a covered work except as expressly
provided under this License. Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).
However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.
Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.
Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License. If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive or
run a copy of the Program. Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance. However,
nothing other than this License grants you permission to propagate or
modify any covered work. These actions infringe copyright if you do
not accept this License. Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.
10. Automatic Licensing of Downstream Recipients.
Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License. You are not responsible
for enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations. If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License. For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based. The
work thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version. For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.
Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement). To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.
If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
network server or other readily accessible means, then you must either
(1) cause the Corresponding Source to be so available, or (2) arrange
to deprive yourself of the benefit of the patent license for this
particular work, or (3) arrange, in a manner consistent with the
requirements of this License, to extend the patent license to
downstream recipients. "Knowingly relying" means you have actual
knowledge that, but for the patent license, your conveying the covered
work in a country, or your recipient's use of the covered work in a
country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.
A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License. You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.
Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.
12. No Surrender of Others' Freedom.
If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not convey it at all. For example, if you agree to terms that
obligate you to collect a royalty for further conveying from those to
whom you convey the Program, the only way you could satisfy both those
terms and this License would be to refrain entirely from conveying the
Program.
13. Use with the GNU Affero General Public License.
Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work. The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions
of the GNU General Public License from time to time. Such new versions
will be similar in spirit to the present version, but may differ in
detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation. If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.
If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.
Later license versions may give you additional or different
permissions. However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU
ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING
ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF
THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO
LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY
OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF
THE POSSIBILITY OF SUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
state the absence of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:
<program> Copyright (C) <year> <name of author>
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications
with the library. If this is what you want to do, use the GNU Lesser
General Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.
+73 -31
View File
@@ -1,57 +1,99 @@
<div style="display:flex; gap:16px; align-items:center;"> <div style="display:flex; gap:16px; align-items:center;">
<img src="app/assets/logo.png" alt="ICRA" width="140"/> <img src="app/assets/logo.png" alt="ICRA" width="140"/>
<div> <div>
<strong>Interactive Color Range Analyzer</strong> is a Tkinter-based desktop tool for highlighting customised colour ranges in images.<br/> <strong>Interactive Color Range Analyzer (ICRA)</strong><br/>
Load a single photo or an entire folder, fine-tune hue/saturation/value sliders, and export overlays complete with quick statistics. A professional desktop application for high-precision color matching, clustering analysis, and batch statistics generation.
</div> </div>
</div> </div>
## Features ## Features
- Two synced previews (original + overlay) - **High-Performance Image Processing:** Native, vectorized NumPy operations for lightning-fast HSV conversion and color matching.
- Hue/Sat/Value sliders with presets and image colour picker - **Automatic Background Exclusion:** Intelligently ignores background pixels (configurable in `config.toml`) to ensure they don't interfere with your analysis.
- Exclusion rectangles or freehand polygons that persist while browsing - **Grouping Score (Clustering):** A high-performance 9x9 box-sum algorithm that rewards solid "splashes" of color and penalizes thin lines or fragmented noise.
- Theme toggle (light/dark) with rounded toolbar buttons and accent-aware highlights - **Batch Processing & Customizable Export:** Load a folder of images and instantly export `icra_stats.csv` and `icra_settings.json`. Features a dedicated **Weighting Dialog** to customize the impact of each core component, outputting precise percentage-based column headers.
- Folder support with wrap-around previous/next navigation - **Import/Export Settings:** Save your HSV ranges, exclusion zones, weighting preferences, and custom overlay colors to a JSON file and reload them later for consistent analysis across different sessions.
- Quick overlay export (PNG) with configurable defaults and language settings via `config.toml` - **Eyedropper Tool:** Quickly pick target matching colors directly from the image canvas.
- **Advanced Selection:** Support for exclusion zones (rectangles and free-draw polygons) overlaid on the image, all dynamically rendered via `QGraphicsView`.
- **Modern UI & UX:**
- Drag-and-drop support for files and folders.
- Custom dark and light themes with native-feeling borderless titlebars.
- Context-aware hover tooltips across the cleanly categorized menu bar.
- Window size and position persistence between launches.
- **Configurable:** Uses `config.toml` to drive everything from overlay matching colors to default sliders and application language (`en`/`de`).
## Core Concepts
### 1. HSV Color Matching
Instead of simple RGB, ICRA uses the **HSV (Hue, Saturation, Value)** color space. This allows for more intuitive color selection:
- **Hue (0-360°):** The base "color" (Red, Green, Blue, etc.).
- **Saturation (0-100%):** The intensity or "vibrancy".
- **Value (0-100%):** The brightness.
Matching is performed by checking if each pixel's HSV values fall within your defined ranges.
### 2. Scoring Components
The **Composite Score** is calculated weighted by your preferences:
- **Match (Keep):** Percentage of matching pixels *after* excluding your manual shapes and background areas.
- **Match (All):** Percentage of matching pixels across the entire image.
- **Brightness Score:** Calculated from the average brightness of the "Keep" areas. If "Prefer Dark" is ON, lower brightness results in a higher score.
- **Grouping Score:** Uses a 9x9 box-sum density check. Higher values indicate that matching pixels are clustered into solid blocks rather than scattered noise.
### 3. Background Filtering
To ensure accurate statistics, ICRA automatically filters out the image background (e.g., the dark grey/black backdrop in weapon screenshots). These settings are fully configurable in `config.toml`, allowing you to adjust the target color and the tolerance required for exclusion.
### 4. Exclusion Zones
Use the **Exclusion Tool** to draw rectangles or polygons over areas you want to ignore. This is essential for focusing your analysis on specific parts of a complex image while ignoring background noise or irrelevant details.
### 5. Configuration (`config.toml`)
The application is highly customizable via `config.toml`. This file is included in the repository and contains detailed comments explaining each available setting—from default sliders and language selection to UI masking colors and initial background exclusion tolerances.
## Typical Workflow
1. **Load Data:** Drag and drop a folder or use the `File` menu to load your images.
2. **Pick Color:** Use the Eyedropper tool to select your target color directly from the image.
3. **Refine HSV:** Fine-tune the Hue, Saturation, and Value sliders to perfect the mask.
4. **Draw Exclusions:** Add exclusion shapes to ignore parts of the image that shouldn't be counted.
5. **Export:** Click `Export Folder Stats`. Define your weights (e.g., prioritize the Grouping Score if you want coherent splashes) and save your results.
6. **Persistent Settings:** Use `File -> Export Settings` to save your configuration, or `File -> Import Settings` to restore a previous setup.
## Requirements ## Requirements
- Python 3.11+ (3.10 works with `tomli`) - Python 3.11+
- [uv](https://github.com/astral-sh/uv) for dependency management - [uv](https://github.com/astral-sh/uv) for dependency management
- Tkinter (install separately on some Linux distros) - Works across Windows, macOS, and Linux (requires PySide6 and numpy)
## Setup with uv (Windows PowerShell) ## Setup with uv
```bash ```bash
git clone https://git.lukasmahler.de/lm/ICRA.git git clone https://git.lukasmahler.de/lm/ICRA.git
cd ICRA cd ICRA
uv venv uv venv
source .venv/Scripts/activate source .venv/Scripts/activate # macOS/Linux: source .venv/bin/activate
uv pip install . uv pip install .
uv run icra uv run icra
``` ```
The launcher copies Tcl/Tk resources into the virtualenv on first run, so no manual environment tweaks are needed. On macOS/Linux replace the activate step with `source .venv/bin/activate`.
## Workflow ## Running the Test Suite
1. Load an image (`📂`) or a folder (`📁`). The core image processing logic and UI data models are rigorously tested using `pytest`.
2. Pick a colour (`🎨` dialog, `🖱️` image click, or preset swatch). ```bash
3. Finetune sliders; watch the overlay update on the right. uv run pytest tests/ -v
4. Toggle freehand mode (`△`) or stick with rectangles and mark areas to exclude (right mouse drag). ```
5. Move through folder images with `⬅️` / `➡️`; exclusions stay put unless you opt into automatic resets.
6. Save an overlay (`💾`) when ready.
## Project Layout ## Project Layout
``` ```
app/ app/
app.py # main app assembly assets/ # Icons and branding
gui/ # UI, theme, picker mixins lang/ # Translations (TOML)
logic/ # image ops, defaults, config helpers qt/ # UI implementation and NumPy processing
lang/ # localisation TOML files tests/ # Unit tests for core analysis
config.toml # optional defaults config.toml # User-facing configuration
main.py # entry point main.py # Entry point
LICENSE # GNU GPLv3 terms
``` ```
## Localisation
- English and German translations ship in `app/lang`. Set the desired language via the top-level `language` key in `config.toml`.
## Development ## Development
- Quick check: `uv run python -m compileall app main.py` - **Tests**: Run `uv run pytest tests/ -v` to verify the core logic.
- Contributions welcome; include screenshots for UI tweaks. - **Environment**: Managed via `uv`. Run `uv sync` to ensure your environment matches the lockfile.
## Contributing
Contributions are welcome! Open an issue or submit a pull request.
## License
GPLv3 License. See the LICENSE file for details.
+7 -3
View File
@@ -1,5 +1,9 @@
"""Application package.""" """Application package exposing the PySide6 entry points."""
from .app import ICRAApp, start_app from __future__ import annotations
__all__ = ["ICRAApp", "start_app"] from .qt import create_application as create_qt_app, run as run_qt_app
start_app = run_qt_app
__all__ = ["create_qt_app", "run_qt_app", "start_app"]
-90
View File
@@ -1,90 +0,0 @@
"""Application composition root."""
from __future__ import annotations
import tkinter as tk
from .gui import ColorPickerMixin, ExclusionMixin, ThemeMixin, UIBuilderMixin
from .i18n import I18nMixin
from .logic import DEFAULTS, LANGUAGE, RESET_EXCLUSIONS_ON_IMAGE_CHANGE, ImageProcessingMixin, ResetMixin
class ICRAApp(
I18nMixin,
ThemeMixin,
UIBuilderMixin,
ImageProcessingMixin,
ExclusionMixin,
ColorPickerMixin,
ResetMixin,
):
"""Tkinter based application for isolating configurable colour ranges."""
def __init__(self, root: tk.Tk):
self.root = root
self.init_i18n(LANGUAGE)
self.root.title(self._t("app.title"))
self._setup_window()
# Theme and styling
self.init_theme()
# Tkinter state variables
self.DEFAULTS = DEFAULTS.copy()
self.hue_min = tk.DoubleVar(value=self.DEFAULTS["hue_min"])
self.hue_max = tk.DoubleVar(value=self.DEFAULTS["hue_max"])
self.sat_min = tk.DoubleVar(value=self.DEFAULTS["sat_min"])
self.val_min = tk.DoubleVar(value=self.DEFAULTS["val_min"])
self.val_max = tk.DoubleVar(value=self.DEFAULTS["val_max"])
self.alpha = tk.IntVar(value=self.DEFAULTS["alpha"])
self.ref_hue = None
# Debounce for heavy preview updates
self.update_delay_ms = 400
self._update_job = None
# Exclusion rectangles (preview coordinates)
self.exclude_shapes: list[dict[str, object]] = []
self._rubber_start = None
self._rubber_id = None
self._stroke_preview_id = None
self.exclude_mode = "rect"
self.reset_exclusions_on_switch = RESET_EXCLUSIONS_ON_IMAGE_CHANGE
self._exclude_mask = None
self._exclude_mask_dirty = True
self._exclude_mask_px = None
self._exclude_canvas_ids: list[int] = []
self._current_stroke: list[tuple[int, int]] | None = None
self.free_draw_width = 14
self.pick_mode = False
# Image references
self.image_path = None
self.orig_img = None
self.preview_img = None
self.preview_tk = None
self.overlay_tk = None
self.image_paths = []
self.current_image_index = -1
# Build UI
self.setup_ui()
self._init_copy_menu()
self.bring_to_front()
def _setup_window(self) -> None:
self.root.overrideredirect(True)
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
self.root.geometry(f"{screen_width}x{screen_height}+0+0")
self.root.configure(bg="#f2f2f7")
def start_app() -> None:
"""Entry point used by the CLI script."""
root = tk.Tk()
app = ICRAApp(root)
root.mainloop()
__all__ = ["ICRAApp", "start_app"]
-13
View File
@@ -1,13 +0,0 @@
"""GUI-related mixins and helpers for the application."""
from .color_picker import ColorPickerMixin
from .exclusions import ExclusionMixin
from .theme import ThemeMixin
from .ui import UIBuilderMixin
__all__ = [
"ColorPickerMixin",
"ExclusionMixin",
"ThemeMixin",
"UIBuilderMixin",
]
-158
View File
@@ -1,158 +0,0 @@
"""Color selection utilities."""
from __future__ import annotations
import colorsys
from tkinter import colorchooser, messagebox
class ColorPickerMixin:
"""Handles colour selection from dialogs and mouse clicks."""
ref_hue: float | None
hue_span: float = 45.0 # degrees around the picked hue
selected_colour: tuple[int, int, int] | None = None
def choose_color(self):
title = self._t("dialog.choose_colour_title") if hasattr(self, "_t") else "Choose colour"
rgb, hex_colour = colorchooser.askcolor(title=title)
if rgb is None:
return
r, g, b = (int(round(channel)) for channel in rgb)
hue_deg, sat_pct, val_pct = self._apply_rgb_selection(r, g, b)
label = hex_colour or f"RGB({r}, {g}, {b})"
message = self._t(
"status.color_selected",
label=label,
hue=hue_deg,
saturation=sat_pct,
value=val_pct,
)
self.status.config(text=message)
self._update_selected_colour(r, g, b)
def apply_sample_colour(self, hex_colour: str, name: str | None = None) -> None:
"""Apply a predefined colour preset."""
rgb = self._parse_hex_colour(hex_colour)
if rgb is None:
return
hue_deg, sat_pct, val_pct = self._apply_rgb_selection(*rgb)
if self.pick_mode:
self.pick_mode = False
label = name or hex_colour.upper()
message = self._t(
"status.sample_colour",
label=label,
hex_code=hex_colour,
hue=hue_deg,
saturation=sat_pct,
value=val_pct,
)
self.status.config(text=message)
self._update_selected_colour(*rgb)
def enable_pick_mode(self):
if self.preview_img is None:
messagebox.showinfo(
self._t("dialog.info_title"),
self._t("dialog.load_image_first"),
)
return
self.pick_mode = True
self.status.config(text=self._t("status.pick_mode_ready"))
def disable_pick_mode(self, event=None):
if self.pick_mode:
self.pick_mode = False
self.status.config(text=self._t("status.pick_mode_ended"))
def on_canvas_click(self, event):
if not self.pick_mode or self.preview_img is None:
return
x = int(event.x)
y = int(event.y)
if x < 0 or y < 0 or x >= self.preview_img.width or y >= self.preview_img.height:
return
r, g, b, a = self.preview_img.getpixel((x, y))
if a == 0:
return
hue_deg, sat_pct, val_pct = self._apply_rgb_selection(r, g, b)
self.disable_pick_mode()
self.status.config(
text=self._t(
"status.pick_mode_from_image",
hue=hue_deg,
saturation=sat_pct,
value=val_pct,
)
)
self._update_selected_colour(r, g, b)
def _apply_rgb_selection(self, r: int, g: int, b: int) -> tuple[float, float, float]:
"""Update slider ranges based on an RGB colour and return HSV summary."""
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
hue_deg = (h * 360.0) % 360.0
self.ref_hue = hue_deg
self._set_slider_targets(hue_deg, s, v)
self.update_preview()
return hue_deg, s * 100.0, v * 100.0
def _update_selected_colour(self, r: int, g: int, b: int) -> None:
self.selected_colour = (r, g, b)
hex_colour = f"#{r:02x}{g:02x}{b:02x}"
if hasattr(self, "current_colour_sw"):
try:
self.current_colour_sw.configure(background=hex_colour)
except Exception:
pass
if hasattr(self, "current_colour_label"):
try:
self.current_colour_label.configure(text=f"({hex_colour})")
except Exception:
pass
def _set_slider_targets(self, hue_deg: float, saturation: float, value: float) -> None:
span = getattr(self, "hue_span", 45.0)
self.hue_min.set((hue_deg - span) % 360)
self.hue_max.set((hue_deg + span) % 360)
sat_pct = saturation * 100.0
sat_margin = 35.0
sat_min = max(0.0, min(100.0, sat_pct - sat_margin))
if saturation <= 0.05:
sat_min = 0.0
self.sat_min.set(sat_min)
v_pct = value * 100.0
val_margin = 35.0
val_min = max(0.0, v_pct - val_margin)
val_max = min(100.0, v_pct + val_margin)
if value <= 0.15:
val_max = min(45.0, max(val_max, 25.0))
if value >= 0.85:
val_min = max(55.0, min(val_min, 80.0))
if val_max <= val_min:
val_max = min(100.0, val_min + 10.0)
self.val_min.set(val_min)
self.val_max.set(val_max)
@staticmethod
def _parse_hex_colour(hex_colour: str | None) -> tuple[int, int, int] | None:
if not hex_colour:
return None
value = hex_colour.strip().lstrip("#")
if len(value) == 3:
value = "".join(ch * 2 for ch in value)
if len(value) != 6:
return None
try:
r = int(value[0:2], 16)
g = int(value[2:4], 16)
b = int(value[4:6], 16)
except ValueError:
return None
return r, g, b
__all__ = ["ColorPickerMixin"]
-207
View File
@@ -1,207 +0,0 @@
"""Mouse handlers for exclusion shapes."""
from __future__ import annotations
class ExclusionMixin:
"""Manage exclusion shapes (rectangles and freehand strokes) on the preview canvas."""
def _exclude_start(self, event):
if self.preview_img is None:
return
mode = getattr(self, "exclude_mode", "rect")
x = max(0, min(self.preview_img.width - 1, int(event.x)))
y = max(0, min(self.preview_img.height - 1, int(event.y)))
if mode == "free":
self._current_stroke = [(x, y)]
preview_id = getattr(self, "_stroke_preview_id", None)
if preview_id:
try:
self.canvas_orig.delete(preview_id)
except Exception:
pass
accent = self._exclusion_preview_colour()
self._stroke_preview_id = self.canvas_orig.create_line(
x,
y,
x,
y,
fill=accent,
width=2,
smooth=True,
capstyle="round",
joinstyle="round",
)
self._rubber_start = None
return
self._rubber_start = (x, y)
if self._rubber_id:
try:
self.canvas_orig.delete(self._rubber_id)
except Exception:
pass
accent = self._exclusion_preview_colour()
self._rubber_id = self.canvas_orig.create_rectangle(x, y, x, y, outline=accent, width=2)
def _exclude_drag(self, event):
mode = getattr(self, "exclude_mode", "rect")
if mode == "free":
stroke = getattr(self, "_current_stroke", None)
if not stroke:
return
x = max(0, min(self.preview_img.width - 1, int(event.x)))
y = max(0, min(self.preview_img.height - 1, int(event.y)))
if stroke[-1] != (x, y):
stroke.append((x, y))
preview_id = getattr(self, "_stroke_preview_id", None)
if preview_id:
coords = [coord for point in stroke for coord in point]
self.canvas_orig.coords(preview_id, *coords)
return
if not self._rubber_start:
return
x0, y0 = self._rubber_start
x1 = max(0, min(self.preview_img.width - 1, int(event.x)))
y1 = max(0, min(self.preview_img.height - 1, int(event.y)))
self.canvas_orig.coords(self._rubber_id, x0, y0, x1, y1)
def _exclude_end(self, event):
mode = getattr(self, "exclude_mode", "rect")
if mode == "free":
stroke = getattr(self, "_current_stroke", None)
if stroke and len(stroke) > 2:
polygon = self._close_polygon(self._compress_stroke(stroke))
if len(polygon) >= 3:
shape = {
"kind": "polygon",
"points": polygon,
}
self.exclude_shapes.append(shape)
stamper = getattr(self, "_stamp_shape_on_mask", None)
if callable(stamper):
stamper(shape)
else:
self._exclude_mask_dirty = True
self._current_stroke = None
preview_id = getattr(self, "_stroke_preview_id", None)
if preview_id:
try:
self.canvas_orig.delete(preview_id)
except Exception:
pass
self._stroke_preview_id = None
self.update_preview()
return
if not self._rubber_start:
return
x0, y0 = self._rubber_start
x1 = max(0, min(self.preview_img.width - 1, int(event.x)))
y1 = max(0, min(self.preview_img.height - 1, int(event.y)))
rx0, rx1 = sorted((x0, x1))
ry0, ry1 = sorted((y0, y1))
if (rx1 - rx0) > 0 and (ry1 - ry0) > 0:
shape = {"kind": "rect", "coords": (rx0, ry0, rx1, ry1)}
self.exclude_shapes.append(shape)
stamper = getattr(self, "_stamp_shape_on_mask", None)
if callable(stamper):
stamper(shape)
else:
self._exclude_mask_dirty = True
if self._rubber_id:
try:
self.canvas_orig.delete(self._rubber_id)
except Exception:
pass
self._rubber_start = None
self._rubber_id = None
self.update_preview()
def clear_excludes(self):
self.exclude_shapes = []
self._rubber_start = None
self._current_stroke = None
if self._rubber_id:
try:
self.canvas_orig.delete(self._rubber_id)
except Exception:
pass
self._rubber_id = None
if self._stroke_preview_id:
try:
self.canvas_orig.delete(self._stroke_preview_id)
except Exception:
pass
self._stroke_preview_id = None
for item in getattr(self, "_exclude_canvas_ids", []):
try:
self.canvas_orig.delete(item)
except Exception:
pass
self._exclude_canvas_ids = []
self._exclude_mask = None
self._exclude_mask_px = None
self._exclude_mask_dirty = True
self.update_preview()
def undo_exclude(self):
if not getattr(self, "exclude_shapes", None):
return
self.exclude_shapes.pop()
self._exclude_mask_dirty = True
self.update_preview()
def toggle_exclusion_mode(self):
current = getattr(self, "exclude_mode", "rect")
next_mode = "free" if current == "rect" else "rect"
self.exclude_mode = next_mode
self._current_stroke = None
if next_mode == "free":
if self._rubber_id:
try:
self.canvas_orig.delete(self._rubber_id)
except Exception:
pass
self._rubber_id = None
self._rubber_start = None
else:
if self._stroke_preview_id:
try:
self.canvas_orig.delete(self._stroke_preview_id)
except Exception:
pass
self._stroke_preview_id = None
self._rubber_id = None
message_key = "status.free_draw_enabled" if next_mode == "free" else "status.free_draw_disabled"
if hasattr(self, "status"):
try:
self.status.config(text=self._t(message_key))
except Exception:
pass
@staticmethod
def _compress_stroke(points: list[tuple[int, int]]) -> list[tuple[int, int]]:
"""Reduce duplicate points without altering the drawn path too much."""
if not points:
return []
compressed: list[tuple[int, int]] = [points[0]]
for point in points[1:]:
if point != compressed[-1]:
compressed.append(point)
return compressed
def _exclusion_preview_colour(self) -> str:
is_dark = getattr(self, "theme", "light") == "dark"
return "#ffd700" if is_dark else "#c56217"
@staticmethod
def _close_polygon(points: list[tuple[int, int]]) -> list[tuple[int, int]]:
"""Ensure the polygon is closed by repeating the start if necessary."""
if len(points) < 3:
return points
closed = list(points)
if closed[0] != closed[-1]:
closed.append(closed[0])
return closed
__all__ = ["ExclusionMixin"]
-106
View File
@@ -1,106 +0,0 @@
"""Theme and window helpers."""
from __future__ import annotations
import platform
from tkinter import ttk
try:
import winreg
except Exception: # pragma: no cover - platform-specific
winreg = None # type: ignore
class ThemeMixin:
"""Provides theme handling utilities for the main application."""
theme: str
style: ttk.Style
scale_style: str
def init_theme(self) -> None:
"""Initialise ttk style handling and apply the detected theme."""
self.style = ttk.Style()
self.style.theme_use("clam")
self.theme = "light"
self.apply_theme(self.detect_system_theme())
def apply_theme(self, mode: str) -> None:
"""Apply light/dark theme including widget palette."""
mode = (mode or "light").lower()
self.theme = "dark" if mode == "dark" else "light"
self.scale_style = "Horizontal.TScale"
if self.theme == "dark":
bg, fg = "#0f0f10", "#f1f1f1"
status_fg = "#f5f5f5"
highlight_fg = "#f2c744"
else:
bg, fg = "#ffffff", "#202020"
status_fg = "#1c1c1c"
highlight_fg = "#c56217"
self.root.configure(bg=bg) # type: ignore[attr-defined]
s = self.style
s.configure("TFrame", background=bg)
s.configure("TLabel", background=bg, foreground=fg, font=("Segoe UI", 10))
s.configure(
"TButton", padding=8, relief="flat", background="#e0e0e0", foreground=fg, font=("Segoe UI", 10)
)
s.map("TButton", background=[("active", "#d0d0d0")])
button_refresher = getattr(self, "_refresh_toolbar_buttons_theme", None)
if callable(button_refresher):
button_refresher()
nav_refresher = getattr(self, "_refresh_navigation_buttons_theme", None)
if callable(nav_refresher):
nav_refresher()
status_refresher = getattr(self, "_refresh_status_palette", None)
if callable(status_refresher) and hasattr(self, "status"):
status_refresher(status_fg)
accent_refresher = getattr(self, "_refresh_accent_labels", None)
if callable(accent_refresher) and hasattr(self, "filename_label"):
accent_refresher(highlight_fg)
canvas_refresher = getattr(self, "_refresh_canvas_backgrounds", None)
if callable(canvas_refresher):
canvas_refresher()
def detect_system_theme(self) -> str:
"""Best-effort detection of the OS theme preference."""
try:
if platform.system() == "Windows" and winreg is not None:
key = winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize",
)
value, _ = winreg.QueryValueEx(key, "AppsUseLightTheme")
return "light" if int(value) == 1 else "dark"
except Exception:
pass
return "light"
def bring_to_front(self) -> None:
"""Try to focus the window and raise it to the foreground."""
try:
self.root.lift()
self.root.focus_force()
self.root.attributes("-topmost", True)
self.root.update()
self.root.attributes("-topmost", False)
except Exception:
pass
def toggle_theme(self) -> None:
"""Toggle between light and dark themes."""
next_mode = "dark" if self.theme == "light" else "light"
self.apply_theme(next_mode)
self.update_preview() # type: ignore[attr-defined]
__all__ = ["ThemeMixin"]
-604
View File
@@ -1,604 +0,0 @@
"""UI helpers and reusable Tk callbacks."""
from __future__ import annotations
import colorsys
import tkinter as tk
import tkinter.font as tkfont
from tkinter import ttk
class UIBuilderMixin:
"""Constructs the Tkinter UI and common widgets."""
def setup_ui(self) -> None:
self._create_titlebar()
toolbar = ttk.Frame(self.root)
toolbar.pack(fill=tk.X, padx=12, pady=(4, 2))
buttons = [
("📂", self._t("toolbar.open_image"), self.load_image),
("📁", self._t("toolbar.open_folder"), self.load_folder),
("🎨", self._t("toolbar.choose_color"), self.choose_color),
("🖱", self._t("toolbar.pick_from_image"), self.enable_pick_mode),
("💾", self._t("toolbar.save_overlay"), self.save_overlay),
("", self._t("toolbar.toggle_free_draw"), self.toggle_exclusion_mode),
("🧹", self._t("toolbar.clear_excludes"), self.clear_excludes),
("", self._t("toolbar.undo_exclude"), self.undo_exclude),
("🔄", self._t("toolbar.reset_sliders"), self.reset_sliders),
("🌓", self._t("toolbar.toggle_theme"), self.toggle_theme),
]
self._toolbar_buttons: list[dict[str, object]] = []
self._nav_buttons: list[tk.Button] = []
buttons_frame = ttk.Frame(toolbar)
buttons_frame.pack(side=tk.LEFT)
for icon, label, command in buttons:
self._add_toolbar_button(buttons_frame, icon, label, command)
status_container = ttk.Frame(toolbar)
status_container.pack(side=tk.RIGHT, expand=True, fill=tk.X)
self.status = ttk.Label(
status_container,
text=self._t("status.no_file"),
anchor="e",
foreground="#efefef",
)
self.status.pack(fill=tk.X)
self._attach_copy_menu(self.status)
self.status_default_text = self.status.cget("text")
self._status_palette = {"fg": self.status.cget("foreground")}
palette_frame = ttk.Frame(self.root)
palette_frame.pack(fill=tk.X, padx=12, pady=(6, 8))
default_colour = self._default_colour_hex()
current_frame = ttk.Frame(palette_frame)
current_frame.pack(side=tk.LEFT, padx=(0, 16))
ttk.Label(current_frame, text=self._t("palette.current")).pack(side=tk.LEFT, padx=(0, 6))
self.current_colour_sw = tk.Canvas(
current_frame,
width=24,
height=24,
highlightthickness=0,
background=default_colour,
bd=0,
)
self.current_colour_sw.pack(side=tk.LEFT, pady=2)
self.current_colour_label = ttk.Label(current_frame, text=f"({default_colour})")
self.current_colour_label.pack(side=tk.LEFT, padx=(6, 0))
ttk.Label(palette_frame, text=self._t("palette.more")).pack(side=tk.LEFT, padx=(0, 8))
swatch_container = ttk.Frame(palette_frame)
swatch_container.pack(side=tk.LEFT)
for name, hex_code in self._preset_colours():
self._add_palette_swatch(swatch_container, name, hex_code)
sliders_frame = ttk.Frame(self.root)
sliders_frame.pack(fill=tk.X, padx=12, pady=4)
sliders = [
(self._t("sliders.hue_min"), self.hue_min, 0, 360),
(self._t("sliders.hue_max"), self.hue_max, 0, 360),
(self._t("sliders.sat_min"), self.sat_min, 0, 100),
(self._t("sliders.val_min"), self.val_min, 0, 100),
(self._t("sliders.val_max"), self.val_max, 0, 100),
(self._t("sliders.alpha"), self.alpha, 0, 255),
]
for index, (label, variable, minimum, maximum) in enumerate(sliders):
self.add_slider_with_value(sliders_frame, label, variable, minimum, maximum, column=index)
sliders_frame.grid_columnconfigure(index, weight=1)
main = ttk.Frame(self.root)
main.pack(fill=tk.BOTH, expand=True, padx=12, pady=12)
left_column = ttk.Frame(main)
left_column.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 6))
left_column.grid_columnconfigure(1, weight=1)
left_column.grid_rowconfigure(0, weight=1)
self._create_navigation_button(left_column, "", self.show_previous_image, column=0)
self.canvas_orig = tk.Canvas(
left_column,
bg=self._canvas_background_colour(),
highlightthickness=0,
relief="flat",
)
self.canvas_orig.grid(row=0, column=1, sticky="nsew")
self.canvas_orig.bind("<Button-1>", self.on_canvas_click)
self.canvas_orig.bind("<ButtonPress-3>", self._exclude_start)
self.canvas_orig.bind("<B3-Motion>", self._exclude_drag)
self.canvas_orig.bind("<ButtonRelease-3>", self._exclude_end)
right_column = ttk.Frame(main)
right_column.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=(6, 0))
right_column.grid_columnconfigure(0, weight=1)
right_column.grid_rowconfigure(0, weight=1)
self.canvas_overlay = tk.Canvas(
right_column,
bg=self._canvas_background_colour(),
highlightthickness=0,
relief="flat",
)
self.canvas_overlay.grid(row=0, column=0, sticky="nsew")
self._create_navigation_button(right_column, "", self.show_next_image, column=1)
info_frame = ttk.Frame(self.root)
info_frame.pack(fill=tk.X, padx=12, pady=(0, 12))
self.filename_label = ttk.Label(
info_frame,
text="",
font=("Segoe UI", 10, "bold"),
anchor="center",
justify="center",
)
self.filename_label.pack(anchor="center")
self._attach_copy_menu(self.filename_label)
self.ratio_label = ttk.Label(
info_frame,
text=self._t("stats.placeholder"),
font=("Segoe UI", 10, "bold"),
anchor="center",
justify="center",
)
self.ratio_label.pack(anchor="center", pady=(4, 0))
self._attach_copy_menu(self.ratio_label)
self.root.bind("<Escape>", self.disable_pick_mode)
self.root.bind("<ButtonPress-1>", self._maybe_focus_window)
def add_slider_with_value(self, parent, text, var, minimum, maximum, column=0):
cell = ttk.Frame(parent)
cell.grid(row=0, column=column, sticky="we", padx=6)
header = ttk.Frame(cell)
header.pack(fill="x")
name_lbl = ttk.Label(header, text=text)
name_lbl.pack(side="left")
self._attach_copy_menu(name_lbl)
val_lbl = ttk.Label(header, text=f"{float(var.get()):.0f}")
val_lbl.pack(side="right")
self._attach_copy_menu(val_lbl)
style_name = getattr(self, "scale_style", "Horizontal.TScale")
ttk.Scale(
cell,
from_=minimum,
to=maximum,
orient="horizontal",
variable=var,
style=style_name,
command=self.on_slider_change,
).pack(fill="x", pady=(2, 8))
def on_var_change(*_):
val_lbl.config(text=f"{float(var.get()):.0f}")
try:
var.trace_add("write", on_var_change)
except Exception:
var.trace("w", lambda *_: on_var_change()) # type: ignore[attr-defined]
def on_slider_change(self, *_):
if self._update_job is not None:
try:
self.root.after_cancel(self._update_job)
except Exception:
pass
self._update_job = self.root.after(self.update_delay_ms, self.update_preview)
def _preset_colours(self):
return [
(self._t("palette.swatch.red"), "#ff3b30"),
(self._t("palette.swatch.orange"), "#ff9500"),
(self._t("palette.swatch.yellow"), "#ffd60a"),
(self._t("palette.swatch.green"), "#34c759"),
(self._t("palette.swatch.teal"), "#5ac8fa"),
(self._t("palette.swatch.blue"), "#0a84ff"),
(self._t("palette.swatch.violet"), "#af52de"),
(self._t("palette.swatch.magenta"), "#ff2d55"),
(self._t("palette.swatch.white"), "#ffffff"),
(self._t("palette.swatch.grey"), "#8e8e93"),
(self._t("palette.swatch.black"), "#000000"),
]
def _add_palette_swatch(self, parent, name: str, hex_code: str) -> None:
swatch = tk.Canvas(
parent,
width=24,
height=24,
highlightthickness=0,
background=hex_code,
bd=0,
relief="flat",
takefocus=1,
cursor="hand2",
)
swatch.pack(side=tk.LEFT, padx=4, pady=2)
def trigger(_event=None, colour=hex_code, label=name):
self.apply_sample_colour(colour, label)
swatch.bind("<Button-1>", trigger)
swatch.bind("<space>", trigger)
swatch.bind("<Return>", trigger)
swatch.bind("<Enter>", lambda _e: swatch.configure(cursor="hand2"))
swatch.bind("<Leave>", lambda _e: swatch.configure(cursor="arrow"))
def _add_toolbar_button(self, parent, icon: str, label: str, command) -> None:
font = tkfont.Font(root=self.root, family="Segoe UI", size=9)
padding_x = 12
gap = font.measure(" ")
icon_width = font.measure(icon) or font.measure(" ")
label_width = font.measure(label)
width = padding_x * 2 + icon_width + gap + label_width
height = 28
radius = 9
bg = self.root.cget("bg") if hasattr(self.root, "cget") else "#f2f2f7"
canvas = tk.Canvas(
parent,
width=width,
height=height,
bd=0,
highlightthickness=0,
bg=bg,
relief="flat",
cursor="hand2",
takefocus=1,
)
canvas.pack(side=tk.LEFT, padx=4, pady=1)
palette = self._toolbar_palette()
rect_id = self._create_round_rect(
canvas,
1,
1,
width - 1,
height - 1,
radius,
fill=palette["normal"],
outline=palette["outline"],
width=1,
)
icon_id = canvas.create_text(
padding_x,
height / 2,
text=icon,
font=font,
fill=palette["text"],
anchor="w",
)
label_id = canvas.create_text(
padding_x + icon_width + gap,
height / 2,
text=label,
font=font,
fill=palette["text"],
anchor="w",
)
button_data = {
"canvas": canvas,
"rect": rect_id,
"text_ids": (icon_id, label_id),
"command": command,
"palette": palette.copy(),
"dimensions": (width, height, radius),
}
self._toolbar_buttons.append(button_data)
def set_fill(state: str) -> None:
pal: dict[str, str] = button_data["palette"] # type: ignore[index]
canvas.itemconfigure(rect_id, fill=pal[state]) # type: ignore[index]
def execute():
command()
def on_press(_event=None):
set_fill("active")
def on_release(event=None):
if event is not None and (
event.x < 0 or event.y < 0 or event.x > width or event.y > height
):
set_fill("normal")
return
set_fill("hover")
self.root.after_idle(execute)
def on_enter(_event):
set_fill("hover")
def on_leave(_event):
set_fill("normal")
def on_focus_in(_event):
pal: dict[str, str] = button_data["palette"] # type: ignore[index]
canvas.itemconfigure(rect_id, outline=pal["outline_focus"]) # type: ignore[index]
def on_focus_out(_event):
pal: dict[str, str] = button_data["palette"] # type: ignore[index]
canvas.itemconfigure(rect_id, outline=pal["outline"]) # type: ignore[index]
def invoke_keyboard(_event=None):
set_fill("active")
canvas.after(120, lambda: set_fill("hover"))
self.root.after_idle(execute)
canvas.bind("<ButtonPress-1>", on_press)
canvas.bind("<ButtonRelease-1>", on_release)
canvas.bind("<Enter>", on_enter)
canvas.bind("<Leave>", on_leave)
canvas.bind("<FocusIn>", on_focus_in)
canvas.bind("<FocusOut>", on_focus_out)
canvas.bind("<space>", invoke_keyboard)
canvas.bind("<Return>", invoke_keyboard)
@staticmethod
def _create_round_rect(canvas: tk.Canvas, x1, y1, x2, y2, radius, **kwargs):
points = [
x1 + radius,
y1,
x2 - radius,
y1,
x2,
y1,
x2,
y1 + radius,
x2,
y2 - radius,
x2,
y2,
x2 - radius,
y2,
x1 + radius,
y2,
x1,
y2,
x1,
y2 - radius,
x1,
y1 + radius,
x1,
y1,
]
return canvas.create_polygon(points, smooth=True, splinesteps=24, **kwargs)
def _create_navigation_button(self, container, symbol: str, command, *, column: int) -> None:
palette = self._navigation_palette()
bg = palette["bg"]
fg = palette["fg"]
container.grid_rowconfigure(0, weight=1)
btn = tk.Button(
container,
text=symbol,
command=command,
font=("Segoe UI", 26, "bold"),
relief="flat",
borderwidth=0,
background=bg,
activebackground=bg,
highlightthickness=0,
fg=fg,
activeforeground=fg,
cursor="hand2",
width=2,
)
btn.grid(row=0, column=column, sticky="ns", padx=6)
self._nav_buttons.append(btn)
def _create_titlebar(self) -> None:
bar_bg = "#1f1f1f"
title_bar = tk.Frame(self.root, bg=bar_bg, relief="flat", height=34)
title_bar.pack(fill=tk.X, side=tk.TOP)
title_bar.pack_propagate(False)
logo = None
try:
from PIL import Image, ImageTk # type: ignore
from importlib import resources
logo_resource = resources.files("app.assets").joinpath("logo.png")
with resources.as_file(logo_resource) as logo_path:
image = Image.open(logo_path).convert("RGBA")
image.thumbnail((26, 26))
logo = ImageTk.PhotoImage(image)
except Exception:
logo = None
if logo is not None:
logo_label = tk.Label(title_bar, image=logo, bg=bar_bg)
logo_label.image = logo # keep reference
logo_label.pack(side=tk.LEFT, padx=(10, 6), pady=4)
title_label = tk.Label(
title_bar,
text=self._t("app.title"),
bg=bar_bg,
fg="#f5f5f5",
font=("Segoe UI", 11, "bold"),
anchor="w",
)
title_label.pack(side=tk.LEFT, padx=6)
close_btn = tk.Button(
title_bar,
text="",
command=self._close_app,
bg=bar_bg,
fg="#f5f5f5",
activebackground="#ff3b30",
activeforeground="#ffffff",
borderwidth=0,
highlightthickness=0,
relief="flat",
font=("Segoe UI", 10, "bold"),
cursor="hand2",
width=3,
)
close_btn.pack(side=tk.RIGHT, padx=8, pady=4)
close_btn.bind("<Enter>", lambda _e: close_btn.configure(bg="#cf212f"))
close_btn.bind("<Leave>", lambda _e: close_btn.configure(bg=bar_bg))
for widget in (title_bar, title_label):
widget.bind("<ButtonPress-1>", self._start_window_drag)
widget.bind("<B1-Motion>", self._perform_window_drag)
def _close_app(self) -> None:
try:
self.root.destroy()
except Exception:
pass
def _start_window_drag(self, event) -> None:
self._drag_offset = (event.x_root - self.root.winfo_rootx(), event.y_root - self.root.winfo_rooty())
def _perform_window_drag(self, event) -> None:
offset = getattr(self, "_drag_offset", None)
if offset is None:
return
x = event.x_root - offset[0]
y = event.y_root - offset[1]
self.root.geometry(f"+{x}+{y}")
def _maybe_focus_window(self, _event) -> None:
try:
self.root.focus_set()
except Exception:
pass
def _toolbar_palette(self) -> dict[str, str]:
is_dark = getattr(self, "theme", "light") == "dark"
if is_dark:
return {
"normal": "#2f2f35",
"hover": "#3a3a40",
"active": "#1f1f25",
"outline": "#4d4d50",
"outline_focus": "#7c7c88",
"text": "#f1f1f5",
}
return {
"normal": "#ffffff",
"hover": "#ededf4",
"active": "#dcdce6",
"outline": "#d0d0d8",
"outline_focus": "#a9a9b2",
"text": "#1f1f1f",
}
def _navigation_palette(self) -> dict[str, str]:
is_dark = getattr(self, "theme", "light") == "dark"
default_bg = "#0f0f10" if is_dark else "#ededf2"
bg = self.root.cget("bg") if hasattr(self.root, "cget") else default_bg
fg = "#f5f5f5" if is_dark else "#1f1f1f"
return {"bg": bg, "fg": fg}
def _refresh_toolbar_buttons_theme(self) -> None:
if not getattr(self, "_toolbar_buttons", None):
return
bg = self.root.cget("bg") if hasattr(self.root, "cget") else "#f2f2f7"
palette = self._toolbar_palette()
for data in self._toolbar_buttons:
canvas = data["canvas"] # type: ignore[index]
rect_id = data["rect"] # type: ignore[index]
text_ids = data["text_ids"] # type: ignore[index]
data["palette"] = palette.copy()
canvas.configure(bg=bg)
canvas.itemconfigure(rect_id, fill=palette["normal"], outline=palette["outline"])
for text_id in text_ids:
canvas.itemconfigure(text_id, fill=palette["text"])
def _refresh_navigation_buttons_theme(self) -> None:
if not getattr(self, "_nav_buttons", None):
return
palette = self._navigation_palette()
for btn in self._nav_buttons:
btn.configure(
background=palette["bg"],
activebackground=palette["bg"],
fg=palette["fg"],
activeforeground=palette["fg"],
)
def _canvas_background_colour(self) -> str:
return "#0f0f10" if getattr(self, "theme", "light") == "dark" else "#ffffff"
def _refresh_canvas_backgrounds(self) -> None:
bg = self._canvas_background_colour()
for attr in ("canvas_orig", "canvas_overlay"):
canvas = getattr(self, attr, None)
if canvas is not None:
try:
canvas.configure(bg=bg)
except Exception:
pass
def _refresh_status_palette(self, fg: str) -> None:
self.status.configure(foreground=fg)
self._status_palette["fg"] = fg
def _refresh_accent_labels(self, colour: str) -> None:
try:
self.filename_label.configure(foreground=colour)
self.ratio_label.configure(foreground=colour)
except Exception:
pass
def _default_colour_hex(self) -> str:
defaults = getattr(self, "DEFAULTS", {})
hue_min = float(defaults.get("hue_min", 0.0))
hue_max = float(defaults.get("hue_max", hue_min))
if hue_min <= hue_max:
hue = (hue_min + hue_max) / 2.0
else:
span = ((hue_max + 360.0) - hue_min) / 2.0
hue = (hue_min + span) % 360.0
sat_min = float(defaults.get("sat_min", 0.0))
saturation = (sat_min + 100.0) / 2.0
val_min = float(defaults.get("val_min", 0.0))
val_max = float(defaults.get("val_max", 100.0))
value = (val_min + val_max) / 2.0
r, g, b = colorsys.hsv_to_rgb(hue / 360.0, saturation / 100.0, value / 100.0)
return f"#{int(r * 255):02x}{int(g * 255):02x}{int(b * 255):02x}"
def _init_copy_menu(self):
self._copy_target = None
self.copy_menu = tk.Menu(self.root, tearoff=0)
label = self._t("menu.copy") if hasattr(self, "_t") else "Copy"
self.copy_menu.add_command(label=label, command=self._copy_current_label)
def _attach_copy_menu(self, widget):
widget.bind("<Button-3>", lambda event, w=widget: self._show_copy_menu(event, w))
widget.bind("<Control-c>", lambda event, w=widget: self._copy_widget_text(w))
def _show_copy_menu(self, event, widget):
self._copy_target = widget
try:
self.copy_menu.tk_popup(event.x_root, event.y_root)
finally:
self.copy_menu.grab_release()
def _copy_current_label(self):
if self._copy_target is not None:
self._copy_widget_text(self._copy_target)
def _copy_widget_text(self, widget):
try:
text = widget.cget("text")
except Exception:
text = ""
if not text:
return
try:
self.root.clipboard_clear()
self.root.clipboard_append(text)
except Exception:
pass
__all__ = ["UIBuilderMixin"]
+7 -6
View File
@@ -40,13 +40,14 @@ def _load_translations(lang: str) -> Dict[str, str]:
data = tomllib.load(handle) data = tomllib.load(handle)
except (OSError, AttributeError, ValueError, TypeError): # type: ignore[arg-type] except (OSError, AttributeError, ValueError, TypeError): # type: ignore[arg-type]
return {} return {}
translations = data.get("translations")
if not isinstance(translations, dict): # Merge all dictionaries found in the TOML (e.g. [translations] and [tooltip])
return {}
out: Dict[str, str] = {} out: Dict[str, str] = {}
for key, value in translations.items(): for section_name, section_data in data.items():
if isinstance(key, str) and isinstance(value, str): if isinstance(section_data, dict):
out[key] = value for key, value in section_data.items():
if isinstance(key, str) and isinstance(value, str):
out[key] = value
return out return out
+73 -6
View File
@@ -3,13 +3,16 @@
"toolbar.open_image" = "Bild laden" "toolbar.open_image" = "Bild laden"
"toolbar.open_folder" = "Ordner laden" "toolbar.open_folder" = "Ordner laden"
"toolbar.choose_color" = "Farbe wählen" "toolbar.choose_color" = "Farbe wählen"
"toolbar.pick_from_image" = "Farbe aus Bild klicken" "toolbar.pick_from_image" = "Farbe aus Bild auswählen"
"toolbar.save_overlay" = "Overlay speichern" "toolbar.save_overlay" = "Bild speichern"
"toolbar.clear_excludes" = "Ausschlüsse löschen" "toolbar.clear_excludes" = "Ausschlüsse löschen"
"toolbar.toggle_free_draw" = "Freihandmodus umschalten" "toolbar.toggle_free_draw" = "Freihandmodus umschalten"
"toolbar.undo_exclude" = "Letzten Ausschluss entfernen" "toolbar.undo_exclude" = "Letzten Ausschluss entfernen"
"toolbar.reset_sliders" = "Slider zurücksetzen" "toolbar.reset_sliders" = "Slider zurücksetzen"
"toolbar.toggle_theme" = "Theme umschalten" "toolbar.toggle_theme" = "Theme umschalten"
"toolbar.open_app_folder" = "Programmordner öffnen"
"toolbar.prefer_dark" = "Dunkelheit bevorzugen"
"toolbar.exclude_bg" = "Hintergrund ausblenden ({color})"
"status.no_file" = "Keine Datei geladen." "status.no_file" = "Keine Datei geladen."
"status.defaults_restored" = "Standardwerte aktiv." "status.defaults_restored" = "Standardwerte aktiv."
"status.free_draw_enabled" = "Freihand-Ausschluss aktiviert." "status.free_draw_enabled" = "Freihand-Ausschluss aktiviert."
@@ -17,11 +20,12 @@
"status.loaded" = "Geladen: {name} — {dimensions}{position}" "status.loaded" = "Geladen: {name} — {dimensions}{position}"
"status.filename_label" = "{name} — {dimensions}{position}" "status.filename_label" = "{name} — {dimensions}{position}"
"status.color_selected" = "Farbe gewählt: {label} — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" "status.color_selected" = "Farbe gewählt: {label} — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%"
"status.sample_colour" = "Beispielfarbe gewählt: {label} ({hex_code}) — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" "status.sample_color" = "Beispielfarbe gewählt: {label} ({hex_code}) — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%"
"status.pick_mode_ready" = "Pick-Modus: Klicke links ins Bild, um Farbe zu wählen (Esc beendet)" "status.pick_mode_ready" = "Pick-Modus: Klicke links ins Bild, um Farbe zu wählen (Esc beendet)"
"status.pick_mode_ended" = "Pick-Modus beendet." "status.pick_mode_ended" = "Pick-Modus beendet."
"status.pick_mode_from_image" = "Farbe vom Bild gewählt: Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" "status.pick_mode_from_image" = "Farbe vom Bild gewählt: Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%"
"palette.current" = "Farbe:" "palette.current" = "Farbe:"
"palette.overlay_color" = "Overlay:"
"palette.more" = "Weitere Farben:" "palette.more" = "Weitere Farben:"
"palette.swatch.red" = "Rot" "palette.swatch.red" = "Rot"
"palette.swatch.orange" = "Orange" "palette.swatch.orange" = "Orange"
@@ -37,19 +41,25 @@
"sliders.hue_min" = "Hue Min (°)" "sliders.hue_min" = "Hue Min (°)"
"sliders.hue_max" = "Hue Max (°)" "sliders.hue_max" = "Hue Max (°)"
"sliders.sat_min" = "Sättigung Min (%)" "sliders.sat_min" = "Sättigung Min (%)"
"sliders.sat_max" = "Sättigung Max (%)"
"sliders.val_min" = "Helligkeit Min (%)" "sliders.val_min" = "Helligkeit Min (%)"
"sliders.val_max" = "Helligkeit Max (%)" "sliders.val_max" = "Helligkeit Max (%)"
"sliders.alpha" = "Overlay Alpha" "sliders.alpha" = "Overlay Alpha"
"stats.placeholder" = "Markierungen (mit Ausschlüssen): —" "stats.placeholder" = "Markierungen (mit Ausschlüssen): —"
"stats.summary" = "Markierungen (mit Ausschlüssen): {with_pct:.2f}% | Markierungen (ohne Ausschlüsse): {without_pct:.2f}% | Ausgeschlossen: {excluded_pct:.2f}% der Pixel, davon {excluded_match_pct:.2f}% markiert" "stats.summary" = "Gesamtwertung: {score:.2f}% | Treffer (m. Ausschl.): {with_pct:.2f}% | Treffer: {without_pct:.2f}% | {brightness_label}: {brightness:.1f}% | Gruppierung: {grouping:.1f}% | Kontinuität: {continuity:.1f}% | Rand: {border:.1f}%"
"stats.brightness_label" = "Helligkeit"
"stats.darkness_label" = "Dunkelheit"
"stats.grouping_label" = "Gruppierung"
"stats.continuity_label" = "Kontinuität"
"stats.border_label" = "Rand"
"menu.copy" = "Kopieren" "menu.copy" = "Kopieren"
"dialog.info_title" = "Info" "dialog.info_title" = "Info"
"dialog.error_title" = "Fehler" "dialog.error_title" = "Fehler"
"dialog.saved_title" = "Gespeichert" "dialog.saved_title" = "Gespeichert"
"dialog.open_image_title" = "Bild wählen" "dialog.open_image_title" = "Bild wählen"
"dialog.open_folder_title" = "Ordner mit Bildern wählen" "dialog.open_folder_title" = "Ordner mit Bildern wählen"
"dialog.save_overlay_title" = "Overlay speichern als" "dialog.save_overlay_title" = "Bild speichern als"
"dialog.choose_colour_title" = "Farbe wählen" "dialog.choose_color_title" = "Farbe wählen"
"dialog.images_filter" = "Bilder" "dialog.images_filter" = "Bilder"
"dialog.folder_not_found" = "Der Ordner wurde nicht gefunden." "dialog.folder_not_found" = "Der Ordner wurde nicht gefunden."
"dialog.folder_empty" = "Keine unterstützten Bilder im Ordner gefunden." "dialog.folder_empty" = "Keine unterstützten Bilder im Ordner gefunden."
@@ -59,3 +69,60 @@
"dialog.no_image_loaded" = "Kein Bild geladen." "dialog.no_image_loaded" = "Kein Bild geladen."
"dialog.no_preview_available" = "Keine Preview vorhanden." "dialog.no_preview_available" = "Keine Preview vorhanden."
"dialog.overlay_saved" = "Overlay gespeichert: {path}" "dialog.overlay_saved" = "Overlay gespeichert: {path}"
"dialog.json_filter" = "JSON-Dateien (*.json)"
"dialog.export_settings_title" = "Einstellungen als JSON exportieren"
"dialog.import_settings_title" = "Einstellungen aus JSON importieren"
"status.settings_exported" = "Einstellungen exportiert: {path}"
"status.settings_imported" = "Einstellungen importiert."
"toolbar.export_settings" = "Einstellungen exportieren (JSON)"
"toolbar.import_settings" = "Einstellungen importieren (JSON)"
"dialog.export_stats_title" = "Ordner-Statistiken exportieren (CSV)"
"dialog.csv_filter" = "CSV-Dateien (*.csv)"
"status.drag_drop" = "Bild oder Ordner hier ablegen."
"status.exporting" = "Statistiken werden exportiert... ({current}/{total})"
"status.export_done" = "Export abgeschlossen: {path}"
"toolbar.export_folder" = "Ordner-Statistik"
"menu.file" = "Datei"
"menu.edit" = "Bearbeiten"
"menu.exclusions" = "Ausschlüsse"
"menu.view" = "Ansicht"
"menu.view_log" = "Protokoll anzeigen"
"menu.tools" = "Werkzeuge"
"toolbar.pull_patterns" = "Muster-Bilder laden"
"dialog.puller_title" = "Muster-Bilder laden"
"dialog.puller_instruction" = "CSGOSkins.gg Item-URL einfügen:"
"dialog.puller_start" = "Download starten"
"dialog.puller_cancel" = "Abbrechen"
"dialog.puller_invalid_url" = "Ungültiges URL-Format."
"dialog.puller_success" = "Alle Muster erfolgreich heruntergeladen!"
"dialog.weighting_title" = "Export-Gewichtung & Präferenz"
"dialog.weighting_instruction" = "Gewichtung der Komponenten festlegen (Summe muss 100% sein):"
"dialog.weight_match_all" = "Treffer (Alle) %"
"dialog.weight_match_keep" = "Treffer (Behalten) %"
"dialog.weight_brightness" = "Helligkeit/Dunkelheit %"
"dialog.weight_grouping" = "Gruppierung %"
"dialog.weight_continuity" = "Kontinuität %"
"dialog.weight_border" = "Rand Sauberkeit %"
"dialog.total_weight" = "Gesamt:"
"dialog.weight_error" = "Gewichtungen müssen exakt 100% ergeben (aktuell {total}%)."
[tooltip]
"tooltip.open_image" = "Öffnet ein einzelnes Bild zur Analyse"
"tooltip.open_folder" = "Öffnet einen Ordner mit Bildern für die Stapelverarbeitung"
"tooltip.export_folder" = "Exportiert die Ergebnisse für den aktuellen Ordner als CSV"
"tooltip.export_settings" = "Speichert die aktuellen Schieberegler- und Farbeinstellungen in einer JSON-Datei"
"tooltip.import_settings" = "Lädt gespeicherte Einstellungen aus einer JSON-Datei"
"tooltip.save_overlay" = "Speichert das aktuell sichtbare zusammengesetzte Overlay-Bild"
"tooltip.open_app_folder" = "Öffnet das Installationsverzeichnis der Anwendung"
"tooltip.reset_sliders" = "Setzt alle Schieberegler auf ihre Standardwerte zurück"
"tooltip.pick_from_image" = "Klicken Sie auf das Bild, um eine Zielfarbe auszuwählen"
"tooltip.prefer_dark" = "Priorisiert dunklere Farben gegenüber helleren bei der Gesamtbewertung"
"tooltip.undo_exclude" = "Macht die zuletzt gezeichnete Ausschlussform rückgängig"
"tooltip.clear_excludes" = "Entfernt alle Ausschlussformen aus dem Bild"
"tooltip.toggle_free_draw" = "Wechselt zwischen dem Zeichnen von Rechtecken und Freiform-Polygonen"
"tooltip.exclude_bg" = "Ignoriert basierend auf den Einstellungen automatisch Hintergrundfarben"
"tooltip.pull_patterns" = "Lädt Musterbilder von einer Remote-Quelle herunter"
"tooltip.toggle_theme" = "Wechselt zwischen hellem und dunklem UI-Design"
"tooltip.view_log" = "Zeigt aktuelle Anwendungsstatusmeldungen und Protokolle an"
+79 -12
View File
@@ -2,27 +2,31 @@
"app.title" = "Interactive Color Range Analyzer" "app.title" = "Interactive Color Range Analyzer"
"toolbar.open_image" = "Open image" "toolbar.open_image" = "Open image"
"toolbar.open_folder" = "Open folder" "toolbar.open_folder" = "Open folder"
"toolbar.choose_color" = "Choose colour" "toolbar.choose_color" = "Choose color"
"toolbar.pick_from_image" = "Pick from image" "toolbar.pick_from_image" = "Select color from image"
"toolbar.save_overlay" = "Save overlay" "toolbar.save_overlay" = "Save Image"
"toolbar.clear_excludes" = "Clear exclusions" "toolbar.clear_excludes" = "Clear exclusions"
"toolbar.toggle_free_draw" = "Toggle free-draw" "toolbar.toggle_free_draw" = "Toggle free-draw"
"toolbar.undo_exclude" = "Undo last exclusion" "toolbar.undo_exclude" = "Undo last exclusion"
"toolbar.reset_sliders" = "Reset sliders" "toolbar.reset_sliders" = "Reset sliders"
"toolbar.toggle_theme" = "Toggle theme" "toolbar.toggle_theme" = "Toggle theme"
"toolbar.open_app_folder" = "Open application folder"
"toolbar.prefer_dark" = "Prefer darkness"
"toolbar.exclude_bg" = "Exclude Background ({color})"
"status.no_file" = "No file loaded." "status.no_file" = "No file loaded."
"status.defaults_restored" = "Defaults restored." "status.defaults_restored" = "Defaults restored."
"status.free_draw_enabled" = "Free-draw exclusion mode enabled." "status.free_draw_enabled" = "Free-draw exclusion mode enabled."
"status.free_draw_disabled" = "Rectangle exclusion mode enabled." "status.free_draw_disabled" = "Rectangle exclusion mode enabled."
"status.loaded" = "Loaded: {name} — {dimensions}{position}" "status.loaded" = "Loaded: {name} — {dimensions}{position}"
"status.filename_label" = "{name} — {dimensions}{position}" "status.filename_label" = "{name} — {dimensions}{position}"
"status.color_selected" = "Colour chosen: {label} — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" "status.color_selected" = "Color chosen: {label} — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%"
"status.sample_colour" = "Sample colour applied: {label} ({hex_code}) — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" "status.sample_color" = "Sample color applied: {label} ({hex_code}) — Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%"
"status.pick_mode_ready" = "Pick mode: Click the left image to choose a colour (Esc exits)" "status.pick_mode_ready" = "Pick mode: Click the left image to choose a color (Esc exits)"
"status.pick_mode_ended" = "Pick mode ended." "status.pick_mode_ended" = "Pick mode ended."
"status.pick_mode_from_image" = "Colour picked from image: Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%" "status.pick_mode_from_image" = "Color picked from image: Hue {hue:.1f}°, S {saturation:.0f}%, V {value:.0f}%"
"palette.current" = "Colour:" "palette.current" = "Color:"
"palette.more" = "More colours:" "palette.overlay_color" = "Overlay:"
"palette.more" = "More colors:"
"palette.swatch.red" = "Red" "palette.swatch.red" = "Red"
"palette.swatch.orange" = "Orange" "palette.swatch.orange" = "Orange"
"palette.swatch.yellow" = "Yellow" "palette.swatch.yellow" = "Yellow"
@@ -37,19 +41,25 @@
"sliders.hue_min" = "Hue min (°)" "sliders.hue_min" = "Hue min (°)"
"sliders.hue_max" = "Hue max (°)" "sliders.hue_max" = "Hue max (°)"
"sliders.sat_min" = "Saturation min (%)" "sliders.sat_min" = "Saturation min (%)"
"sliders.sat_max" = "Saturation max (%)"
"sliders.val_min" = "Value min (%)" "sliders.val_min" = "Value min (%)"
"sliders.val_max" = "Value max (%)" "sliders.val_max" = "Value max (%)"
"sliders.alpha" = "Overlay alpha" "sliders.alpha" = "Overlay alpha"
"stats.placeholder" = "Matches (with exclusions): —" "stats.placeholder" = "Matches (with exclusions): —"
"stats.summary" = "Matches (with exclusions): {with_pct:.2f}% | Matches (without exclusions): {without_pct:.2f}% | Excluded: {excluded_pct:.2f}% of pixels, {excluded_match_pct:.2f}% marked" "stats.summary" = "Composite Score: {score:.2f}% | Matches (w/ excl.): {with_pct:.2f}% | Matches: {without_pct:.2f}% | {brightness_label}: {brightness:.1f}% | Grouping: {grouping:.1f}% | Continuity: {continuity:.1f}% | Border: {border:.1f}%"
"stats.brightness_label" = "Brightness"
"stats.darkness_label" = "Darkness"
"stats.grouping_label" = "Grouping"
"stats.continuity_label" = "Continuity"
"stats.border_label" = "Border"
"menu.copy" = "Copy" "menu.copy" = "Copy"
"dialog.info_title" = "Info" "dialog.info_title" = "Info"
"dialog.error_title" = "Error" "dialog.error_title" = "Error"
"dialog.saved_title" = "Saved" "dialog.saved_title" = "Saved"
"dialog.open_image_title" = "Select image" "dialog.open_image_title" = "Select image"
"dialog.open_folder_title" = "Select folder" "dialog.open_folder_title" = "Select folder"
"dialog.save_overlay_title" = "Save overlay as" "dialog.save_overlay_title" = "Save Image as"
"dialog.choose_colour_title" = "Choose colour" "dialog.choose_color_title" = "Choose color"
"dialog.images_filter" = "Images" "dialog.images_filter" = "Images"
"dialog.folder_not_found" = "The folder could not be found." "dialog.folder_not_found" = "The folder could not be found."
"dialog.folder_empty" = "No supported images were found in the folder." "dialog.folder_empty" = "No supported images were found in the folder."
@@ -59,3 +69,60 @@
"dialog.no_image_loaded" = "No image loaded." "dialog.no_image_loaded" = "No image loaded."
"dialog.no_preview_available" = "No preview available." "dialog.no_preview_available" = "No preview available."
"dialog.overlay_saved" = "Overlay saved: {path}" "dialog.overlay_saved" = "Overlay saved: {path}"
"dialog.json_filter" = "JSON Files (*.json)"
"dialog.export_settings_title" = "Export settings to JSON"
"dialog.import_settings_title" = "Import settings from JSON"
"status.settings_exported" = "Settings exported: {path}"
"status.settings_imported" = "Settings imported."
"toolbar.export_settings" = "Export settings (JSON)"
"toolbar.import_settings" = "Import settings (JSON)"
"dialog.export_stats_title" = "Export Folder Statistics (CSV)"
"dialog.csv_filter" = "CSV Files (*.csv)"
"status.drag_drop" = "Drop an image or folder here to open it."
"status.exporting" = "Exporting statistics... ({current}/{total})"
"status.export_done" = "Export complete: {path}"
"toolbar.export_folder" = "Export Folder Stats"
"menu.file" = "File"
"menu.edit" = "Edit"
"menu.exclusions" = "Exclusions"
"menu.view" = "View"
"menu.view_log" = "View Log"
"menu.tools" = "Tools"
"toolbar.pull_patterns" = "Pull Pattern Images"
"dialog.puller_title" = "Pull Pattern Images"
"dialog.puller_instruction" = "Paste a CSGOSkins.gg item URL:"
"dialog.puller_start" = "Start Download"
"dialog.puller_cancel" = "Cancel"
"dialog.puller_invalid_url" = "Invalid URL format."
"dialog.puller_success" = "All patterns downloaded successfully!"
"dialog.weighting_title" = "Export Weighting & Preference"
"dialog.weighting_instruction" = "Set the weighting for each component (total must be 100%):"
"dialog.weight_match_all" = "Match (All) %"
"dialog.weight_match_keep" = "Match (Keep) %"
"dialog.weight_brightness" = "Brightness/Darkness %"
"dialog.weight_grouping" = "Grouping %"
"dialog.weight_continuity" = "Continuity %"
"dialog.weight_border" = "Border Cleanliness %"
"dialog.total_weight" = "Total:"
"dialog.weight_error" = "Weights must sum exactly to 100% (currently {total}%)."
[tooltip]
"tooltip.open_image" = "Open a single image for analysis"
"tooltip.open_folder" = "Open a folder of images to process in batch"
"tooltip.export_folder" = "Export CSV results for the current folder"
"tooltip.export_settings" = "Save current slider and color settings to a JSON file"
"tooltip.import_settings" = "Load saved settings from a JSON file"
"tooltip.save_overlay" = "Save the currently visible composite overlay image"
"tooltip.open_app_folder" = "Open the application installation directory"
"tooltip.reset_sliders" = "Reset all sliders to their default values"
"tooltip.pick_from_image" = "Click on the image to select a target color"
"tooltip.prefer_dark" = "Prioritize darker colors over brighter ones in the composite score"
"tooltip.undo_exclude" = "Undo the last exclusion shape drawn"
"tooltip.clear_excludes" = "Remove all exclusion shapes from the image"
"tooltip.toggle_free_draw" = "Toggle between drawing rectangles and free-form polygons"
"tooltip.exclude_bg" = "Automatically ignore background colors based on settings"
"tooltip.pull_patterns" = "Download pattern images from a remote source"
"tooltip.toggle_theme" = "Switch between light and dark UI themes"
"tooltip.view_log" = "View recent application status messages and logs"
+3 -40
View File
@@ -1,49 +1,12 @@
"""Launcher ensuring Tcl/Tk resources are available before starting ICRA.""" """Launcher for the PySide6 ICRA application."""
from __future__ import annotations from __future__ import annotations
import os from .qt import run
import shutil
import subprocess
import sys
from pathlib import Path
def _copy_tcl_runtime(venv_root: Path) -> tuple[Path, Path] | None:
"""Copy Tcl/Tk directories from the base interpreter into the venv if needed."""
base_prefix = Path(getattr(sys, "base_prefix", sys.prefix))
base_tcl_dir = base_prefix / "tcl"
if not base_tcl_dir.exists():
return None
tcl_src = base_tcl_dir / "tcl8.6"
tk_src = base_tcl_dir / "tk8.6"
if not tcl_src.exists() or not tk_src.exists():
return None
target_root = venv_root / "tcl"
tcl_dest = target_root / "tcl8.6"
tk_dest = target_root / "tk8.6"
if not tcl_dest.exists():
shutil.copytree(tcl_src, tcl_dest, dirs_exist_ok=True)
if not tk_dest.exists():
shutil.copytree(tk_src, tk_dest, dirs_exist_ok=True)
return tcl_dest, tk_dest
def main() -> int: def main() -> int:
venv_root = Path(sys.prefix) return run()
tcl_paths = _copy_tcl_runtime(venv_root)
env = os.environ.copy()
if tcl_paths:
env.setdefault("TCL_LIBRARY", str(tcl_paths[0]))
env.setdefault("TK_LIBRARY", str(tcl_paths[1]))
return subprocess.call([sys.executable, "main.py"], env=env)
if __name__ == "__main__": if __name__ == "__main__":
+8 -5
View File
@@ -1,25 +1,28 @@
"""Logic utilities and mixins for processing and configuration.""" """Logic utilities and configuration constants."""
from .constants import ( from .constants import (
BASE_DIR, BASE_DIR,
DEFAULTS, DEFAULTS,
IMAGES_DIR, IMAGES_DIR,
LANGUAGE, LANGUAGE,
OVERLAY_COLOR,
EXCLUDE_BG_COLOR,
EXCLUDE_BG_TOLERANCE,
WEIGHTS,
PREVIEW_MAX_SIZE, PREVIEW_MAX_SIZE,
RESET_EXCLUSIONS_ON_IMAGE_CHANGE, RESET_EXCLUSIONS_ON_IMAGE_CHANGE,
SUPPORTED_IMAGE_EXTENSIONS, SUPPORTED_IMAGE_EXTENSIONS,
) )
from .image_processing import ImageProcessingMixin
from .reset import ResetMixin
__all__ = [ __all__ = [
"BASE_DIR", "BASE_DIR",
"DEFAULTS", "DEFAULTS",
"IMAGES_DIR", "IMAGES_DIR",
"LANGUAGE", "LANGUAGE",
"OVERLAY_COLOR",
"EXCLUDE_BG_COLOR",
"WEIGHTS",
"PREVIEW_MAX_SIZE", "PREVIEW_MAX_SIZE",
"RESET_EXCLUSIONS_ON_IMAGE_CHANGE", "RESET_EXCLUSIONS_ON_IMAGE_CHANGE",
"SUPPORTED_IMAGE_EXTENSIONS", "SUPPORTED_IMAGE_EXTENSIONS",
"ImageProcessingMixin",
"ResetMixin",
] ]
+39 -2
View File
@@ -94,7 +94,21 @@ def _extract_language(data: dict[str, Any]) -> str:
_CONFIG_DATA = _load_config_data() _CONFIG_DATA = _load_config_data()
_OPTION_DEFAULTS = {"reset_exclusions_on_image_change": False} _OPTION_DEFAULTS = {
"reset_exclusions_on_image_change": False,
"overlay_color": "#ff0000",
"exclude_bg_color": "#1f2937",
"exclude_bg_tolerance": 5,
}
_WEIGHT_DEFAULTS = {
"match_all": 20,
"match_keep": 20,
"brightness": 10,
"grouping": 10,
"continuity": 20,
"border": 20,
}
def _extract_options(data: dict[str, Any]) -> dict[str, Any]: def _extract_options(data: dict[str, Any]) -> dict[str, Any]:
@@ -105,12 +119,35 @@ def _extract_options(data: dict[str, Any]) -> dict[str, Any]:
value = section.get("reset_exclusions_on_image_change") value = section.get("reset_exclusions_on_image_change")
if isinstance(value, bool): if isinstance(value, bool):
result["reset_exclusions_on_image_change"] = value result["reset_exclusions_on_image_change"] = value
color = section.get("overlay_color")
if isinstance(color, str) and color.startswith("#") and len(color) in (7, 9):
result["overlay_color"] = color
exclude_bg = section.get("exclude_bg_color")
if isinstance(exclude_bg, str) and exclude_bg.startswith("#") and len(exclude_bg) in (7, 9):
result["exclude_bg_color"] = exclude_bg
tolerance = section.get("exclude_bg_tolerance")
if isinstance(tolerance, int):
result["exclude_bg_tolerance"] = max(0, min(255, tolerance))
return result return result
_CONFIG_DATA = _load_config_data() def _extract_weights(data: dict[str, Any]) -> dict[str, int]:
section = data.get("weights")
if not isinstance(section, dict):
return {}
result: dict[str, int] = {}
for key in _WEIGHT_DEFAULTS:
value = section.get(key)
if isinstance(value, int):
result[key] = max(0, min(100, value))
return result
DEFAULTS = {**_DEFAULTS_BASE, **_extract_default_overrides(_CONFIG_DATA)} DEFAULTS = {**_DEFAULTS_BASE, **_extract_default_overrides(_CONFIG_DATA)}
LANGUAGE = _extract_language(_CONFIG_DATA) LANGUAGE = _extract_language(_CONFIG_DATA)
OPTIONS = {**_OPTION_DEFAULTS, **_extract_options(_CONFIG_DATA)} OPTIONS = {**_OPTION_DEFAULTS, **_extract_options(_CONFIG_DATA)}
RESET_EXCLUSIONS_ON_IMAGE_CHANGE = OPTIONS["reset_exclusions_on_image_change"] RESET_EXCLUSIONS_ON_IMAGE_CHANGE = OPTIONS["reset_exclusions_on_image_change"]
OVERLAY_COLOR = OPTIONS["overlay_color"]
EXCLUDE_BG_COLOR = OPTIONS["exclude_bg_color"]
EXCLUDE_BG_TOLERANCE = OPTIONS["exclude_bg_tolerance"]
WEIGHTS = {**_WEIGHT_DEFAULTS, **_extract_weights(_CONFIG_DATA)}
-483
View File
@@ -1,483 +0,0 @@
"""Image loading, processing, and statistics logic."""
from __future__ import annotations
import colorsys
from pathlib import Path
from typing import Iterable, Sequence, Tuple
from tkinter import filedialog, messagebox
from PIL import Image, ImageDraw, ImageTk
from .constants import IMAGES_DIR, PREVIEW_MAX_SIZE, SUPPORTED_IMAGE_EXTENSIONS
class ImageProcessingMixin:
"""Handles all image related operations."""
image_path: Path | None
orig_img: Image.Image | None
preview_img: Image.Image | None
preview_tk: ImageTk.PhotoImage | None
overlay_tk: ImageTk.PhotoImage | None
image_paths: list[Path]
current_image_index: int
def load_image(self) -> None:
default_dir = IMAGES_DIR if IMAGES_DIR.exists() else Path.cwd()
path = filedialog.askopenfilename(
title=self._t("dialog.open_image_title"),
filetypes=[(self._t("dialog.images_filter"), "*.webp *.png *.jpg *.jpeg *.bmp")],
initialdir=str(default_dir),
)
if not path:
return
self._set_image_collection([Path(path)], 0)
def load_folder(self) -> None:
default_dir = IMAGES_DIR if IMAGES_DIR.exists() else Path.cwd()
directory = filedialog.askdirectory(
title=self._t("dialog.open_folder_title"),
initialdir=str(default_dir),
)
if not directory:
return
folder = Path(directory)
if not folder.exists():
messagebox.showerror(
self._t("dialog.error_title"),
self._t("dialog.folder_not_found"),
)
return
image_files = sorted(
(
path
for path in folder.iterdir()
if path.suffix.lower() in SUPPORTED_IMAGE_EXTENSIONS and path.is_file()
),
key=lambda item: item.name.lower(),
)
if not image_files:
messagebox.showinfo(
self._t("dialog.info_title"),
self._t("dialog.folder_empty"),
)
return
self._set_image_collection(image_files, 0)
def show_next_image(self, event=None) -> None:
if not getattr(self, "image_paths", None):
return
if not self.image_paths:
return
current = getattr(self, "current_image_index", -1)
next_index = (current + 1) % len(self.image_paths)
self._display_image_by_index(next_index)
def show_previous_image(self, event=None) -> None:
if not getattr(self, "image_paths", None):
return
if not self.image_paths:
return
current = getattr(self, "current_image_index", -1)
prev_index = (current - 1) % len(self.image_paths)
self._display_image_by_index(prev_index)
def _set_image_collection(self, paths: Sequence[Path], start_index: int) -> None:
self.image_paths = list(paths)
if not self.image_paths:
return
self.exclude_shapes = []
self._rubber_start = None
self._rubber_id = None
self._stroke_preview_id = None
self._exclude_canvas_ids = []
self._exclude_mask = None
self._exclude_mask_px = None
self._exclude_mask_dirty = True
self.current_image_index = -1
self._display_image_by_index(max(0, start_index))
def _display_image_by_index(self, index: int) -> None:
if not self.image_paths:
return
if index < 0 or index >= len(self.image_paths):
return
path = self.image_paths[index]
if not path.exists():
messagebox.showerror(
self._t("dialog.error_title"),
self._t("dialog.file_missing", path=path),
)
return
try:
image = Image.open(path).convert("RGBA")
except Exception as exc:
messagebox.showerror(
self._t("dialog.error_title"),
self._t("dialog.image_open_failed", error=exc),
)
return
self.image_path = path
self.orig_img = image
if getattr(self, "reset_exclusions_on_switch", False):
self.exclude_shapes = []
self._rubber_start = None
self._rubber_id = None
self._stroke_preview_id = None
self._exclude_canvas_ids = []
self._exclude_mask = None
self._exclude_mask_px = None
self._exclude_mask_dirty = True
self.pick_mode = False
self.prepare_preview()
self.update_preview()
dimensions = f"{self.orig_img.width}x{self.orig_img.height}"
suffix = f" [{index + 1}/{len(self.image_paths)}]" if len(self.image_paths) > 1 else ""
status_text = self._t("status.loaded", name=path.name, dimensions=dimensions, position=suffix)
self.status.config(text=status_text)
self.status_default_text = status_text
if hasattr(self, "filename_label"):
filename_text = self._t(
"status.filename_label",
name=path.name,
dimensions=dimensions,
position=suffix,
)
self.filename_label.config(text=filename_text)
self.current_image_index = index
def save_overlay(self) -> None:
if self.orig_img is None:
messagebox.showinfo(
self._t("dialog.info_title"),
self._t("dialog.no_image_loaded"),
)
return
if self.preview_img is None:
messagebox.showerror(
self._t("dialog.error_title"),
self._t("dialog.no_preview_available"),
)
return
overlay = self._build_overlay_image(
self.orig_img,
tuple(self.exclude_shapes),
alpha=int(self.alpha.get()),
scale_from_preview=self.preview_img.size,
is_match_fn=self.matches_target_color,
)
merged = Image.alpha_composite(self.orig_img.convert("RGBA"), overlay)
out_path = filedialog.asksaveasfilename(
defaultextension=".png",
filetypes=[("PNG", "*.png")],
title=self._t("dialog.save_overlay_title"),
)
if not out_path:
return
merged.save(out_path)
messagebox.showinfo(
self._t("dialog.saved_title"),
self._t("dialog.overlay_saved", path=out_path),
)
def prepare_preview(self) -> None:
if self.orig_img is None:
return
width, height = self.orig_img.size
max_w, max_h = PREVIEW_MAX_SIZE
scale = min(max_w / width, max_h / height, 1.0)
size = (max(1, int(width * scale)), max(1, int(height * scale)))
self.preview_img = self.orig_img.resize(size, Image.LANCZOS)
self.preview_tk = ImageTk.PhotoImage(self.preview_img)
self.canvas_orig.delete("all")
self.canvas_orig.config(width=size[0], height=size[1])
self.canvas_overlay.config(width=size[0], height=size[1])
self.canvas_orig.create_image(0, 0, anchor="nw", image=self.preview_tk)
self._exclude_mask = None
self._exclude_mask_px = None
self._exclude_mask_dirty = True
if getattr(self, "exclude_shapes", None):
self._ensure_exclude_mask()
def update_preview(self) -> None:
if self.preview_img is None:
return
self._ensure_exclude_mask()
merged = self.create_overlay_preview()
if merged is None:
return
self.overlay_tk = ImageTk.PhotoImage(merged)
self.canvas_overlay.delete("all")
self.canvas_overlay.create_image(0, 0, anchor="nw", image=self.overlay_tk)
self.canvas_orig.delete("all")
self.canvas_orig.create_image(0, 0, anchor="nw", image=self.preview_tk)
self._render_exclusion_overlays()
stats = self.compute_stats_preview()
if stats:
matches_all, total_all = stats["all"]
matches_keep, total_keep = stats["keep"]
matches_ex, total_ex = stats["excl"]
r_with = (matches_keep / total_keep * 100) if total_keep else 0.0
r_no = (matches_all / total_all * 100) if total_all else 0.0
excl_share = (total_ex / total_all * 100) if total_all else 0.0
excl_match = (matches_ex / total_ex * 100) if total_ex else 0.0
self.ratio_label.config(
text=self._t(
"stats.summary",
with_pct=r_with,
without_pct=r_no,
excluded_pct=excl_share,
excluded_match_pct=excl_match,
)
)
refresher = getattr(self, "_refresh_canvas_backgrounds", None)
if callable(refresher):
refresher()
else:
bg = "#0f0f10" if self.theme == "dark" else "#ffffff"
self.canvas_orig.configure(bg=bg)
self.canvas_overlay.configure(bg=bg)
def create_overlay_preview(self) -> Image.Image | None:
if self.preview_img is None:
return None
self._ensure_exclude_mask()
base = self.preview_img.convert("RGBA")
overlay = Image.new("RGBA", base.size, (0, 0, 0, 0))
draw = ImageDraw.Draw(overlay)
pixels = base.load()
mask_px = self._exclude_mask_px
width, height = base.size
alpha = int(self.alpha.get())
for y in range(height):
for x in range(width):
if mask_px is not None and mask_px[x, y]:
continue
r, g, b, a = pixels[x, y]
if a == 0:
continue
if self.matches_target_color(r, g, b):
draw.point((x, y), fill=(255, 0, 0, alpha))
merged = Image.alpha_composite(base, overlay)
outline = ImageDraw.Draw(merged)
accent_dark = (255, 215, 0, 200)
accent_light = (197, 98, 23, 200)
accent = accent_dark if getattr(self, "theme", "light") == "dark" else accent_light
for shape in getattr(self, "exclude_shapes", []):
if shape.get("kind") == "rect":
x0, y0, x1, y1 = shape["coords"] # type: ignore[index]
outline.rectangle([x0, y0, x1, y1], outline=accent, width=3)
elif shape.get("kind") == "polygon":
points = shape.get("points", [])
if len(points) < 2:
continue
path = points if points[0] == points[-1] else points + [points[0]]
outline.line(path, fill=accent, width=2, joint="round")
return merged
def compute_stats_preview(self):
if self.preview_img is None:
return None
self._ensure_exclude_mask()
px = self.preview_img.convert("RGBA").load()
mask_px = self._exclude_mask_px
width, height = self.preview_img.size
matches_all = total_all = 0
matches_keep = total_keep = 0
matches_excl = total_excl = 0
for y in range(height):
for x in range(width):
r, g, b, a = px[x, y]
if a == 0:
continue
excluded = bool(mask_px and mask_px[x, y])
total_all += 1
if self.matches_target_color(r, g, b):
matches_all += 1
if not excluded:
total_keep += 1
if self.matches_target_color(r, g, b):
matches_keep += 1
else:
total_excl += 1
if self.matches_target_color(r, g, b):
matches_excl += 1
return {
"all": (matches_all, total_all),
"keep": (matches_keep, total_keep),
"excl": (matches_excl, total_excl),
}
def matches_target_color(self, r, g, b) -> bool:
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
hue = h * 360.0
hmin = float(self.hue_min.get())
hmax = float(self.hue_max.get())
smin = float(self.sat_min.get()) / 100.0
vmin = float(self.val_min.get()) / 100.0
vmax = float(self.val_max.get()) / 100.0
if hmin <= hmax:
hue_ok = hmin <= hue <= hmax
else:
hue_ok = (hue >= hmin) or (hue <= hmax)
return hue_ok and (s >= smin) and (v >= vmin) and (v <= vmax)
def _is_excluded(self, x: int, y: int) -> bool:
self._ensure_exclude_mask()
if self._exclude_mask_px is None:
return False
try:
return bool(self._exclude_mask_px[x, y])
except Exception:
return False
@classmethod
def _build_overlay_image(
cls,
image: Image.Image,
shapes: Iterable[dict[str, object]],
*,
alpha: int,
scale_from_preview: Tuple[int, int],
is_match_fn,
) -> Image.Image:
overlay = Image.new("RGBA", image.size, (0, 0, 0, 0))
draw = ImageDraw.Draw(overlay)
pixels = image.load()
width, height = image.size
mask = cls._build_exclude_mask_for_size(tuple(shapes), scale_from_preview, image.size)
mask_px = mask.load() if mask else None
for y in range(height):
for x in range(width):
if mask_px is not None and mask_px[x, y]:
continue
r, g, b, a = pixels[x, y]
if a == 0:
continue
if is_match_fn(r, g, b):
draw.point((x, y), fill=(255, 0, 0, alpha))
return overlay
@classmethod
def _build_exclude_mask_for_size(
cls,
shapes: Iterable[dict[str, object]],
preview_size: Tuple[int, int],
target_size: Tuple[int, int],
) -> Image.Image | None:
if not preview_size or not target_size or preview_size[0] == 0 or preview_size[1] == 0:
return None
mask = Image.new("L", target_size, 0)
draw = ImageDraw.Draw(mask)
scale_x = target_size[0] / preview_size[0]
scale_y = target_size[1] / preview_size[1]
for shape in shapes:
kind = shape.get("kind")
cls._draw_shape_on_mask(draw, shape, scale_x=scale_x, scale_y=scale_y)
return mask
def _ensure_exclude_mask(self) -> None:
if self.preview_img is None:
return
size = self.preview_img.size
if (
self._exclude_mask is None
or self._exclude_mask.size != size
or getattr(self, "_exclude_mask_dirty", False)
):
self._exclude_mask = Image.new("L", size, 0)
draw = ImageDraw.Draw(self._exclude_mask)
for shape in getattr(self, "exclude_shapes", []):
self._draw_shape_on_mask(draw, shape, scale_x=1.0, scale_y=1.0)
self._exclude_mask_px = self._exclude_mask.load()
self._exclude_mask_dirty = False
elif self._exclude_mask_px is None:
self._exclude_mask_px = self._exclude_mask.load()
def _stamp_shape_on_mask(self, shape: dict[str, object]) -> None:
if self.preview_img is None:
return
if self._exclude_mask is None or self._exclude_mask.size != self.preview_img.size:
self._exclude_mask_dirty = True
return
draw = ImageDraw.Draw(self._exclude_mask)
self._draw_shape_on_mask(draw, shape, scale_x=1.0, scale_y=1.0)
self._exclude_mask_px = self._exclude_mask.load()
@staticmethod
def _draw_shape_on_mask(
draw: ImageDraw.ImageDraw,
shape: dict[str, object],
*,
scale_x: float,
scale_y: float,
) -> None:
kind = shape.get("kind")
if kind == "rect":
x0, y0, x1, y1 = shape["coords"] # type: ignore[index]
draw.rectangle(
[
x0 * scale_x,
y0 * scale_y,
x1 * scale_x,
y1 * scale_y,
],
fill=255,
)
elif kind == "polygon":
points = shape.get("points")
if not points or len(points) < 2:
return
scaled = [(px * scale_x, py * scale_y) for px, py in points] # type: ignore[misc]
draw.polygon(scaled, fill=255)
def _render_exclusion_overlays(self) -> None:
if not hasattr(self, "canvas_orig"):
return
for item in getattr(self, "_exclude_canvas_ids", []):
try:
self.canvas_orig.delete(item)
except Exception:
pass
self._exclude_canvas_ids = []
accent_dark = "#ffd700"
accent_light = "#c56217"
accent = accent_dark if getattr(self, "theme", "light") == "dark" else accent_light
for shape in getattr(self, "exclude_shapes", []):
kind = shape.get("kind")
if kind == "rect":
x0, y0, x1, y1 = shape["coords"] # type: ignore[index]
item = self.canvas_orig.create_rectangle(
x0, y0, x1, y1, outline=accent, width=3
)
self._exclude_canvas_ids.append(item)
elif kind == "polygon":
points = shape.get("points")
if not points or len(points) < 2:
continue
closed = points if points[0] == points[-1] else points + [points[0]] # type: ignore[operator]
coords = [coord for point in closed for coord in point] # type: ignore[misc]
item = self.canvas_orig.create_line(
*coords,
fill=accent,
width=2,
smooth=True,
capstyle="round",
joinstyle="round",
)
self._exclude_canvas_ids.append(item)
__all__ = ["ImageProcessingMixin"]
-36
View File
@@ -1,36 +0,0 @@
"""Utility mixin for restoring default slider values."""
from __future__ import annotations
class ResetMixin:
def reset_sliders(self):
self.hue_min.set(self.DEFAULTS["hue_min"])
self.hue_max.set(self.DEFAULTS["hue_max"])
self.sat_min.set(self.DEFAULTS["sat_min"])
self.val_min.set(self.DEFAULTS["val_min"])
self.val_max.set(self.DEFAULTS["val_max"])
self.alpha.set(self.DEFAULTS["alpha"])
self.update_preview()
try:
default_hex = self._default_colour_hex() # type: ignore[attr-defined]
except Exception:
default_hex = None
if default_hex and hasattr(self, "_parse_hex_colour") and hasattr(self, "_update_selected_colour"):
try:
rgb = self._parse_hex_colour(default_hex) # type: ignore[attr-defined]
except Exception:
rgb = None
if rgb:
try:
self._update_selected_colour(*rgb) # type: ignore[arg-type,attr-defined]
except Exception:
pass
default_text = getattr(self, "status_default_text", None)
if default_text is None:
default_text = self._t("status.defaults_restored") if hasattr(self, "_t") else "Defaults restored."
if hasattr(self, "status"):
self.status.config(text=default_text)
__all__ = ["ResetMixin"]
+7
View File
@@ -0,0 +1,7 @@
"""PySide6 application entry points."""
from __future__ import annotations
from .app import create_application, run
__all__ = ["create_application", "run"]
+69
View File
@@ -0,0 +1,69 @@
"""Application bootstrap for the PySide6 GUI."""
from __future__ import annotations
import sys
from pathlib import Path
from PySide6 import QtCore, QtGui, QtWidgets
from app.logic import DEFAULTS, LANGUAGE, RESET_EXCLUSIONS_ON_IMAGE_CHANGE
from .main_window import MainWindow
def create_application() -> QtWidgets.QApplication:
"""Create the Qt application instance with customised styling."""
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
app.setOrganizationName("ICRA")
app.setApplicationName("Interactive Color Range Analyzer")
app.setApplicationDisplayName("ICRA")
palette = QtGui.QPalette()
palette.setColor(QtGui.QPalette.Window, QtGui.QColor("#111216"))
palette.setColor(QtGui.QPalette.WindowText, QtGui.QColor("#f5f5f5"))
palette.setColor(QtGui.QPalette.Base, QtGui.QColor("#1a1b21"))
palette.setColor(QtGui.QPalette.AlternateBase, QtGui.QColor("#20212a"))
palette.setColor(QtGui.QPalette.Button, QtGui.QColor("#20212a"))
palette.setColor(QtGui.QPalette.ButtonText, QtGui.QColor("#f5f5f5"))
palette.setColor(QtGui.QPalette.Text, QtGui.QColor("#f5f5f5"))
palette.setColor(QtGui.QPalette.Highlight, QtGui.QColor("#5168ff"))
palette.setColor(QtGui.QPalette.HighlightedText, QtGui.QColor("#ffffff"))
app.setPalette(palette)
font = QtGui.QFont("Segoe UI", 10)
app.setFont(font)
logo_path = Path(__file__).resolve().parents[1] / "assets" / "logo.png"
if logo_path.exists():
app.setWindowIcon(QtGui.QIcon(str(logo_path)))
return app
def run() -> int:
"""Run the PySide6 GUI."""
app = create_application()
from app.logic import OVERLAY_COLOR, EXCLUDE_BG_COLOR, EXCLUDE_BG_TOLERANCE, WEIGHTS
window = MainWindow(
language=LANGUAGE,
defaults=DEFAULTS.copy(),
reset_exclusions=RESET_EXCLUSIONS_ON_IMAGE_CHANGE,
weights=WEIGHTS.copy(),
overlay_color=OVERLAY_COLOR,
exclude_bg_color=EXCLUDE_BG_COLOR,
exclude_bg_tolerance=EXCLUDE_BG_TOLERANCE,
)
# Respect saved geometry from QSettings; fall back to maximised on first launch
settings = QtCore.QSettings("ICRA", "MainWindow")
if settings.value("geometry"):
window.show()
else:
primary_screen = app.primaryScreen()
if primary_screen is not None:
window.setGeometry(primary_screen.availableGeometry())
window.showMaximized()
return app.exec()
+905
View File
@@ -0,0 +1,905 @@
"""Minimal image processing pipeline adapted for the Qt frontend."""
from __future__ import annotations
import colorsys
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, Iterable, Tuple
import numpy as np
from PIL import Image, ImageDraw
from PySide6 import QtGui
from app.logic import PREVIEW_MAX_SIZE
@dataclass
class Stats:
matches_all: int = 0
total_all: int = 0
matches_keep: int = 0
total_keep: int = 0
matches_excl: int = 0
total_excl: int = 0
brightness_score: float = 0.0
grouping_score: float = 0.0
continuity_score: float = 0.0
border_score: float = 0.0
prefer_dark: bool = False
@property
def effective_brightness(self) -> float:
"""Returns inverted brightness when prefer_dark is on."""
return (100.0 - self.brightness_score) if self.prefer_dark else self.brightness_score
def composite_score(self, weights: dict[str, int]) -> float:
"""Calculates weighted composite based on provided weights (0-100)."""
pct_all = (self.matches_all / self.total_all * 100) if self.total_all else 0.0
pct_keep = (self.matches_keep / self.total_keep * 100) if self.total_keep else 0.0
# weights keys: match_all, match_keep, brightness, grouping, continuity, border
w_all = weights.get("match_all", 30) / 100.0
w_keep = weights.get("match_keep", 30) / 100.0
w_bright = weights.get("brightness", 10) / 100.0
w_group = weights.get("grouping", 10) / 100.0
w_cont = weights.get("continuity", 10) / 100.0
w_bord = weights.get("border", 10) / 100.0
return (w_all * pct_all +
w_keep * pct_keep +
w_bright * self.effective_brightness +
w_group * self.grouping_score +
w_cont * self.continuity_score +
w_bord * self.border_score)
def summary(self, translate, weights: dict[str, int]) -> str:
if self.total_all == 0:
return translate("stats.placeholder")
with_pct = (self.matches_keep / self.total_keep * 100) if self.total_keep else 0.0
without_pct = (self.matches_all / self.total_all * 100) if self.total_all else 0.0
excluded_pct = (self.total_excl / self.total_all * 100) if self.total_all else 0.0
brightness_label = translate("stats.darkness_label") if self.prefer_dark else translate("stats.brightness_label")
score = self.composite_score(weights)
return translate(
"stats.summary",
score=score,
with_pct=with_pct,
without_pct=without_pct,
brightness_label=brightness_label,
brightness=self.effective_brightness,
grouping=self.grouping_score,
continuity=self.continuity_score,
border=self.border_score,
excluded_pct=excluded_pct,
)
def _rgb_to_hsv_numpy(arr: np.ndarray) -> np.ndarray:
"""Vectorized RGB→HSV conversion. arr shape: (H, W, 3), dtype float32, range [0,1].
Returns array of same shape with channels [H(0-360), S(0-100), V(0-100)].
"""
r = arr[..., 0]
g = arr[..., 1]
b = arr[..., 2]
cmax = np.maximum(np.maximum(r, g), b)
cmin = np.minimum(np.minimum(r, g), b)
delta = cmax - cmin
# Value
v = cmax
# Saturation
s = np.zeros_like(r)
np.divide(delta, cmax, out=s, where=cmax > 0)
# Hue
h = np.zeros_like(r)
mask_r = (delta > 0) & (cmax == r)
mask_g = (delta > 0) & (cmax == g)
mask_b = (delta > 0) & (cmax == b)
h[mask_r] = (60.0 * ((g[mask_r] - b[mask_r]) / delta[mask_r])) % 360.0
h[mask_g] = (60.0 * ((b[mask_g] - r[mask_g]) / delta[mask_g]) + 120.0) % 360.0
h[mask_b] = (60.0 * ((r[mask_b] - g[mask_b]) / delta[mask_b]) + 240.0) % 360.0
return np.stack([h, s * 100.0, v * 100.0], axis=-1)
def _calculate_border_score(mask: np.ndarray, val: np.ndarray, alpha_ch: np.ndarray, prefer_dark: bool, excl_mask: np.ndarray | None = None) -> float:
"""Measure border cleanliness: penalizes extremely dark (or bright) pixels along the match perimeter.
Uses Top-10% percentile to ensure local artifacts (halos) aren't diluted by clean edges.
"""
if not mask.any():
return 100.0
dilated = mask.copy()
# Manual morphological 1-pixel dilation
dilated[:-1, :] |= mask[1:, :]
dilated[1:, :] |= mask[:-1, :]
dilated[:, :-1] |= mask[:, 1:]
dilated[:, 1:] |= mask[:, :-1]
dil2 = dilated.copy()
dil2[:-1, :] |= dilated[1:, :]
dil2[1:, :] |= dilated[:-1, :]
dil2[:, :-1] |= dilated[:, 1:]
dil2[:, 1:] |= dilated[:, :-1]
# Target exterior pixels that aren't transparent and NOT excluded
outer = dil2 & ~mask & (alpha_ch >= 128)
if excl_mask is not None:
outer &= ~excl_mask
if not outer.any():
return 100.0
border_vals = val[outer]
if prefer_dark:
# Penalize super bright edges (white/silver > 60)
penalties = np.clip(border_vals - 60.0, 0, None)
else:
# Penalize super dark edges (black/heavy shadows < 40)
penalties = np.clip(40.0 - border_vals, 0, None)
# Hammer down harsh cuts: focus on the 'worst' parts of the border
if not penalties.any():
return 100.0
# Using 4th power penalty for 'catastrophic' edge detection.
# A single pitch-black line (high diff) is now exponentially worse than a gray transition.
total_penalty = np.sum(penalties ** 4)
# Collector's Grade: only 20 pixels at full intensity (40^4)
# are required for a 1% drop in the Border Score.
max_penalty_sum = 20.0 * (40.0 ** 4)
score = 100.0 * (1.0 - (total_penalty / max_penalty_sum))
return max(0.0, float(score))
def _export_worker(args: tuple) -> tuple:
"""Standalone worker for ProcessPoolExecutor batch export.
Receives ``(image_path, params)`` where *params* is the dict produced by
``QtImageProcessor.get_export_params()``. Opens the image, runs the
full stats pipeline, and returns a plain results tuple. No processor
instance is needed so nothing has to be pickled.
"""
image_path, params = args
from pathlib import Path
try:
img_path = Path(image_path)
hue_min = params["hue_min"]
hue_max = params["hue_max"]
sat_min = params["sat_min"]
sat_max = params["sat_max"]
val_min = params["val_min"]
val_max = params["val_max"]
exclude_bg = params["exclude_bg"]
exclude_bg_rgb = tuple(params["exclude_bg_rgb"])
exclude_bg_tolerance = params["exclude_bg_tolerance"]
prefer_dark = params["prefer_dark"]
exclude_shapes = params["exclude_shapes"]
exclude_ref_size = params["exclude_ref_size"]
img = Image.open(img_path).convert("RGBA")
arr = np.asarray(img, dtype=np.float32)
rgb = arr[..., :3] / 255.0
alpha_ch = arr[..., 3].copy()
if exclude_bg:
r_bg, g_bg, b_bg = exclude_bg_rgb
tol = exclude_bg_tolerance
bg_mask = (
(np.abs(arr[..., 0] - r_bg) <= tol) &
(np.abs(arr[..., 1] - g_bg) <= tol) &
(np.abs(arr[..., 2] - b_bg) <= tol)
)
alpha_ch[bg_mask] = 0
hsv = _rgb_to_hsv_numpy(rgb)
hue = hsv[..., 0]
sat = hsv[..., 1]
val = hsv[..., 2]
if hue_min <= hue_max:
hue_ok = (hue >= hue_min) & (hue <= hue_max)
else:
hue_ok = (hue >= hue_min) | (hue <= hue_max)
match_mask = (
hue_ok
& (sat >= sat_min)
& (sat <= sat_max)
& (val >= val_min)
& (val <= val_max)
& (alpha_ch >= 128)
)
# Build exclusion mask
w, h = img.size
if not exclude_shapes:
excl_mask = np.zeros((h, w), dtype=bool)
else:
target_w, target_h = w, h
ref_w, ref_h = exclude_ref_size or (w, h)
sx = target_w / ref_w if ref_w > 0 else 1.0
sy = target_h / ref_h if ref_h > 0 else 1.0
mask_img = Image.new("L", (w, h), 0)
draw = ImageDraw.Draw(mask_img)
for shape in exclude_shapes:
kind = shape.get("kind")
if kind == "rect":
x0, y0, x1, y1 = shape["coords"]
draw.rectangle([x0 * sx, y0 * sy, x1 * sx, y1 * sy], fill=255)
elif kind == "polygon":
points = shape.get("points", [])
if len(points) >= 3:
scaled_pts = [(int(px * sx), int(py * sy)) for px, py in points]
draw.polygon(scaled_pts, fill=255)
excl_mask = np.asarray(mask_img, dtype=bool)
keep_match = match_mask & ~excl_mask
visible = alpha_ch >= 128
keep_visible = visible & ~excl_mask
if keep_visible.any():
v_vals = val[keep_visible]
mean_v = float(v_vals.mean())
std_v = float(v_vals.std())
# Collector's Purity: multiply mean by a factor derived from variance
# A perfectly uniform pattern (std=0) gets 100% of its mean.
# Blotchy patterns (std > 10) get a significant reduction.
purity_factor = max(0.0, 1.0 - (std_v / 20.0))
brightness = mean_v * purity_factor
else:
brightness = 0.0
# Grouping score (inline for worker isolation)
if not keep_match.any():
grouping = 0.0
else:
mh, mw = keep_match.shape
padded = np.pad(keep_match, 5, mode='constant', constant_values=0)
cumsum = padded.astype(np.int32).cumsum(axis=0).cumsum(axis=1)
y2, x2 = np.arange(9, 9 + mh)[:, None], np.arange(9, 9 + mw)
y1_1, x1_1 = np.arange(0, mh)[:, None], np.arange(0, mw)
window_sums = cumsum[y2, x2] - cumsum[y1_1, x2] - cumsum[y2, x1_1] + cumsum[y1_1, x1_1]
neighbors = (window_sums - keep_match.astype(np.int32)).clip(min=0)
match_neighbors = neighbors[keep_match]
grouping = float(((match_neighbors / 80.0) ** 2).mean() * 100.0)
matches_all = int(match_mask[visible].sum())
total_all = int(visible.sum())
matches_keep = int(keep_match[visible].sum())
total_keep = int(keep_visible.sum())
# Continuity score (inline for worker isolation)
continuity = 0.0
if keep_match.any():
area = keep_match.sum()
y_idx, x_idx = np.nonzero(keep_match)
unvisited = set(zip(y_idx, x_idx))
max_cc_area = 0
while unvisited:
start_node = unvisited.pop()
queue = [start_node]
cc_area = 0
while queue:
cy, cx = queue.pop()
cc_area += 1
for ny, nx in ((cy-1, cx), (cy+1, cx), (cy, cx-1), (cy, cx+1)):
if (ny, nx) in unvisited:
unvisited.remove((ny, nx))
queue.append((ny, nx))
if cc_area > max_cc_area:
max_cc_area = cc_area
continuity = float(max_cc_area / area * 100.0) if area > 0 else 0.0
eff_brightness = (100.0 - brightness) if prefer_dark else brightness
# Border Cleanliness score calculation using standalone util
border = _calculate_border_score(keep_match, val, alpha_ch, prefer_dark, excl_mask)
pct_all = (matches_all / total_all * 100) if total_all else 0.0
pct_keep = (matches_keep / total_keep * 100) if total_keep else 0.0
weights = params["weights"]
w_all = weights.get("match_all", 30) / 100.0
w_keep = weights.get("match_keep", 30) / 100.0
w_bright = weights.get("brightness", 10) / 100.0
w_group = weights.get("grouping", 10) / 100.0
w_cont = weights.get("continuity", 10) / 100.0
w_bord = weights.get("border", 10) / 100.0
composite = (w_all * pct_all + w_keep * pct_keep + w_bright * eff_brightness +
w_group * grouping + w_cont * continuity + w_bord * border)
img.close()
return (img_path.name, pct_all, pct_keep, eff_brightness, grouping, continuity, border, composite)
except Exception:
return (img_path.name, None, None, None, None, None, None, None)
class QtImageProcessor:
"""Process images and build overlays for the Qt UI."""
def __init__(self) -> None:
self.orig_img: Image.Image | None = None
self.preview_img: Image.Image | None = None
self.overlay_img: Image.Image | None = None
self.preview_paths: list[Path] = []
self.current_index: int = -1
self.stats = Stats()
# Overlay tint color
self.overlay_r = 255
self.overlay_g = 0
self.overlay_b = 0
self.defaults: Dict[str, int] = {
"hue_min": 0,
"hue_max": 360,
"sat_min": 25,
"sat_max": 100,
"val_min": 15,
"val_max": 100,
"alpha": 120,
}
self.hue_min = self.defaults["hue_min"]
self.hue_max = self.defaults["hue_max"]
self.sat_min = self.defaults["sat_min"]
self.sat_max = self.defaults["sat_max"]
self.val_min = self.defaults["val_min"]
self.val_max = self.defaults["val_max"]
self.alpha = self.defaults["alpha"]
self.exclude_shapes: list[dict[str, object]] = []
self.reset_exclusions_on_switch: bool = False
# Mask caching
self._cached_mask: np.ndarray | None = None
self._cached_mask_size: Tuple[int, int] | None = None
self.exclude_ref_size: Tuple[int, int] | None = None
self.prefer_dark: bool = False
self.exclude_bg: bool = True
self.exclude_bg_rgb: Tuple[int, int, int] = (31, 41, 55)
self.exclude_bg_tolerance: int = 5
self.weights: Dict[str, int] = {
"match_all": 20,
"match_keep": 20,
"brightness": 10,
"grouping": 10,
"continuity": 20,
"border": 20
}
def set_defaults(self, defaults: dict) -> None:
for key in self.defaults:
if key in defaults:
self.defaults[key] = int(defaults[key])
for key, value in self.defaults.items():
setattr(self, key, value)
self._rebuild_overlay()
# thresholds -------------------------------------------------------------
def set_threshold(self, key: str, value: int) -> None:
setattr(self, key, value)
if self.preview_img is not None:
self._rebuild_overlay()
# image handling --------------------------------------------------------
def load_single_image(self, path: Path, *, reset_collection: bool = True) -> Path:
image = Image.open(path).convert("RGBA")
self.orig_img = image
if reset_collection:
self.preview_paths = [path]
self.current_index = 0
self._build_preview()
self._rebuild_overlay()
return path
def load_folder(self, paths: Iterable[Path], start_index: int = 0) -> Path:
self.preview_paths = list(paths)
if not self.preview_paths:
raise ValueError("No images in folder.")
self.current_index = max(0, min(start_index, len(self.preview_paths) - 1))
return self._load_image_at_current()
def next_image(self) -> Path | None:
if not self.preview_paths:
return None
self.current_index = (self.current_index + 1) % len(self.preview_paths)
return self._load_image_at_current()
def previous_image(self) -> Path | None:
if not self.preview_paths:
return None
self.current_index = (self.current_index - 1) % len(self.preview_paths)
return self._load_image_at_current()
def _load_image_at_current(self) -> Path:
path = self.preview_paths[self.current_index]
return self.load_single_image(path, reset_collection=False)
# preview/overlay -------------------------------------------------------
def _build_preview(self) -> None:
if self.orig_img is None:
self.preview_img = None
return
img_to_process = self.orig_img.convert("RGBA")
if self.exclude_bg:
# Mask the background color with tolerance on the original image before resizing
# this prevents interpolation artifacts from leaving a background 'halo'
arr = np.array(img_to_process)
r_bg, g_bg, b_bg = self.exclude_bg_rgb
tol = self.exclude_bg_tolerance
bg_mask = (
(np.abs(arr[..., 0].astype(np.int16) - r_bg) <= tol) &
(np.abs(arr[..., 1].astype(np.int16) - g_bg) <= tol) &
(np.abs(arr[..., 2].astype(np.int16) - b_bg) <= tol)
)
arr[bg_mask, 3] = 0
img_to_process = Image.fromarray(arr, "RGBA")
width, height = img_to_process.size
max_w, max_h = PREVIEW_MAX_SIZE
scale = min(max_w / width, max_h / height)
if scale <= 0:
scale = 1.0
size = (max(1, int(width * scale)), max(1, int(height * scale)))
self.preview_img = img_to_process.resize(size, Image.LANCZOS)
def _rebuild_overlay(self) -> None:
"""Build color-match overlay using vectorized NumPy operations."""
if self.preview_img is None:
self.overlay_img = None
self.stats = Stats()
return
base = self.preview_img.convert("RGBA")
arr = np.asarray(base, dtype=np.float32) # (H, W, 4)
rgb = arr[..., :3] / 255.0
alpha_ch = arr[..., 3].copy() # alpha channel of the image
if self.exclude_bg:
# Exclude specific background color
r_bg, g_bg, b_bg = self.exclude_bg_rgb
tol = self.exclude_bg_tolerance
bg_mask = (
(np.abs(arr[..., 0] - r_bg) <= tol) &
(np.abs(arr[..., 1] - g_bg) <= tol) &
(np.abs(arr[..., 2] - b_bg) <= tol)
)
alpha_ch[bg_mask] = 0
hsv = _rgb_to_hsv_numpy(rgb) # (H, W, 3): H°, S%, V%
hue = hsv[..., 0]
sat = hsv[..., 1]
val = hsv[..., 2]
hue_min = float(self.hue_min)
hue_max = float(self.hue_max)
if hue_min <= hue_max:
hue_ok = (hue >= hue_min) & (hue <= hue_max)
else:
hue_ok = (hue >= hue_min) | (hue <= hue_max)
match_mask = (
hue_ok
& (sat >= float(self.sat_min))
& (sat <= float(self.sat_max))
& (val >= float(self.val_min))
& (val <= float(self.val_max))
& (alpha_ch >= 128)
)
# Exclusion mask (same pixel space as preview)
excl_mask = self._build_exclusion_mask_numpy(base.size) # bool (H,W)
keep_match = match_mask & ~excl_mask
excl_match = match_mask & excl_mask
visible = alpha_ch >= 128
matches_all = int(match_mask[visible].sum())
total_all = int(visible.sum())
matches_keep = int(keep_match[visible].sum())
total_keep = int((visible & ~excl_mask).sum())
matches_excl = int(excl_match[visible].sum())
total_excl = int((visible & excl_mask).sum())
# Brightness: mean Value (0-100) of ALL non-excluded visible pixels
keep_visible = visible & ~excl_mask
if keep_visible.any():
v_vals = val[keep_visible]
mean_v = float(v_vals.mean())
std_v = float(v_vals.std())
# Purity factor: subtract deviation from mean to punish blotchy patterns
brightness = max(0.0, mean_v - (std_v * 1.5))
else:
brightness = 0.0
# Grouping: measure clustering of match_mask
grouping = self._calculate_grouping_score(keep_match)
# Continuity: Measure connectivity of matched area
continuity = self._calculate_continuity_score(keep_match)
# Border Cleanliness: Calculate hard edges based on preference
border = _calculate_border_score(keep_match, val, alpha_ch, self.prefer_dark, excl_mask)
# Build overlay image
overlay_arr = np.zeros((base.height, base.width, 4), dtype=np.uint8)
overlay_arr[keep_match, 0] = self.overlay_r
overlay_arr[keep_match, 1] = self.overlay_g
overlay_arr[keep_match, 2] = self.overlay_b
overlay_arr[keep_match, 3] = int(self.alpha)
self.overlay_img = Image.fromarray(overlay_arr, "RGBA")
self.stats = Stats(
matches_all=matches_all,
total_all=total_all,
matches_keep=matches_keep,
total_keep=total_keep,
matches_excl=matches_excl,
total_excl=total_excl,
brightness_score=brightness,
grouping_score=grouping,
continuity_score=continuity,
border_score=border,
prefer_dark=self.prefer_dark,
)
def get_stats_headless(self, image: Image.Image) -> Stats:
"""Calculate color-match statistics natively without building UI elements or scaling."""
base = image.convert("RGBA")
arr = np.asarray(base, dtype=np.float32)
rgb = arr[..., :3] / 255.0
alpha_ch = arr[..., 3].copy()
if self.exclude_bg:
# Exclude background color with tolerance
r_bg, g_bg, b_bg = self.exclude_bg_rgb
tol = self.exclude_bg_tolerance
bg_mask = (
(np.abs(arr[..., 0] - r_bg) <= tol) &
(np.abs(arr[..., 1] - g_bg) <= tol) &
(np.abs(arr[..., 2] - b_bg) <= tol)
)
alpha_ch[bg_mask] = 0
hsv = _rgb_to_hsv_numpy(rgb)
hue = hsv[..., 0]
sat = hsv[..., 1]
val = hsv[..., 2]
hue_min = float(self.hue_min)
hue_max = float(self.hue_max)
if hue_min <= hue_max:
hue_ok = (hue >= hue_min) & (hue <= hue_max)
else:
hue_ok = (hue >= hue_min) | (hue <= hue_max)
match_mask = (
hue_ok
& (sat >= float(self.sat_min))
& (sat <= float(self.sat_max))
& (val >= float(self.val_min))
& (val <= float(self.val_max))
& (alpha_ch >= 128)
)
excl_mask = self._build_exclusion_mask_numpy(base.size)
keep_match = match_mask & ~excl_mask
excl_match = match_mask & excl_mask
visible = alpha_ch >= 128
matches_keep_count = int(keep_match[visible].sum())
keep_visible = visible & ~excl_mask
if keep_visible.any():
v_vals = val[keep_visible]
mean_v = float(v_vals.mean())
std_v = float(v_vals.std())
# Collector's Purity: multiply mean by a factor derived from variance
# A perfectly uniform pattern (std=0) gets 100% of its mean.
# Blotchy patterns (std > 10) get a significant reduction.
purity_factor = max(0.0, 1.0 - (std_v / 20.0))
brightness = mean_v * purity_factor
else:
brightness = 0.0
grouping = self._calculate_grouping_score(keep_match)
continuity = self._calculate_continuity_score(keep_match)
border = _calculate_border_score(keep_match, val, alpha_ch, self.prefer_dark, excl_mask)
return Stats(
matches_all=int(match_mask[visible].sum()),
total_all=int(visible.sum()),
matches_keep=matches_keep_count,
total_keep=int((visible & ~excl_mask).sum()),
matches_excl=int(excl_match[visible].sum()),
total_excl=int((visible & excl_mask).sum()),
brightness_score=brightness,
grouping_score=grouping,
continuity_score=continuity,
border_score=border,
prefer_dark=self.prefer_dark,
)
def _calculate_grouping_score(self, mask: np.ndarray) -> float:
"""Measure clustering: average density in a 9x9 neighborhood (0-100)."""
if not mask.any():
return 0.0
h, w = mask.shape
# Use cumulative sums for O(1) box sum calculation
padded = np.pad(mask, 5, mode='constant', constant_values=0)
cumsum = padded.astype(np.int32).cumsum(axis=0).cumsum(axis=1)
# Indices for 9x9 windows centered at each mask pixel
y2, x2 = np.arange(9, 9 + h)[:, None], np.arange(9, 9 + w)
y1_1, x1_1 = np.arange(0, h)[:, None], np.arange(0, w)
# Box sum formula: S(window) = S(x2,y2) - S(x1-1,y2) - S(x2,y1-1) + S(x1-1,y1-1)
window_sums = cumsum[y2, x2] - cumsum[y1_1, x2] - cumsum[y2, x1_1] + cumsum[y1_1, x1_1]
# Max neighbors in 9x9 is 80 (excluding the center pixel itself)
neighbors = (window_sums - mask.astype(np.int32)).clip(min=0)
match_neighbors = neighbors[mask]
# Square the density to heavily penalize thin bridges and frayed edges
score = ( (match_neighbors / 80.0) ** 2 ).mean() * 100.0
return float(score)
def _calculate_continuity_score(self, mask: np.ndarray) -> float:
"""Measure continuity: largest connected component ratio and surface smoothness (0-100).
Penalizes jaggedness and 'perforated' patterns with many internal holes.
"""
if not mask.any():
return 0.0
area = mask.sum()
# 1. Connectivity Ratio
y_idx, x_idx = np.nonzero(mask)
unvisited = set(zip(y_idx, x_idx))
max_cc_area = 0
while unvisited:
start_node = unvisited.pop()
queue = [start_node]
cc_area = 0
while queue:
cy, cx = queue.pop()
cc_area += 1
for ny, nx in ((cy-1, cx), (cy+1, cx), (cy, cx-1), (cy, cx+1)):
if (ny, nx) in unvisited:
unvisited.remove((ny, nx))
queue.append((ny, nx))
if cc_area > max_cc_area:
max_cc_area = cc_area
connectivity = max_cc_area / area
# 2. Smoothness / Jaggedness (Perimeter-to-Area)
# Theoretically perfect smoothness (circle) has perimeter 2*sqrt(pi*area)
# We penalize departure from 'ideal' shape density
eroded = mask.copy()
eroded[:-1, :] &= mask[1:, :]
eroded[1:, :] &= mask[:-1, :]
eroded[:, :-1] &= mask[:, 1:]
eroded[:, 1:] &= mask[:, :-1]
perimeter = np.count_nonzero(mask ^ eroded)
# min_perim for a circle
min_perim = 2.0 * np.sqrt(np.pi * area)
# Jaggedness factor (0 is perfect, higher is messier)
# We normalize by the expected complexity of the item (e.g. 15 for Karambit)
# but here we use a general sensitivity factor
jaggedness = max(0.0, (perimeter / min_perim) - 1.0)
# Penalty increases as jaggedness goes up.
# For Urban Masked, we are more lenient (factor of 40 instead of 20)
smoothness_factor = 1.0 / (1.0 + (jaggedness / 40.0))
# 3. Island Count Penalty
# Premium patterns should be unified. Each separate piece (island)
# adds a small deduction to the continuity score.
y, x = np.nonzero(mask)
unvisited = set(zip(y, x))
islands = 0
while unvisited:
islands += 1
node = unvisited.pop()
q = [node]
while q:
cy, cx = q.pop()
for ny, nx in ((cy-1, cx), (cy+1, cx), (cy, cx-1), (cy, cx+1)):
if (ny, nx) in unvisited:
unvisited.remove((ny, nx))
q.append((ny, nx))
# Collector's factor: 2000 is now the baseline for Karambits.
island_factor = max(0.0, 1.0 - (islands / 2000.0))
score = connectivity * smoothness_factor * island_factor * 100.0
return float(score)
# helpers ----------------------------------------------------------------
def _matches(self, r: int, g: int, b: int) -> bool:
"""Single-pixel match — kept for compatibility / eyedropper use."""
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
hue = (h * 360.0) % 360.0
if self.hue_min <= self.hue_max:
hue_ok = self.hue_min <= hue <= self.hue_max
else:
hue_ok = hue >= self.hue_min or hue <= self.hue_max
sat_ok = self.sat_min <= s * 100.0 <= self.sat_max
val_ok = self.val_min <= v * 100.0 <= self.val_max
return hue_ok and sat_ok and val_ok
def pick_color(self, x: int, y: int) -> Tuple[float, float, float] | None:
"""Return (hue°, sat%, val%) of the preview pixel at (x, y), or None."""
if self.preview_img is None:
return None
img = self.preview_img.convert("RGBA")
try:
r, g, b, a = img.getpixel((x, y))
except IndexError:
return None
if a == 0:
return None
h, s, v = colorsys.rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
return (h * 360.0) % 360.0, s * 100.0, v * 100.0
# exported data ----------------------------------------------------------
def preview_pixmap(self) -> QtGui.QPixmap:
return self._to_pixmap(self.preview_img)
def overlay_pixmap(self) -> QtGui.QPixmap:
if self.preview_img is None:
return QtGui.QPixmap()
if self.overlay_img is None:
return self.preview_pixmap()
merged = Image.alpha_composite(self.preview_img.convert("RGBA"), self.overlay_img)
return self._to_pixmap(merged)
@staticmethod
def _to_pixmap(image: Image.Image | None) -> QtGui.QPixmap:
if image is None:
return QtGui.QPixmap()
buffer = image.tobytes("raw", "RGBA")
qt_image = QtGui.QImage(buffer, image.width, image.height, QtGui.QImage.Format_RGBA8888)
return QtGui.QPixmap.fromImage(qt_image)
# exclusions -------------------------------------------------------------
def set_exclusions(self, shapes: list[dict[str, object]], ref_size: Tuple[int, int] | None = None) -> None:
copied: list[dict[str, object]] = []
for shape in shapes:
kind = shape.get("kind")
if kind == "rect":
coords = tuple(shape.get("coords", (0, 0, 0, 0))) # type: ignore[assignment]
copied.append({"kind": "rect", "coords": tuple(int(c) for c in coords)})
elif kind == "polygon":
pts = shape.get("points", [])
copied.append({"kind": "polygon", "points": [(int(x), int(y)) for x, y in pts]})
self.exclude_shapes = copied
if ref_size:
self.exclude_ref_size = ref_size
elif self.preview_img:
self.exclude_ref_size = self.preview_img.size
else:
self.exclude_ref_size = None
self._cached_mask = None # Invalidate cache
self._cached_mask_size = None
self._rebuild_overlay()
def _build_exclusion_mask(self, size: Tuple[int, int]) -> Image.Image | None:
if not self.exclude_shapes:
return None
target_w, target_h = size
ref_w, ref_h = self.exclude_ref_size or size
sx = target_w / ref_w if ref_w > 0 else 1.0
sy = target_h / ref_h if ref_h > 0 else 1.0
mask = Image.new("L", size, 0)
draw = ImageDraw.Draw(mask)
for shape in self.exclude_shapes:
kind = shape.get("kind")
if kind == "rect":
x0, y0, x1, y1 = shape["coords"] # type: ignore[index]
draw.rectangle([x0 * sx, y0 * sy, x1 * sx, y1 * sy], fill=255)
elif kind == "polygon":
points = shape.get("points", [])
if len(points) >= 3:
scaled_pts = [(int(x * sx), int(y * sy)) for x, y in points]
draw.polygon(scaled_pts, fill=255)
return mask
def set_overlay_color(self, hex_code: str) -> None:
"""Set the RGB channels for the match overlay from a hex string."""
if not hex_code.startswith("#") or len(hex_code) not in (7, 9):
return
try:
self.overlay_r = int(hex_code[1:3], 16)
self.overlay_g = int(hex_code[3:5], 16)
self.overlay_b = int(hex_code[5:7], 16)
if self.preview_img:
self._rebuild_overlay()
except ValueError:
pass
def _build_exclusion_mask_numpy(self, size: Tuple[int, int]) -> np.ndarray:
"""Return a boolean (H, W) mask — True where pixels are excluded."""
if self._cached_mask is not None and self._cached_mask_size == size:
return self._cached_mask
w, h = size
if not self.exclude_shapes:
mask = np.zeros((h, w), dtype=bool)
else:
pil_mask = self._build_exclusion_mask(size)
if pil_mask is None:
mask = np.zeros((h, w), dtype=bool)
else:
mask = np.asarray(pil_mask, dtype=bool)
self._cached_mask = mask
self._cached_mask_size = size
return mask
def set_exclude_bg_color(self, hex_code: str, tolerance: int = 5) -> None:
"""Set the RGB channels for background exclusion from a hex string."""
self.exclude_bg_tolerance = tolerance
if not hex_code.startswith("#") or len(hex_code) not in (7, 9):
return
try:
r = int(hex_code[1:3], 16)
g = int(hex_code[3:5], 16)
b = int(hex_code[5:7], 16)
self.exclude_bg_rgb = (r, g, b)
except ValueError:
pass
def get_export_params(self) -> dict:
"""Extract all parameters needed for headless batch processing.
Called once before a batch export so that each worker receives
a plain dict instead of re-reading instance attributes.
"""
return {
"hue_min": float(self.hue_min),
"hue_max": float(self.hue_max),
"sat_min": float(self.sat_min),
"sat_max": float(self.sat_max),
"val_min": float(self.val_min),
"val_max": float(self.val_max),
"exclude_bg": self.exclude_bg,
"exclude_bg_rgb": self.exclude_bg_rgb,
"exclude_bg_tolerance": self.exclude_bg_tolerance,
"prefer_dark": self.prefer_dark,
"exclude_shapes": self.exclude_shapes,
"exclude_ref_size": self.exclude_ref_size,
"weights": self.weights,
}
@property
def exclude_bg_color_hex(self) -> str:
r, g, b = self.exclude_bg_rgb
return f"#{r:02x}{g:02x}{b:02x}"
@property
def overlay_color_hex(self) -> str:
return f"#{self.overlay_r:02x}{self.overlay_g:02x}{self.overlay_b:02x}"
File diff suppressed because it is too large Load Diff
+192
View File
@@ -0,0 +1,192 @@
"""Dialog and worker thread for batch downloading CSGOSkins patterns."""
from __future__ import annotations
import re
import time
import urllib.request
import urllib.error
from pathlib import Path
from PySide6 import QtCore, QtWidgets, QtGui
from app.i18n import I18nMixin
import concurrent.futures
class PatternDownloadWorker(QtCore.QThread):
progress = QtCore.Signal(int, int) # current, total
status = QtCore.Signal(str) # textual update
finished = QtCore.Signal(bool) # True if Success, False if Interrupted/Error
error = QtCore.Signal(str) # Error message
def __init__(self, slug: str, save_dir: Path, parent: QtCore.QObject | None = None) -> None:
super().__init__(parent)
self.slug = slug
self.save_dir = save_dir
self.total_seeds = 1000
def _download_seed(self, seed: int) -> tuple[bool, str | None]:
url = f"https://cdn.csgoskins.gg/public/images/patterns/v1/{self.slug}/{seed}.png"
filename = self.save_dir / f"{seed}.png"
if filename.exists():
try:
from PIL import Image
with Image.open(filename) as img:
img.verify()
return True, None
except Exception:
filename.unlink(missing_ok=True)
try:
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0 ICRA/1.0'})
with urllib.request.urlopen(req, timeout=10) as response:
with open(filename, 'wb') as f:
f.write(response.read())
try:
from PIL import Image
with Image.open(filename) as img:
img.verify()
return True, None
except Exception:
filename.unlink(missing_ok=True)
return False, "Downloaded file is invalid/corrupt"
except urllib.error.HTTPError as e:
return False, f"HTTP {e.code}"
except Exception as e:
filename.unlink(missing_ok=True)
return False, f"Network error: {e}"
def run(self) -> None:
self.save_dir.mkdir(parents=True, exist_ok=True)
completed = 0
# Validate seed 1 synchronously first to avoid spawning 1000 threads for invalid slugs
success, error_msg = self._download_seed(1)
if not success and error_msg in ("HTTP 403", "HTTP 404"):
self.error.emit(f"Failed to fetch seed 1. Does '{self.slug}' have patterns?")
self.finished.emit(False)
return
completed += 1
self.progress.emit(completed, self.total_seeds)
# Download the rest concurrently
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
future_to_seed = {executor.submit(self._download_seed, seed): seed for seed in range(2, self.total_seeds + 1)}
for future in concurrent.futures.as_completed(future_to_seed):
if self.isInterruptionRequested():
self.status.emit("Download cancelled. Waiting for threads to finish...")
executor.shutdown(wait=False, cancel_futures=True)
self.finished.emit(False)
return
seed = future_to_seed[future]
completed += 1
success, error = future.result()
if success:
self.status.emit(f"Downloaded seed {seed}/{self.total_seeds}")
else:
self.status.emit(f"Skipped seed {seed} ({error})")
self.progress.emit(completed, self.total_seeds)
self.status.emit("Download complete!")
self.finished.emit(True)
class PatternPullerDialog(QtWidgets.QDialog, I18nMixin):
"""Dialog for extracting patterns from CSGOSkins.gg URLs."""
def __init__(self, language: str, parent: QtWidgets.QWidget | None = None) -> None:
super().__init__(parent)
self.init_i18n(language)
self.setWindowTitle(self._t("dialog.puller_title", default="Pull Pattern Images"))
self.setMinimumWidth(450)
self._worker: PatternDownloadWorker | None = None
self._build_ui()
def _build_ui(self) -> None:
layout = QtWidgets.QVBoxLayout(self)
layout.setSpacing(12)
instruction_label = QtWidgets.QLabel(self._t("dialog.puller_instruction", default="Paste a CSGOSkins.gg item URL:"))
layout.addWidget(instruction_label)
self.url_input = QtWidgets.QLineEdit()
self.url_input.setPlaceholderText("https://csgoskins.gg/items/glock-18-trace-lock")
layout.addWidget(self.url_input)
self.status_label = QtWidgets.QLabel("")
self.status_label.setStyleSheet("color: palette(window-text); font-style: italic;")
layout.addWidget(self.status_label)
self.progress_bar = QtWidgets.QProgressBar()
self.progress_bar.setRange(0, 1000)
self.progress_bar.setValue(0)
layout.addWidget(self.progress_bar)
button_layout = QtWidgets.QHBoxLayout()
self.start_btn = QtWidgets.QPushButton(self._t("dialog.puller_start", default="Start Download"))
self.cancel_btn = QtWidgets.QPushButton(self._t("dialog.puller_cancel", default="Cancel"))
self.start_btn.clicked.connect(self._on_start_clicked)
self.cancel_btn.clicked.connect(self._on_cancel_clicked)
button_layout.addWidget(self.start_btn)
button_layout.addWidget(self.cancel_btn)
layout.addLayout(button_layout)
def _extract_slug(self, url: str) -> str | None:
# Match https://csgoskins.gg/items/SLUG
match = re.search(r"csgoskins\.gg/items/([^/?#]+)", url)
if match:
return match.group(1).lower()
return None
def _on_start_clicked(self) -> None:
url = self.url_input.text().strip()
slug = self._extract_slug(url)
if not slug:
QtWidgets.QMessageBox.warning(self, "Error", self._t("dialog.puller_invalid_url", default="Invalid URL format."))
return
save_dir = Path("analyses") / slug / "images"
self.start_btn.setEnabled(False)
self.url_input.setEnabled(False)
self.progress_bar.setValue(0)
self.status_label.setText("Starting download...")
self._worker = PatternDownloadWorker(slug=slug, save_dir=save_dir, parent=self)
self._worker.progress.connect(self._on_progress)
self._worker.status.connect(self.status_label.setText)
self._worker.error.connect(self._on_error)
self._worker.finished.connect(self._on_finished)
self._worker.start()
def _on_cancel_clicked(self) -> None:
if self._worker and self._worker.isRunning():
self._worker.requestInterruption()
self.cancel_btn.setEnabled(False)
self.status_label.setText("Cancelling...")
else:
self.reject() # Close dialog if not downloading
def _on_progress(self, current: int, total: int) -> None:
self.progress_bar.setValue(current)
def _on_error(self, message: str) -> None:
QtWidgets.QMessageBox.warning(self, "Error", message)
def _on_finished(self, success: bool) -> None:
self.start_btn.setEnabled(True)
self.url_input.setEnabled(True)
self.cancel_btn.setEnabled(True)
self._worker = None
if success:
QtWidgets.QMessageBox.information(self, "Done", self._t("dialog.puller_success", default="All patterns downloaded successfully!"))
+32 -10
View File
@@ -3,16 +3,38 @@
language = "en" language = "en"
[options] [options]
# Set to true to clear exclusion shapes whenever the image changes. # Set to true to clear exclusion shapes whenever the image changes in the preview viewer.
reset_exclusions_on_image_change = false reset_exclusions_on_image_change = false
# Hex color code for the match overlay (e.g. "#ff0000" for Red, "#00ff00" for Green)
# This is the color that paints over pixels that successfully match your HSV ranges.
overlay_color = "#ff0000"
# Hex color code for the background to be excluded (default #1f2937)
# This is useful for automatically removing flat background colors from UI screenshots
# or web scrapings before the analysis runs.
exclude_bg_color = "#1f2937"
# Tolerance for background color matching (0-255, default 5)
# A higher value will exclude pixels that are "close" to the hex color above, allowing
# you to bypass slight compression artifacts or noise in the image background.
exclude_bg_tolerance = 5
[defaults] [defaults]
# Override any of the following keys to tweak the initial slider values: # Override any of the following to tweak the initial slider values upon application start.
# hue_min, hue_max, sat_min, val_min, val_max accept floating point numbers. hue_min = 250.0 # (0-360) Starting Hue for the target color range
# alpha accepts an integer between 0 and 255. hue_max = 310.0 # (0-360) Ending Hue for the target color range
hue_min = 250.0 sat_min = 15.0 # (0-100) Minimum Saturation percentage
hue_max = 310.0 sat_max = 100.0 # (0-100) Maximum Saturation percentage
sat_min = 15.0 val_min = 15.0 # (0-100) Minimum Value/Brightness percentage
val_min = 15.0 val_max = 100.0 # (0-100) Maximum Value/Brightness percentage
val_max = 100.0 alpha = 150 # (0-255) Opacity of the red overlay in the UI preview
alpha = 120
[weights]
# Contribution of each measurement to the final Composite Score (0-100%).
match_all = 20 # % of the total visible image that matches
match_keep = 30 # % of the non-excluded area that matches (the most important area)
brightness = 10 # % Importance of Vibrance (or Darkness if "Prefer Darkness" is on)
grouping = 10 # % Importance of pixel clustering (rewarding solid color blocks)
continuity = 15 # % Quality of the largest connected surface area
border = 15 # % Quality of the transition edges (penalizing dark/hard outlines)
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 384 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 464 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 369 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 376 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 380 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 387 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 456 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 457 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 457 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 465 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 384 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 378 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 384 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 393 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 375 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 441 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 389 KiB

+9 -2
View File
@@ -1,13 +1,15 @@
[project] [project]
name = "icra" name = "icra"
version = "0.1.0" version = "0.1.0"
description = "Interactive Color Range Analyzer (ICRA) for Tkinter" description = "Interactive Color Range Analyzer (ICRA) desktop app (PySide6)"
readme = "README.md" readme = "README.md"
authors = [{ name = "ICRA contributors" }] authors = [{ name = "ICRA contributors" }]
license = "MIT" license = "GPL-3.0-only"
requires-python = ">=3.10" requires-python = ">=3.10"
dependencies = [ dependencies = [
"numpy>=1.26",
"pillow>=10.0.0", "pillow>=10.0.0",
"PySide6>=6.7",
] ]
[project.scripts] [project.scripts]
@@ -21,3 +23,8 @@ include = ["app"]
[tool.setuptools.package-data] [tool.setuptools.package-data]
"app" = ["assets/logo.png", "lang/*.toml"] "app" = ["assets/logo.png", "lang/*.toml"]
[dependency-groups]
dev = [
"pytest>=9.0.2",
]
+72
View File
@@ -0,0 +1,72 @@
import pytest
from pathlib import Path
from unittest.mock import MagicMock, patch
from PySide6 import QtWidgets
from app.qt.main_window import MainWindow
from app.qt.pattern_puller import PatternDownloadWorker
@pytest.fixture
def qt_app():
from PySide6.QtWidgets import QApplication
import sys
app = QApplication.instance()
if app is None:
app = QApplication(sys.argv)
yield app
def test_export_settings_path_generation(qt_app, tmp_path):
mock_widget = QtWidgets.QWidget()
mock_widget.title_label = MagicMock()
mock_widget.apply_theme = MagicMock()
with patch('app.qt.main_window.TitleBar', return_value=mock_widget):
window = MainWindow(language="en", defaults={}, reset_exclusions=False)
with patch("PySide6.QtWidgets.QFileDialog.getSaveFileName", return_value=("", "")) as mock_get_save:
# Test case 1: New subfolder structure (analyses/m4a1-s/images/1.png)
root = tmp_path / "analyses" / "m4a1_s"
img_dir = root / "images"
img_dir.mkdir(parents=True)
img_path = img_dir / "1.png"
window._current_image_path = img_path
window.export_settings()
# Verify settings directory was created
assert (root / "settings").exists()
# Verify default path given to QFileDialog
args, kwargs = mock_get_save.call_args
# args[2] is the default path string
expected_path = str(root / "settings" / "icra_settings_m4a1_s.json")
assert args[2] == expected_path
def test_export_folder_path_generation(qt_app, tmp_path):
mock_widget = QtWidgets.QWidget()
mock_widget.title_label = MagicMock()
mock_widget.apply_theme = MagicMock()
with patch('app.qt.main_window.TitleBar', return_value=mock_widget):
window = MainWindow(language="en", defaults={}, reset_exclusions=False)
with patch("PySide6.QtWidgets.QFileDialog.getSaveFileName", return_value=("", "")) as mock_get_save:
# Mock processor paths
root = tmp_path / "analyses" / "m4a1_s"
img_dir = root / "images"
img_dir.mkdir(parents=True)
window.processor.preview_paths = [img_dir / "1.png"]
window.export_folder()
assert (root / "results").exists()
args, kwargs = mock_get_save.call_args
expected_path = str(root / "results" / "icra_results_m4a1_s.csv")
assert args[2] == expected_path
def test_pattern_download_worker_dir(tmp_path):
worker = PatternDownloadWorker(slug="test-slug", save_dir=tmp_path / "analyses" / "test-slug" / "images")
assert worker.save_dir == tmp_path / "analyses" / "test-slug" / "images"
+73
View File
@@ -0,0 +1,73 @@
import pytest
from pathlib import Path
import re
# Base directory for language files
LANG_DIR = Path(__file__).resolve().parent.parent / "app" / "lang"
def get_structure(file_path: Path):
"""
Returns a list of (line_number, key/header) for a TOML file.
Only captures keys and section headers, ignoring the values.
"""
structure = []
# Regex to capture "key" = or [header]
key_pattern = re.compile(r'^\s*"?([^"\s=]+)"?\s*=')
header_pattern = re.compile(r'^\s*\[([^\]]+)\]')
with open(file_path, "r", encoding="utf-8") as f:
for i, line in enumerate(f, 1):
line = line.strip()
if not line:
structure.append((i, "<empty>"))
continue
# Check for header [section]
header_match = header_pattern.match(line)
if header_match:
structure.append((i, f"[{header_match.group(1)}]"))
continue
# Check for key "name" =
key_match = key_pattern.match(line)
if key_match:
structure.append((i, key_match.group(1)))
continue
# Comments or anything else
structure.append((i, "<other/comment>"))
return structure
def test_i18n_files_exist():
assert LANG_DIR.exists(), f"Language directory {LANG_DIR} not found"
en_file = LANG_DIR / "en.toml"
assert en_file.exists(), "English language file (en.toml) must exist as baseline"
def test_i18n_synchronization():
"""
Ensures all language files have the same keys/headers on the same lines
as the baseline en.toml.
"""
en_path = LANG_DIR / "en.toml"
en_structure = get_structure(en_path)
other_files = list(LANG_DIR.glob("*.toml"))
other_files.remove(en_path)
for lang_file in other_files:
lang_name = lang_file.name
lang_structure = get_structure(lang_file)
# Check line count
assert len(lang_structure) == len(en_structure), \
f"{lang_name} length mismatch: expected {len(en_structure)} lines, got {len(lang_structure)}"
# Check line-by-line sync
for (en_line, en_key), (lang_line, lang_key) in zip(en_structure, lang_structure):
assert en_key == lang_key, \
f"Sync error at {lang_name}:{lang_line}. Expected '{en_key}', found '{lang_key}'"
if __name__ == "__main__":
# Allow running directly as a script
pytest.main([__file__])
+174
View File
@@ -0,0 +1,174 @@
import numpy as np
import pytest
from PIL import Image
from app.qt.image_processor import Stats, _rgb_to_hsv_numpy, QtImageProcessor
def test_stats_summary():
s = Stats(
matches_all=50, total_all=100,
matches_keep=40, total_keep=80,
matches_excl=10, total_excl=20
)
def mock_t(key, **kwargs):
if key == "stats.placeholder":
return "Placeholder"
if not kwargs:
return key
return f"{kwargs['with_pct']:.1f} {kwargs['without_pct']:.1f} {kwargs['excluded_pct']:.1f}"
weights = {"match_all": 30, "match_keep": 50, "brightness": 10, "grouping": 10}
res = s.summary(mock_t, weights)
# with_pct: 40/80 = 50.0
# without_pct: 50/100 = 50.0
# excluded_pct: 20/100 = 20.0
assert res == "50.0 50.0 20.0"
def test_stats_empty():
s = Stats()
weights = {"match_all": 30, "match_keep": 50, "brightness": 10, "grouping": 10}
assert s.summary(lambda k, **kw: "Empty", weights) == "Empty"
def test_rgb_to_hsv_numpy():
# Test red
arr = np.array([[[1.0, 0.0, 0.0]]], dtype=np.float32)
hsv = _rgb_to_hsv_numpy(arr)
assert np.allclose(hsv[0, 0], [0.0, 100.0, 100.0])
# Test green
arr = np.array([[[0.0, 1.0, 0.0]]], dtype=np.float32)
hsv = _rgb_to_hsv_numpy(arr)
assert np.allclose(hsv[0, 0], [120.0, 100.0, 100.0])
# Test blue
arr = np.array([[[0.0, 0.0, 1.0]]], dtype=np.float32)
hsv = _rgb_to_hsv_numpy(arr)
assert np.allclose(hsv[0, 0], [240.0, 100.0, 100.0])
# Test white
arr = np.array([[[1.0, 1.0, 1.0]]], dtype=np.float32)
hsv = _rgb_to_hsv_numpy(arr)
assert np.allclose(hsv[0, 0], [0.0, 0.0, 100.0])
# Test black
arr = np.array([[[0.0, 0.0, 0.0]]], dtype=np.float32)
hsv = _rgb_to_hsv_numpy(arr)
assert np.allclose(hsv[0, 0], [0.0, 0.0, 0.0])
def test_qt_processor_matches_legacy():
proc = QtImageProcessor()
proc.hue_min = 350
proc.hue_max = 10
proc.sat_min = 50
proc.val_min = 50
proc.val_max = 100
# Red wraps around 360, so H=0 -> ok
assert proc._matches(255, 0, 0) is True
# Green H=120 -> fail
assert proc._matches(0, 255, 0) is False
# Dark red S=100, V=25 -> fail because val_min=50
assert proc._matches(64, 0, 0) is False
def test_set_overlay_color():
proc = QtImageProcessor()
# default red
assert proc.overlay_r == 255
assert proc.overlay_g == 0
assert proc.overlay_b == 0
proc.set_overlay_color("#00ff00")
assert proc.overlay_r == 0
assert proc.overlay_g == 255
assert proc.overlay_b == 0
# invalid hex does nothing
proc.set_overlay_color("blue")
assert proc.overlay_r == 0
def test_coordinate_scaling():
proc = QtImageProcessor()
# Create a 200x200 image where everything is red
red_img_small = Image.new("RGBA", (200, 200), (255, 0, 0, 255))
proc.orig_img = red_img_small # satisfy preview logic
proc.preview_img = red_img_small
# All red. Thresholds cover all red.
proc.hue_min = 0
proc.hue_max = 360
proc.sat_min = 10
proc.val_min = 10
# Exclude the right half (100-200)
proc.set_exclusions([{"kind": "rect", "coords": (100, 0, 200, 200)}])
# Verify small stats
s_small = proc.get_stats_headless(red_img_small)
# total=40000, keep=20000, excl=20000
assert s_small.total_all == 40000
assert s_small.total_keep == 20000
assert s_small.total_excl == 20000
# Now check on a 1000x1000 image (5x scale)
red_img_large = Image.new("RGBA", (1000, 1000), (255, 0, 0, 255))
s_large = proc.get_stats_headless(red_img_large)
# total=1,000,000. If scaling works, keep=500,000, excl=500,000.
# If scaling FAILED, the mask is still 100x200 (20,000 px) -> excl=20,000.
assert s_large.total_all == 1000000
assert s_large.total_keep == 500000
assert s_large.total_excl == 500000
def test_calculate_grouping_score():
proc = QtImageProcessor()
# 1. Empty mask
mask_empty = np.zeros((20, 20), dtype=bool)
assert proc._calculate_grouping_score(mask_empty) == 0.0
# 2. Single mask pixel (0 neighbors)
mask_single = np.zeros((20, 20), dtype=bool)
mask_single[10, 10] = True
assert proc._calculate_grouping_score(mask_single) == 0.0
# 3. 2x2 block
# each pixel in 2x2 has 3 neighbors in 3x3, 3 neighbors in 5x5, 3 neighbors in 9x9.
# score = ((3/80)^2) * 100
expected_2x2 = ((3/80.0)**2) * 100.0
mask_block = np.zeros((20, 20), dtype=bool)
mask_block[10:12, 10:12] = True
assert pytest.approx(proc._calculate_grouping_score(mask_block)) == expected_2x2
# 4. 9x9 block
# center pixel has 80 neighbors (100% density).
# many pixels have high density.
mask_9x9 = np.zeros((20, 20), dtype=bool)
mask_9x9[5:14, 5:14] = True
res_9x9 = proc._calculate_grouping_score(mask_9x9)
assert res_9x9 > expected_2x2
# For a 9x9 block, the center pixel is 100%. Boundary pixels are less.
# 1 center pixel = 80/80 = 1.0.
# Overall it should be a healthy percentage.
assert res_9x9 > 10.0 # significant grouping
def test_export_worker_error():
from app.qt.image_processor import _export_worker
# 1. Provide a missing file to trigger an exception during Image.open()
res1 = _export_worker(("missing_file.png", {
"hue_min": 0, "hue_max": 360, "sat_min": 0, "sat_max": 100,
"val_min": 0, "val_max": 100, "exclude_bg": False,
"exclude_bg_rgb": (0, 0, 0), "exclude_bg_tolerance": 5,
"prefer_dark": False, "exclude_shapes": [], "exclude_ref_size": None,
"weights": {}
}))
assert res1 == ("missing_file.png", None, None, None, None, None)
# 2. Provide an empty params dict to trigger KeyError before opening image
res2 = _export_worker(("dummy.png", {}))
assert res2 == ("dummy.png", None, None, None, None, None)