Skip to content

Instantly share code, notes, and snippets.

@carlinmack
Created January 11, 2020 04:07
Show Gist options
  • Save carlinmack/601b2994225538f3642c37ff3560a89f to your computer and use it in GitHub Desktop.
Save carlinmack/601b2994225538f3642c37ff3560a89f to your computer and use it in GitHub Desktop.
# place this file in folder with pngs or jpgs
# run with python warmingstripes.py
# will work with non warming stripes images
# but will be an image with columns of the top row of pixels stretched down
from PIL import Image
import drawSvg as draw
import os
files = [i for i in os.listdir() if i.endswith("png") or i.endswith("jpg")]
for file in files:
im = Image.open(file)
width, height = im.size
colors = []
stripes = [0]
pointer = 0
for x in range(width-1):
r0,g0,b0 = im.getpixel((x,0))
r1,g1,b1 = im.getpixel((x+1,0))
if stripes[pointer] == 1:
colors.append('#%02x%02x%02x' % (r0,g0,b0))
if r0 == r1 and g0 == g1 and b0 == b1:
stripes[pointer] = stripes[pointer] + 1
else:
pointer = pointer + 1
stripes.append(1)
d = draw.Drawing(width, height)
index = 0
pointer = 0
for i in range(len(colors)):
d.append(draw.Rectangle(pointer,0,stripes[index],height, fill=colors[index]))
pointer = pointer + stripes[index]
index = index + 1
d.setPixelScale(1) # Set number of pixels per geometry unit
filename = file[:-4]
d.saveSvg(filename + '.svg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment