Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
pythonlearning/Scripts/find_similar_images.py at main · Lovel0ri/pythonlearning · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Lovel0ri
pythonlearning
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
pythonlearning
/
Scripts
/
find_similar_images.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
82 lines (68 loc) · 2.23 KB
main
Breadcrumbs
pythonlearning
/
Scripts
/
find_similar_images.py
Copy path
Top
File metadata and controls
Code
Blame
82 lines (68 loc) · 2.23 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!E:\网络与新媒体\github\pythonlearning\Scripts\python.exe
from
__future__
import
absolute_import
,
division
,
print_function
from
PIL
import
Image
import
imagehash
"""
Demo of hashing
"""
def
find_similar_images
(
userpaths
,
hashfunc
=
imagehash
.
average_hash
):
def
is_image
(
filename
):
f
=
filename
.
lower
()
return
f
.
endswith
(
'.png'
)
or
f
.
endswith
(
'.jpg'
)
or
\
f
.
endswith
(
'.jpeg'
)
or
f
.
endswith
(
'.bmp'
)
or
\
f
.
endswith
(
'.gif'
)
or
'.jpg'
in
f
or
f
.
endswith
(
'.svg'
)
image_filenames
=
[]
for
userpath
in
userpaths
:
image_filenames
+=
[
os
.
path
.
join
(
userpath
,
path
)
for
path
in
os
.
listdir
(
userpath
)
if
is_image
(
path
)]
images
=
{}
for
img
in
sorted
(
image_filenames
):
try
:
hash
=
hashfunc
(
Image
.
open
(
img
))
except
Exception
as
e
:
print
(
'Problem:'
,
e
,
'with'
,
img
)
continue
if
hash
in
images
:
print
(
img
,
' already exists as'
,
' '
.
join
(
images
[
hash
]))
if
'dupPictures'
in
img
:
print
(
'rm -v'
,
img
)
images
[
hash
]
=
images
.
get
(
hash
, [])
+
[
img
]
# for k, img_list in six.iteritems(images):
# if len(img_list) > 1:
# print(" ".join(img_list))
if
__name__
==
'__main__'
:
# noqa: C901
import
os
import
sys
def
usage
():
sys
.
stderr
.
write
(
"""SYNOPSIS: %s [ahash|phash|dhash|...] [<directory>]
Identifies similar images in the directory.
Method:
ahash: Average hash
phash: Perceptual hash
dhash: Difference hash
whash-haar: Haar wavelet hash
whash-db4: Daubechies wavelet hash
colorhash: HSV color hash
crop-resistant: Crop-resistant hash
(C) Johannes Buchner, 2013-2017
"""
%
sys
.
argv
[
0
])
sys
.
exit
(
1
)
hashmethod
=
sys
.
argv
[
1
]
if
len
(
sys
.
argv
)
>
1
else
usage
()
if
hashmethod
==
'ahash'
:
hashfunc
=
imagehash
.
average_hash
elif
hashmethod
==
'phash'
:
hashfunc
=
imagehash
.
phash
elif
hashmethod
==
'dhash'
:
hashfunc
=
imagehash
.
dhash
elif
hashmethod
==
'whash-haar'
:
hashfunc
=
imagehash
.
whash
elif
hashmethod
==
'whash-db4'
:
def
hashfunc
(
img
):
return
imagehash
.
whash
(
img
,
mode
=
'db4'
)
elif
hashmethod
==
'colorhash'
:
hashfunc
=
imagehash
.
colorhash
elif
hashmethod
==
'crop-resistant'
:
hashfunc
=
imagehash
.
crop_resistant_hash
else
:
usage
()
userpaths
=
sys
.
argv
[
2
:]
if
len
(
sys
.
argv
)
>
2
else
'.'
find_similar_images
(
userpaths
=
userpaths
,
hashfunc
=
hashfunc
)
You can’t perform that action at this time.