Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

imambungo/histogram

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

M Imam Pratama
09021281722063
IF Reg A 17

Histogram

Membuat histogram citra menggunakan Python 3. Semua source code dan gambar yang digunakan dalam tugas ini bisa didapat di link berikut: https://github.com/imambungo/histogram.

Requirement

  • matplotlib
  • PIL (Python Image Library)

Kedua module di atas bisa diinstall menggunakan command berikut:

pip3 install matplotlib pillow

Program

histogram.py :

from matplotlib import pyplot
from os.path import basename
from os.path import splitext
from PIL import Image

def get_file_name(file_path):
    # https://stackoverflow.com/a/678266/9157799
    return splitext(basename(file_path))[0]

def tampilkan_histogram(r, g, b, gambar):
    intensitas = list(range(256))
    lebar_bar = 0.3

    # https://stackoverflow.com/q/9304408/9157799
    intensitas = [i-lebar_bar for i in intensitas]
    # https://stackoverflow.com/q/14270391/9157799
    pyplot.bar(intensitas, r, width=lebar_bar, color='r')

    intensitas = [i+lebar_bar for i in intensitas]
    pyplot.bar(intensitas, g, width=lebar_bar, color='g')

    intensitas = [i+lebar_bar for i in intensitas]
    pyplot.bar(intensitas, b, width=lebar_bar, color='b')

    pyplot.title('Histogram ' + gambar)
    pyplot.xlabel('Intensitas')
    pyplot.ylabel('Kemunculan')
    pyplot.legend(['R', 'G', 'B'])
    pyplot.show()

def histogram(gambar):
    GAMBAR = Image.open(gambar)
    PIXEL = GAMBAR.load()

    ukuran_horizontal = GAMBAR.size[0]
    ukuran_vertikal = GAMBAR.size[1]

    gambar_r = Image.new('RGB', (ukuran_horizontal, ukuran_vertikal))
    pixel_r = gambar_r.load()

    gambar_g = Image.new('RGB', (ukuran_horizontal, ukuran_vertikal))
    pixel_g = gambar_g.load()

    gambar_b = Image.new('RGB', (ukuran_horizontal, ukuran_vertikal))
    pixel_b = gambar_b.load()

    # https://stackoverflow.com/q/10712002/9157799
    r = [0] * 256
    g = [0] * 256
    b = [0] * 256

    for x in range(ukuran_horizontal):
        for y in range(ukuran_vertikal):
            intensitas_r = PIXEL[x, y][0]
            intensitas_g = PIXEL[x, y][1]
            intensitas_b = PIXEL[x, y][2]
            r[intensitas_r] += 1
            g[intensitas_g] += 1
            b[intensitas_b] += 1
            pixel_r[x, y] = (intensitas_r, 0, 0)
            pixel_g[x, y] = (0, intensitas_g, 0)
            pixel_b[x, y] = (0, 0, intensitas_b)

    gambar_r.save('img/' + get_file_name(gambar) + '_r.jpg')
    gambar_g.save('img/' + get_file_name(gambar) + '_g.jpg')
    gambar_b.save('img/' + get_file_name(gambar) + '_b.jpg')

    tampilkan_histogram(r, g, b, gambar)


histogram('img/gambar.jpg')
histogram('img/gambar2.jpg')

Histogram

Sample 1

gambar.jpg :

Histogram

Komposisi RGB

R G B

Sample 2

gambar2.jpg :

Histogram

Komposisi RGB

R G B

About

Histogram citra menggunakan Python 3

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages