Example #1
0
def affectOnLineContrast(img, contrast=10, span=10, vertical=False, randomise=False, ifContrastLessThan=True):
  if vertical:
    img = np.swapaxes(img, 0, 1)
  newImg = np.asarray(img[:])
  ymax = img.shape[0]-1
  xmax = img.shape[1]-1
  print ""
  for y in range(0, ymax):
    tools.displayPercentage("running outlines... ", y, ymax)
    for x in range(span, xmax-span):
      maxVal = 0
      minVal = 255
      avrg = 0
      for i in range(x-span, x+span):
        if max(img[y, i, 0:2]) > maxVal:
          maxVal = max(img[y, i, 0:2])
        elif min(img[y, i, 0:2]) < minVal:
          minVal = min(img[y, i, 0:2])
        avrg += img[y, i, 0]

      avrg /= (span * 2 + 1)

      if ifContrastLessThan == ((maxVal - minVal) < contrast):
        if randomise:
          randY = int(y - (span / 2) + random.random() * span)
          new_red = getRandomColourFromYSpan(img, randY, x, span, ymax, 0)
          new_green = getRandomColourFromYSpan(img, randY, x, span, ymax, 1)
          new_blue = getRandomColourFromYSpan(img, randY, x, span, ymax, 2)
        else:
          new_red = int(1. * (int(img[wrap(ymax, y), wrap(xmax, x-1), 0]) + int(img[wrap(ymax, y), wrap(xmax-1, x-2), 0]) + int(img[wrap(ymax, y), wrap(xmax, x+1), 0]) + int(img[wrap(ymax, y), wrap(xmax, x+2), 0])) / 4)
          new_green = int(1. * (int(img[wrap(ymax, y), wrap(xmax, x-1), 1]) + int(img[wrap(ymax, y), wrap(xmax-1, x-2), 1]) + int(img[wrap(ymax, y), wrap(xmax, x+1), 1]) + int(img[wrap(ymax, y), wrap(xmax, x+2), 1])) / 4)
          new_blue = int(1. * (int(img[wrap(ymax, y), wrap(xmax, x-1), 2]) + int(img[wrap(ymax, y), wrap(xmax-1, x-2), 2]) + int(img[wrap(ymax, y), wrap(xmax, x+1), 2]) + int(img[wrap(ymax, y), wrap(xmax, x+2), 2])) / 4)
          
        if new_red > 10 or new_green > 10 or new_blue > 10:
          newImg[y][max(x-span, 0):min(x+span, xmax)][:] = [new_red, new_green, new_blue]

  if vertical:
    newImg = np.swapaxes(newImg, 0, 1)

  return newImg
def linify(img, separateColours=False, lineFactor=4, lean=0, allowLineMerging=False):
	if lineFactor == 0: lineFactor = 1

	print ""

	newImg = np.zeros((img.shape))

	colourMap = {0:"red",1:"blue",2:"green"}

	numberOfLines = int(1.0 * img.shape[1] / lineFactor) - 1

	gradientArray = np.zeros((img.shape[0], img.shape[1] - 1, img.shape[2]))
	imgTemp = img[:, 1:, :]
	gradientArray = img[:, :-1, :] - imgTemp


	imgWidth = img.shape[1]-2

	for colour in range(0, 2):
		offset = colour if separateColours else (lineFactor / 2)

		previousPositions = np.zeros((img.shape[0]))
		previousPositions = [x*lineFactor + 1 for x in range(0, numberOfLines)]
		newImg[0, 2::lineFactor, :] = img[0, 2::lineFactor, :]
		for i in range(1, img.shape[0]):
			displayPercentage("running linify for colour %s... " % colourMap[colour], i, img.shape[0])
			for j in range(0, numberOfLines):
				direction = 0
				if gradientArray[i, wrap(imgWidth-1, previousPositions[j]), colour] - int(gradientArray[i, wrap(imgWidth-1, previousPositions[j]-1), colour]) > lean:
					direction = 1
				elif gradientArray[i, wrap(imgWidth-1, previousPositions[j]), colour] - int(gradientArray[i, wrap(imgWidth-1, previousPositions[j]-1), colour]) < lean:
					direction = -1

				if separateColours:
					newImg[i, wrap(imgWidth, previousPositions[j]+direction), colour] = img[i, wrap(imgWidth, previousPositions[j]+direction), colour]
				else:
					newImg[i, wrap(imgWidth, previousPositions[j]+direction), 0] = img[i, wrap(imgWidth, previousPositions[j]+direction), 0]
					newImg[i, wrap(imgWidth, previousPositions[j]+direction), 1] = img[i, wrap(imgWidth, previousPositions[j]+direction), 1]
					newImg[i, wrap(imgWidth, previousPositions[j]+direction), 2] = img[i, wrap(imgWidth, previousPositions[j]+direction), 2]

				if allowLineMerging or previousPositions[wrap(numberOfLines-1, j+1)] > previousPositions[j]:
					previousPositions[j] += direction
					previousPositions[j] = wrap(imgWidth, previousPositions[j])


	return newImg