Created
April 27, 2020 22:51
-
-
Save renamedquery/4b3b2f26d133b28c95b3d02498eab292 to your computer and use it in GitHub Desktop.
The program that converts the black and white image to the album art
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2, numpy, PIL.Image, PIL.ImageDraw, PIL.ImageFont, sys | |
verticalLines = int(sys.argv[-1]) | |
lineIntesity = int(sys.argv[-2]) | |
margin = 205 | |
finalResolution = [1200 + 5, 1000 + margin] | |
lineWidth = 5 | |
colors = ['#303030', '#ffffff'] #background and foreground | |
positionPercentagesFinalFinal = [0.66, 0.82] #[center of where finalImg will be pasted (1+ is higher 1- is lower 1 is dead center), percentage of height from top for the text] | |
bottomText = ['Q U A R A N T I N E'] #title | |
img = cv2.imread('./black-and-white.png', cv2.COLOR_BGR2GRAY) | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
bwShape = [img.shape[1], img.shape[0]] #black and white image shape | |
finalImg = PIL.Image.new('RGBA', finalResolution, colors[0]) | |
finalImageDraw = PIL.ImageDraw.Draw(finalImg) | |
minMax = cv2.minMaxLoc(img) | |
minimumPixelValue = minMax[0] | |
maximumPixelValue = minMax[1] | |
for yPixel in range(verticalLines): | |
lastPosition = [0, int(finalResolution[0] // verticalLines) * yPixel] | |
lastPositionIntensity = lastPosition[1] | |
for xPixel in range(bwShape[0]): | |
color = int(img[int(yPixel * (bwShape[1] / verticalLines)), xPixel]) | |
distFromMin = abs(minimumPixelValue - color) | |
rangeTotal = maximumPixelValue - minimumPixelValue | |
intensity = int(distFromMin / rangeTotal * lineIntesity) | |
currentPosition = [int((xPixel / bwShape[0]) * finalResolution[0]), lastPosition[1]] | |
finalImageDraw.line([lastPosition[0], lastPositionIntensity, currentPosition[0], currentPosition[1] - intensity + int(margin / 2)], fill = colors[1], width = lineWidth) | |
lastPositionIntensity = currentPosition[1] - intensity + int(margin / 2) #forgot why this is needed but if i remove it the thing breaks so whatever | |
lastPosition = currentPosition | |
finalImg = finalImg.crop([5, 0, finalImg.width, finalImg.height]) #crop the leftmost 5 pixels to get rid of the white line (yes thats my best solution :/) | |
finalFinalImg = PIL.Image.new('RGBA', [int(finalResolution[0] * 1.5), int(finalResolution[1] * 1.5)], colors[0]) | |
finalFinalImg.paste(finalImg, [int((finalFinalImg.width - finalImg.width) / 2), int(((finalFinalImg.height - finalImg.height) / 2) * positionPercentagesFinalFinal[0])]) | |
finalFinalImageDraw = PIL.ImageDraw.Draw(finalFinalImg) | |
font1 = PIL.ImageFont.truetype('./noto-mono-regular.ttf', int(finalResolution[1] / 10)) | |
fontSizeLine1 = finalFinalImageDraw.textsize(bottomText[0], font = font1) | |
finalFinalImageDraw.text([int((finalFinalImg.width - fontSizeLine1[0]) / 2), int(finalFinalImg.height * positionPercentagesFinalFinal[1])], bottomText[0], colors[1], font = font1) | |
finalFinalImg.save('output.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment