-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocky_image.py
61 lines (50 loc) · 1.96 KB
/
blocky_image.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 19 16:03:24 2024
@author: alfie
"""
import numpy as np
from PIL import Image
import math
import os
def image_to_pixel_blocks(path, block_size, subblock_size, output):
# convert picture
image = Image.open(path).convert("L")
width, height = image.size
# convert into numpy array
pixel_data = np.array(image)
# create the block image
new_height = height - height % block_size
new_width = width - width % block_size
#print(new_height,new_width) #debug
block_image = np.ones((new_height, new_width), dtype=np.uint8) * 255 # initialise as white
# pixel_data[i,j] is the grey value of the pixel
pixel_per_block = block_size ** 2
block_subblock_number = pixel_per_block / (subblock_size **2)
# going through every block
for i in range(0, new_height, block_size):
for j in range(0, new_width, block_size):
#now we are at the very corner of the block
total_grey_value = 0
for k in range(block_size):
for l in range(block_size):
total_grey_value += pixel_data[i+k, j+l]
subblock = round((1 - (total_grey_value / pixel_per_block /225)) * block_subblock_number) #black subblock number of the block
#print(subblock) #debug
if subblock >=0:
depth = round(math.sqrt(subblock) * subblock_size)
else:
depth = 0
for k in range(i, i + depth, 1):
for l in range(j, j + depth, 1):
block_image[k, l] = 0
os.chdir(output)
block_image = Image.fromarray(block_image)
block_image.save(fp = "blocky_image.png")
# sample use
a = input("Input path of the image: ")
b = int(input("Input block size: "))
c = int(input("input subblock size (block size must be divisible by it): "))
d = input("Output directory: ")
image_to_pixel_blocks(a,b,c,d)