Skip to content

Instantly share code, notes, and snippets.

@lefuturiste
Created May 1, 2021 08:53
Show Gist options
  • Save lefuturiste/8c6cf828603cd56f58e31d3dff40bad8 to your computer and use it in GitHub Desktop.
Save lefuturiste/8c6cf828603cd56f58e31d3dff40bad8 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
import sys
## EXERCICE 1
def gris(f_in, f_out):
f = plt.imread(f_in)
l,c,d = np.shape(f)
g = np.zeros((l,c,d))
for i in range(l):
for j in range(c):
g[i,j] = 3*[0.30*f[i,j,0] + 0.59*f[i,j,1] + 0.11*f[i,j,2]]
plt.imshow(g)
plt.show()
plt.imsave(f_out,g)
#gris('./Etretat.png', './Etretat_g.png')
#gris('Etretat.png','Etretat_g.png')
#f=plt.imread('./Etretat.png')
'''
pour n = 256 on a 2^8 valeurs possibles
pour n = 1 soit noir ou blanc
propor
256 n
val ?
? = (val*n)/256
'''
## EXERCICE 2
def posteriseVal(toN, val):
assert val >= 0 and val <= 1
return int(toN * val)/toN
def posterise(f_in, f_out, n):
f = plt.imread(f_in)
l,c,d = np.shape(f)
g = np.zeros((l,c,d))
for i in range(l):
for j in range(c):
g[i,j] = [posteriseVal(n, val) for val in f[i,j]]
return g
'''
G = posterise('Etretat.png', 'Etretat_g.png', 4)
plt.imshow(G)
plt.show()
'''
## EXERCICE 3
# Q1
'''
1392 px de largeur par 1040 px de hauteur
on a une matrice avec 1040 lignes et 1392 colonnes
résolution du can de 12 bits donc 4096 valeurs possibles pour chaque pixels
taille en bit = 12 * 1040 * 1392
taille en octet = (12 * 1040 * 1392)/8 = 2 171 520 octets soit ~2.2Mo
min=0 max=(2**12)-1=4095
'''
# Q2
'''
760*753
'''
def cree():
return np.array(753*[760*[0]])
# or np.zeros((760,753))
# lignes: min = 0; max = 753
# colonnes: min = 0; max = 760
# Q3
def transfo1(imSrc, b):
VMax = 2**b-1
G = np.zeros(imSrc.shape):
for i in range(imgSrc.shape[0]):
for j in range(imSrc.shape[1]):
G[i,j] = VMax - imSrc[i,j]
return G
# Q4
def histo(imSrc, b):
VMax = 2**b-1
F = transfo1(imSrc, b)
O = np.zeros(VMax + 1)
Hist = O.copy()
for i in range(F.shape[0]):
for j in range(F.shape[1]):
Hist[F[i,j]] += 1
plt.bar(O, H)
return Hist
# Q5
def transfo2(imgSrc, b)
VMax = 2**b-1
T = transfo1(imSrc, b)
H = histo(imSrc, b)
TFlat = np.flatten(T)
valMin = np.min(TFlat)
valMax = np.max(TFlat)
Ipp = np.shape(imSrc)
for i in range(imSrc.shape[0]):
for j in range(imSrc.shape[1]):
G[i,j] = int(Vmax * (T[i,j]-ValMin)/(valMax-valMin))
return G
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment