Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AP-belkacemi-melissa
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Melissa Belkacemi
AP-belkacemi-melissa
Commits
1f971f3f
Commit
1f971f3f
authored
1 year ago
by
Melissa Belkacemi
Browse files
Options
Downloads
Patches
Plain Diff
Update file decoupe.py
parent
5dcfebb3
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
PJ/decoupe.py
+64
-14
64 additions, 14 deletions
PJ/decoupe.py
with
64 additions
and
14 deletions
PJ/decoupe.py
+
64
−
14
View file @
1f971f3f
from
PIL
import
Image
,
ImageDraw
# Belkacemi Melissa
# 01/03/2024
from
bloc
import
*
from
couleur
import
*
from
PIL
import
Image
,
ImageDraw
def
decoupe
(
image
:
Image
,
p_hg
,
p_bd
,
n
:
int
)
->
Bloc
:
...
...
@@ -12,16 +16,16 @@ def decoupe(image:Image,p_hg,p_bd,n:int)->Bloc:
if
n
>
0
:
b1
,
b2
,
b3
,
b4
=
quatre_blocs
(
image
,
hg
,
bd
)
decoupe
(
b1
,
n
-
1
,
b1
.
pixel_hg
,
b1
.
pixel_bd
)
decoupe
(
b2
,
n
-
1
,
b2
.
pixel_hg
,
b2
.
pixel_bd
)
decoupe
(
b3
,
n
-
1
,
b3
.
pixel_hg
,
b3
.
pixel_bd
)
decoupe
(
b4
,
n
-
1
,
b4
.
pixel_hg
,
b4
.
pixel_bd
)
decoupe
(
image
,
b1
.
pixel_hg
,
b1
.
pixel_bd
,
n
-
1
)
decoupe
(
image
,
b2
.
pixel_hg
,
b2
.
pixel_bd
,
n
-
1
)
decoupe
(
image
,
b3
.
pixel_hg
,
b3
.
pixel_bd
,
n
-
1
)
decoupe
(
image
,
b4
.
pixel_hg
,
b4
.
pixel_bd
,
n
-
1
)
if
b1
.
sont_proches
(
b2
,
b3
,
b4
):
return
(
Bloc
(
couleur_moyenne
([
b1
.
couleur
,
b2
.
couleur
,
b3
.
couleur
,
b4
.
couleur
]),
b1
.
pixel_hg
,
b4
.
pixel_bd
))
else
:
return
(
Bloc
((
0
,
0
,
0
),
b1
.
pixel_hg
,
b4
.
pixel_bd
,
b1
,
b2
,
b3
,
b4
))
else
:
couleurs
=
[
im
_rgb
.
getpixel
((
i
,
j
))
for
i
in
range
(
p_hg
[
0
],
1
+
p_bd
[
0
])
for
j
in
range
(
p_hg
[
1
],
1
+
p_bd
[
1
])]
couleurs
=
[
im
age
.
getpixel
((
i
,
j
))
for
i
in
range
(
p_hg
[
0
],
1
+
p_bd
[
0
])
for
j
in
range
(
p_hg
[
1
],
1
+
p_bd
[
1
])]
coul_moyenne
=
couleur_moyenne
(
couleurs
)
return
Bloc
(
coul_moyenne
,
p_hg
,
p_bd
)
...
...
@@ -47,13 +51,59 @@ def quatre_blocs(im:Image,hg:tuple[int,int],bd:tuple[int,int])->tuple[Bloc,Bloc,
couleurs3
=
[
im
.
getpixel
((
i
,
j
))
for
i
in
range
(
pixel_hg3
[
0
],
1
+
pixel_bd3
[
0
])
for
j
in
range
(
pixel_hg3
[
1
],
1
+
pixel_bd3
[
1
])]
couleurs4
=
[
im
.
getpixel
((
i
,
j
))
for
i
in
range
(
pixel_hg4
[
0
],
1
+
pixel_bd4
[
0
])
for
j
in
range
(
pixel_hg4
[
1
],
1
+
pixel_bd4
[
1
])]
return
(
Bloc
(
couleur_moyenne
(
couleurs1
),
pixel_hg1
,
pixel_bd1
),
Bloc
(
couleur_moyenne
(
couleurs2
),
pixel_hg2
,
pixel_bd2
),
Bloc
(
couleur_moyenne
(
couleurs3
),
pixel_hg3
,
pixel_bd3
),
Bloc
(
couleur_moyenne
(
couleurs4
),
pixel_hg4
,
pixel_bd4
))
def
ouvre_image
(
image
:
str
):
"""
ouvre et convertit en formay RGB l
'
image prise en paramètres
Précondition : le chemin vers l
'
image est correct
"""
img
=
Image
.
open
(
image
)
img
.
convert
(
'
RGB
'
)
return
img
def
dessine_blocs
(
dessin
,
bloc
:
Bloc
):
"""
dessine le bloc dans l
'
image
Précondition :
"""
if
len
(
bloc
.
sous_blocs
)
==
0
:
dessin
.
rectangle
((
bloc
.
pixel_hg
,
bloc
.
pixel_bd
),
fill
=
bloc
.
couleur
)
else
:
for
elem
in
bloc
.
sous_blocs
:
dessine_blocs
(
elem
)
def
dessine
(
img
,
bloc
:
Bloc
):
"""
associe à un bloc une image bitmap le représentant
Précondition : l
'
image est ouverte
"""
dessin
=
ImageDraw
.
Draw
(
img
)
dessine_blocs
(
dessin
,
bloc
)
if
__name__
==
"
__main__
"
:
import
sys
if
len
(
sys
.
argv
)
!=
4
:
print
(
"
usage: decoupe.py operation image ordre
"
)
else
:
operation
,
image
,
ordre
=
sys
.
argv
[
1
:]
if
operation
in
{
"
affiche
"
,
"
enregistre
"
}:
ordre
=
int
(
ordre
)
im_rgb
=
ouvre_image
(
image
)
t
=
im_rgb
.
size
hg
=
(
0
,
0
)
bd
=
(
t
[
0
]
-
1
,
t
[
1
]
-
1
)
b
=
decoupe
(
im_rgb
,
hg
,
bd
,
ordre
)
dessin
=
dessine
(
im_rgb
,
b
)
if
operation
==
"
affiche
"
:
im_rgb
.
show
()
else
:
im_rgb
.
save
(
"
images/img_rec.png
"
,
"
PNG
"
)
im
=
Image
.
open
(
'
images/calbuth.png
'
)
t
=
im
.
size
hg
=
(
0
,
0
)
bd
=
(
t
[
0
]
-
1
,
t
[
1
]
-
1
)
im_rgb
=
im
.
convert
(
'
RGB
'
)
b
=
decoupe
(
im_rgb
,
hg
,
bd
,
1
)
b
.
sous_blocs
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment