def nearest(pixel_color, mem={}):
if pixel_color in mem:
return mem[pixel_color]
n = min(floss_palette, key=lambda fc:delta_e_cie2000(pixel_color, fc))
mem[pixel_color] = n
return mem[pixel_color]
result = [nearest(pixel_color) for pixel_color in image]
import pandas as pd
import numpy as np
from PIL import Image
from PIL import ImageEnhance
import cv2
im = Image.open(path)
# Get np.array from image
im_array = np.asarray(im)
# Blur Image
blur = Image.fromarray(cv2.medianBlur(im_array,5))
# Enhance color for easier split
im_ready = ImageEnhance.Color(blur).enhance(100)
# Get unique colors from Image
unique_colors = set()
def get_unique_colors(img):
for i in range(0,img.size[0]):
for j in range(0,img.size[1]):
r,g,b = img.getpixel((i,j))
unique_colors.add((r,g,b))
return(unique_colors)
unique_colors = get_unique_colors(im_ready)
# Create a dictionary computing each unique color frequency
from collections import defaultdict
by_color = defaultdict(int)
for pixel in im_ready.getdata():
by_color[pixel] += 1
# Detect two most frequent colors that are not black
# (black will always be sorted(by_color.values(),reverse=True)[0] with over 10k black pixels)
colors_list = []
for e,j in by_color.iteritems():
if j == sorted(by_color.values(),reverse=True)[1]:
colors_list.append(e)
if j == sorted(by_color.values(),reverse=True)[2]:
colors_list.append(e)
def get_letter_order(im):
impx = im.load()
for x in range(0,im.size[0]):
for y in range(0,im.size[1]):
if impx[x,y] == colors_list[0]:
return(colors_list[0],colors_list[1])
if impx[x,y] == colors_list[1]:
return(colors_list[1],colors_list[0])
first_letter_color, second_letter_color = get_letter_order(im_ready)
# Get each letter separately by replacing the other color with black pixels
l1 = im_ready.copy()
l2 = im_ready.copy()
im1 = l1.load()
im2 = l2.load()
for x in range(0,im_ready.size[0]):
for y in range(0,im_ready.size[1]):
if im_ready.getpixel((x,y)) <> (0,0,0):
if im1[x,y] <> first_letter_color:
im1[x,y] = (0,0,0)
if im2[x,y] <> second_letter_color:
im2[x,y] = (0,0,0)
#!/usr/bin/env python3
from PIL import Image
def quantizetopalette(silf, palette, dither=False):
"""Convert an RGB or L mode image to use a given P image's palette."""
silf.load()
# use palette from reference image
palette.load()
if palette.mode != "P":
raise ValueError("bad mode for palette image")
if silf.mode != "RGB" and silf.mode != "L":
raise ValueError(
"only RGB or L mode images can be quantized to a palette"
)
im = silf.im.convert("P", 1 if dither else 0, palette.im)
# the 0 above means turn OFF dithering
# Later versions of Pillow (4.x) rename _makeself to _new
try:
return silf._new(im)
except AttributeError:
return silf._makeself(im)
palettedata = [0, 0, 0, 102, 102, 102, 176, 176, 176, 255, 255, 255]
palimage = Image.new('P', (16, 16))
palimage.putpalette(palettedata * 64)
oldimage = Image.open("School_scrollable1.png")
newimage = quantizetopalette(oldimage, palimage, dither=False)
newimage.show()
from PIL import Image
image_file = Image.open("cat-tied-icon.png") # open colour image
image_file = image_file.convert('1') # convert image to black and white
image_file.save('/tmp/result.png')
from PIL import Image
col = Image.open('myimage.jpg')
gry = col.convert('L')
grarray = np.asarray(gry)
bw = (grarray > grarray.mean())*255
imshow(bw)
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
img = Image.new('RGB', (264, 176), color = (255, 255, 255))
font_path = '/usr/share/fonts/truetype/ubuntu/UbuntuMono-R.ttf'
font = ImageFont.truetype(font_path, 20)
draw = ImageDraw.Draw(img)
draw.text((15, 15), 'Is it your text?', font=font, fill=(0, 0, 0))
img.save('img_with_text.bmp', 'bmp')