Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
42
Task/Bitmap-Histogram/Python/bitmap-histogram.py
Normal file
42
Task/Bitmap-Histogram/Python/bitmap-histogram.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
from PIL import Image
|
||||
|
||||
# Open the image
|
||||
image = Image.open("lena.jpg")
|
||||
# Get the width and height of the image
|
||||
width, height = image.size
|
||||
# Calculate the amount of pixels
|
||||
amount = width * height
|
||||
|
||||
# Total amount of greyscale
|
||||
total = 0
|
||||
# B/W image
|
||||
bw_image = Image.new('L', (width, height), 0)
|
||||
# Bitmap image
|
||||
bm_image = Image.new('1', (width, height), 0)
|
||||
|
||||
for h in range(0, height):
|
||||
for w in range(0, width):
|
||||
r, g, b = image.getpixel((w, h))
|
||||
|
||||
greyscale = int((r + g + b) / 3)
|
||||
total += greyscale
|
||||
|
||||
bw_image.putpixel((w, h), gray_scale)
|
||||
|
||||
# The average greyscale
|
||||
avg = total / amount
|
||||
|
||||
black = 0
|
||||
white = 1
|
||||
|
||||
for h in range(0, height):
|
||||
for w in range(0, width):
|
||||
v = bw_image.getpixel((w, h))
|
||||
|
||||
if v >= avg:
|
||||
bm_image.putpixel((w, h), white)
|
||||
else:
|
||||
bm_image.putpixel((w, h), black)
|
||||
|
||||
bw_image.show()
|
||||
bm_image.show()
|
||||
Loading…
Add table
Add a link
Reference in a new issue