-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.py
61 lines (47 loc) · 1.88 KB
/
converter.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 Feb 16 23:39:28 2023
@author: shree
"""
import numpy as np
import cv2
import PIL
from matplotlib import pyplot as plt
import hasher
def window_down(image, window_size):
"""
Parameters
----------
image : np.array
the image to be tiled
window_size : int
size of tile
Returns
-------
numpy tensor representing the scaled image
"""
if window_size == 1:
return image
# for some unknown reason, here opencv wants dimensions in reverse. WATCH OUT!
return cv2.resize(image, dsize=(image.shape[1]//window_size,
image.shape[0]//window_size),
interpolation=cv2.INTER_CUBIC)
def convert(image):
# the array to contain `result_image` is constant globally so memory doesn't
# have to be reallocated each loop
image = window_down(image, window_size)
for i in range(height):
for j in range(width):
b, g, r = image[i, j]
# dB, dG, dR = image[i, j] - bgr_avg[hashbin[g, b, r]]
result_image[i*tile_size: (i+1)*tile_size, j*tile_size: (j+1)*tile_size] = im_list[hashbin[g, b, r]]
# result_image[i*tile_size: (i+1)*tile_size, j*tile_size: (j+1)*tile_size] = shift_colour(im_list[hashbin[g, b, r]], dB, dG, dR)
# okay idk why but due to some very weird bug i have to look at index gbr, not bgr
return image
def init(h, w, i_list, avg, h_bin, w_size):
global height, width, im_list, bgr_avg, hashbin, result_image, window_size, tile_size
height, width, im_list, bgr_avg, hashbin, window_size = h, w, i_list, avg, h_bin, w_size
tile_size = im_list.shape[1]
# we create an empty rgb image to be filled using the hashbin we've created
result_image = np.zeros((height*tile_size, width*tile_size, 3), np.uint8)