[IMP] web_company_color: black, isort, prettier

This commit is contained in:
Alexandre Díaz
2020-03-19 12:04:11 +01:00
committed by Bernat Puig Font
parent 38f0903100
commit 56893f28d0
7 changed files with 153 additions and 135 deletions

View File

@@ -2,12 +2,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import base64
import math
from PIL import Image
from io import BytesIO
from PIL import Image
def n_rgb_to_hex(_r, _g, _b):
return '#%02x%02x%02x' % (int(255*_r), int(255*_g), int(255*_b))
return "#{:02x}{:02x}{:02x}".format(int(255 * _r), int(255 * _g), int(255 * _b))
def convert_to_image(field_binary):
@@ -16,22 +17,20 @@ def convert_to_image(field_binary):
def image_to_rgb(img):
def normalize_vec3(vec3):
_l = 1.0 / math.sqrt(vec3[0]*vec3[0]
+ vec3[1]*vec3[1]
+ vec3[2]*vec3[2])
return (vec3[0]*_l, vec3[1]*_l, vec3[2]*_l)
_l = 1.0 / math.sqrt(vec3[0] * vec3[0] + vec3[1] * vec3[1] + vec3[2] * vec3[2])
return (vec3[0] * _l, vec3[1] * _l, vec3[2] * _l)
# Force Alpha Channel
if img.mode != 'RGBA':
img = img.convert('RGBA')
if img.mode != "RGBA":
img = img.convert("RGBA")
width, height = img.size
# Reduce pixels
width, height = (max(1, int(width/4)), max(1, int(height/4)))
width, height = (max(1, int(width / 4)), max(1, int(height / 4)))
img = img.resize((width, height))
rgb_sum = [0, 0, 0]
# Mix. image colors using addition method
RGBA_WHITE = (255, 255, 255, 255)
for i in range(0, height*width):
for i in range(0, height * width):
rgba = img.getpixel((i % width, i / width))
if rgba[3] > 128 and rgba != RGBA_WHITE:
rgb_sum[0] += rgba[0]