def relight(img, hue_weight, lum_weight, sat_weight, step): ''' relights image in hsl space according to assigned weights and step-size img: PIL image hue_weight: 2D array storing hue weights lum_weight: 2D array storing illumination weights sat_weight: 2D array storing saturation weights step: [3x1] set of weights for [hue, lum, sat] relighting ''' img_pixels = np.array(img) hgt, wth, c = img_pixels.shape img_pix_new = np.zeros((hgt,wth, c), float) for y in range(hgt): for x in range(wth): hls = luminance.rgb_to_hls(img_pixels[y,x]) hls = np.asarray(hls) hls[0] -= step[0] * hue_weight[y,x] hls[1] -= step[1] * lum_weight[y,x] hls[2] -= step[2] * sat_weight[y,x] hls[hls > 1] = 1 hls[hls < 0] = 0 img_pix_new[y,x] = luminance.hls_to_rgb(hls) img_new = Image.fromarray(np.uint8(img_pix_new)) return img_new
def class_segment(img): ''' creates segmentation plot of global hue classes img: PIL image ''' img_pixels = np.array(img) hgt, wth, c = img_pixels.shape img_hls = np.zeros((hgt, wth, c), float) img_seg = np.zeros((hgt, wth), float) for y in range(hgt): for x in range(wth): img_hls[y, x] = luminance.rgb_to_hls(img_pixels[y, x]) img_temp = img_hls.transpose() img_hue_temp = img_temp[0] img_lum_temp = img_temp[1] img_sat_temp = img_temp[2] img_hues = img_hue_temp.transpose() img_lums = img_lum_temp.transpose() img_sats = img_sat_temp.transpose() for i in range(len(globalIllum_list)): glo = globalIllum_list[i] hl = glo.hl hu = glo.hu ll = glo.ll lu = glo.lu sl = glo.sl su = glo.su global_hue = glo.h_avg global_lum = glo.l_avg global_sat = glo.s_avg glo_rgb = luminance.hls_to_rgb([global_hue, global_lum, global_sat]) hues_thres = np.copy(img_hues) lums_thres = np.copy(img_lums) sats_thres = np.copy(img_sats) hues_thres[hues_thres < hl] = 0 hues_thres[hues_thres > hu] = 0 hues_thres[lums_thres < ll] = 0 hues_thres[lums_thres > lu] = 0 hues_thres[sats_thres < sl] = 0 hues_thres[sats_thres > su] = 0 hues_indices = (hues_thres != 0) img_seg[hues_indices] = i return img_seg
def plot_chart_multi(img1, img2): ''' comparison chart of global hues between img1 and img2 img1: PIL image img2: PIL image ''' img1_pixels = np.array(img1) img2_pixels = np.array(img2) hgt, wth, c = img1_pixels.shape img1_hls = np.zeros((hgt, wth, c), float) img2_hls = np.zeros((hgt, wth, c), float) for y in range(hgt): for x in range(wth): img1_hls[y, x] = luminance.rgb_to_hls(img1_pixels[y, x]) img2_hls[y, x] = luminance.rgb_to_hls(img2_pixels[y, x]) img1_temp = img1_hls.transpose() img1_hue_temp = img1_temp[0] img1_lum_temp = img1_temp[1] img1_sat_temp = img1_temp[2] img1_hues = img1_hue_temp.transpose() img1_lums = img1_lum_temp.transpose() img1_sats = img1_sat_temp.transpose() img2_temp = img2_hls.transpose() img2_hue_temp = img2_temp[0] img2_lum_temp = img2_temp[1] img2_sat_temp = img2_temp[2] img2_hues = img2_hue_temp.transpose() img2_lums = img2_lum_temp.transpose() img2_sats = img2_sat_temp.transpose() offset = 15 patchSize = 100 patchHalfsize = patchSize / 2 width = offset + (patchSize + offset) * 6 height = offset + (patchSize + offset) * 5 im = Image.new("RGB", (width, height), (255, 255, 255)) draw = ImageDraw.Draw(im) for i in range(len(globalIllum_list)): glo = globalIllum_list[i] hl = glo.hl hu = glo.hu ll = glo.ll lu = glo.lu sl = glo.sl su = glo.su global_hue = glo.h_avg global_lum = glo.l_avg global_sat = glo.s_avg hues1_thres = np.copy(img1_hues) lums1_thres = np.copy(img1_lums) sats1_thres = np.copy(img1_sats) hues2_thres = np.copy(img2_hues) lums2_thres = np.copy(img2_lums) sats2_thres = np.copy(img2_sats) avg1_hue = 0 avg1_lum = 0 avg1_sat = 0 num1_pixels = 0 avg2_hue = 0 avg2_lum = 0 avg2_sat = 0 num2_pixels = 0 hues1_thres[hues1_thres < hl] = 0 hues1_thres[hues1_thres > hu] = 0 hues1_thres[lums1_thres < ll] = 0 hues1_thres[lums1_thres > lu] = 0 hues1_thres[sats1_thres < sl] = 0 hues1_thres[sats1_thres > su] = 0 lums1_thres[hues1_thres < hl] = 0 lums1_thres[hues1_thres > hu] = 0 lums1_thres[lums1_thres < ll] = 0 lums1_thres[lums1_thres > lu] = 0 lums1_thres[sats1_thres < sl] = 0 lums1_thres[sats1_thres > su] = 0 sats1_thres[hues1_thres < hl] = 0 sats1_thres[hues1_thres > hu] = 0 sats1_thres[lums1_thres < ll] = 0 sats1_thres[lums1_thres > lu] = 0 sats1_thres[sats1_thres < sl] = 0 sats1_thres[sats1_thres > su] = 0 hues2_thres[hues2_thres < hl] = 0 hues2_thres[hues2_thres > hu] = 0 hues2_thres[lums2_thres < ll] = 0 hues2_thres[lums2_thres > lu] = 0 hues2_thres[sats2_thres < sl] = 0 hues2_thres[sats2_thres > su] = 0 lums2_thres[hues2_thres < hl] = 0 lums2_thres[hues2_thres > hu] = 0 lums2_thres[lums2_thres < ll] = 0 lums2_thres[lums2_thres > lu] = 0 lums2_thres[sats2_thres < sl] = 0 lums2_thres[sats2_thres > su] = 0 sats2_thres[hues2_thres < hl] = 0 sats2_thres[hues2_thres > hu] = 0 sats2_thres[lums2_thres < ll] = 0 sats2_thres[lums2_thres > lu] = 0 sats2_thres[sats2_thres < sl] = 0 sats2_thres[sats2_thres > su] = 0 num1_pixels = np.count_nonzero(hues1_thres.flatten()) avg1_hue = np.sum(hues1_thres.flatten()) avg1_lum = np.sum(lums1_thres.flatten()) avg1_sat = np.sum(sats1_thres.flatten()) num2_pixels = np.count_nonzero(hues2_thres.flatten()) avg2_hue = np.sum(hues2_thres.flatten()) avg2_lum = np.sum(lums2_thres.flatten()) avg2_sat = np.sum(sats2_thres.flatten()) if (num1_pixels != 0): avg1_hue /= num1_pixels avg1_lum /= num1_pixels avg1_sat /= num1_pixels if (num2_pixels != 0): avg2_hue /= num2_pixels avg2_lum /= num2_pixels avg2_sat /= num2_pixels avg1_rgb = luminance.hls_to_rgb([avg1_hue, avg1_lum, avg1_sat]) avg2_rgb = luminance.hls_to_rgb([avg2_hue, avg2_lum, avg2_sat]) glo_rgb = luminance.hls_to_rgb([global_hue, global_lum, global_sat]) ix = i % 6 iy = int(i / 6) rx = offset + (patchSize + offset) * ix ry = offset + (patchHalfsize + patchSize + offset) * iy draw.rectangle( (rx, ry, rx + patchSize, ry + patchHalfsize), fill=(int(avg1_rgb[0]), int(avg1_rgb[1]), int(avg1_rgb[2]))) draw.rectangle( (rx, ry + patchHalfsize, rx + patchSize, ry + patchSize), fill=(int(avg2_rgb[0]), int(avg2_rgb[1]), int(avg2_rgb[2]))) draw.rectangle( (rx, ry + patchSize, rx + patchSize, ry + patchSize + patchHalfsize), fill=(int(glo_rgb[0]), int(glo_rgb[1]), int(glo_rgb[2]))) draw.multiline_text( (rx + patchHalfsize - 20, ry + 2 + patchSize + patchHalfsize), '[{0:.2f}, {1:.2f}]'.format(hl, hu), fill=(0, 0, 0)) im.show() return im