r/Python • u/lhad89 • Jan 11 '20
I wrote a little python package to render images as continuous lines with varying thicknesses using matplotlib
68
u/knestleknox I hate R Jan 11 '20
you should MIT license it! (if you want of course). I'm sure lots of people would want to use this for projects and would be worried about licensing issues.
45
2
u/nostril_extension Jan 12 '20
Or better yet GPL it.
1
u/lhad89 Jan 12 '20
Why GPL in this case? I'll be honest, I don't really know the difference.
5
u/nostril_extension Jan 12 '20 edited Jan 12 '20
Copyleft license(like GPL) prevents someone from taking your work and closing it down and selling it - any future edition that uses your work must be free and open too. While MIT license does not prevent that and it's basically public domain with "don't blame the author if something breaks clause".
In general if you believe in Libre software and ideology Copyleft contributes to preserving communities from the influence of greed and general game theory abuse.
5
u/lhad89 Jan 12 '20
Ahh, I see, thanks. But given that I've added an MIT license to it already, does that stop me from making it GPL instead?
2
u/yvrelna Jan 13 '20
Note that if you use GPL, that will prevent the library from being used as part of a commercial project. If that's what you want, that's fine, but it'll limit the number of users for the library to GPL compatible projects; note that not all open source licenses are GPL-compatible.
If you want a balance between allowing commercial/non GPL compatible open source projects to use your library and preventing closed source derivatives of your project, you might want to consider LGPL instead.
1
10
2
u/campbellm Jan 12 '20
Be aware that if you want companies to use it, regardless of how good it is, many/most big ones don't allow GPL. Not for any rational reason, but legal departments get paid to say "no", and it hasn't been tested in court.
1
1
u/iekiko89 Jan 12 '20
for those of us just looking into python do you have a good link for a read up on mit license?
5
1
u/yvrelna Jan 13 '20
Just read the license itself for a start. MIT license is one of the shortest license there is.
But if you need the TL;DR version anyway, it's "you can do whatever you want with it, as long as you keep the attribution and license notice".
8
15
u/jabbeboy Jan 11 '20
That's either Jeff Bezoz or Bruce Willis xD or that guy Larry something.
12
u/lhad89 Jan 11 '20
Haha! Yep, it is that guy Larry something. Wasn't sure if anyone was going to get it.
6
5
2
2
2
15
Jan 11 '20
Maybe turn it in a flask web app, so we can edit, our own photos?
15
u/lhad89 Jan 11 '20
Good idea. I'll add it to the to do list. For now you can use SpeckWidget which creates an ipywidget in a jupyter notebook to play with the config parameters. Not as good, I know, but it's something.
4
u/LoboBandito Jan 12 '20
I wish I knew what you were talking about. I’m learning and this is awesome!
3
u/lhad89 Jan 12 '20
If you follow the install instructions from the github link I posted and copy the code underneath "Interactive Widget" into a jupyter notebook it should work. It doesn't have all the options but has the main ones you'd want to play with. DM me if you're having any trouble and I'll help you out.
2
5
u/laterral Jan 11 '20
This is dope! The thickness I get, but what's driving the lines' curves?
11
u/lhad89 Jan 11 '20
I added a configuration option to add some random noise to each line to make it look a bit more hand drawn. In this case, it's the product of a bunch of random sine waves (random frequency and phase offset).
8
u/prajitura_fermecata Jan 11 '20
This is really interesting. How did you get the inspiration to do it?
13
u/lhad89 Jan 11 '20
I saw a reddit post a while back with someone drawing like this freehand which looked pretty cool. Prompted me to see if I could recreate it with code. Can't for the life of me find that post anymore though.
2
u/Hrothehun Jan 12 '20
Amazing script, happy to see such cool projects every now and then :))
Could you share with us the particular configuration for this image that you generated?
2
u/lhad89 Jan 12 '20
Sure, well the input image is https://i.imgur.com/fzUVbTU.jpg . All I did was scale down the original and pump up the contrast.
Then code wise:
from speck import SpeckPlot, SineNoise, CmapColours = SpeckPlot.from_path('ld.jpg', scale_factor=20)s.draw(weights=(0.2, 0.6), noise=SineNoise(scale=0.7), colour=CmapColour('Oranges'))s.save('out.jpg')The weights need to be in the range (0-1) and basically determine the thickness that white and black map to, respectively. The noise adds random offsets to the lines to make them look a bit hand-drawn in this case and the colour colours each line according to the 'Oranges' matplotlib cmap. There's a bunch of other options which are documented at the bottom of the README if you're interested.
2
u/L33monJuic33 Jan 12 '20
What would happen if you reversed the thickness from top to bottom?
2
u/lhad89 Jan 12 '20
You can reverse the weights so that white maps to thick lines and black to thin. In this example, it would look like this: https://i.imgur.com/xY4GMbM.jpg
2
Jan 12 '20
This is so cool. How did you go about doing this if you could explain? Like what is the rough algorithm?
3
u/lhad89 Jan 12 '20
It's actually pretty straight forward. Basically, I convert the image to greyscale and then draw two mirrored lines either side of the point half way up the row of pixels with the area between them filled in. The lines are 1 unit apart while on a pixel that's black, and 0 units apart when on a pixel that's white and something in between for grey. I then use a sigmoid function to smooth the boundaries between each adjacent pixel to avoid it looking blocky. Here's a zoomed in version of what's going on: https://i.imgur.com/mbWbMfW.png (each black square is one pixel from the original image) After that I'm just adding some random noise and a colour map.
1
Jan 12 '20
This is fascinating. I am not sure I understand everything but I think I get the idea. Thanks for sharing!
25
u/maxbridgland Jan 11 '20
I just can't seem to curb my enthusiasm with this, it's so cool. Well done!
4
7
1
u/sage_x10 Jan 12 '20
Any tips or resources on how someone would learn to do something like this?
4
u/lhad89 Jan 12 '20
Well the heavy lifting is done by numpy so if you want to learn that I'd suggest looking into python data analysis content (which will likely also include pandas). I didn't use them myself but sentdex on youtube has good videos and he also has a series on matplotlib. My learning process was more one of trial and error and stackoverflow. More generally, I thought the book "Fluent Python" was excellent but if you're brand new to python might be a bit daunting at first.
3
3
3
3
4
2
2
2
u/SweetSoursop Jan 12 '20
Cant tell if it's Bernie Sanders, Clint Eastwood or Larry David
Now I need to make a package that compares these pictures to a dataset of famous people pictures.
1
1
1
1
1
1
u/Chevaboogaloo Jan 12 '20
It would be cool to see this on a display with a camera. Like some sort of funky mirror.
1
1
1
1
1
1
1
Jan 13 '20
[removed] — view removed comment
1
u/lhad89 Jan 13 '20 edited Jan 13 '20
I've pushed up a change that should make it a fair bit faster but nevertheless, I'd suggest scaling down the image. There's a size argument you can pass to the constructor that will scale down the image. Something like resize=100 should be a good place to start which will scale it down to 100 pixels on the long edge and maintain the aspect ratio. For reference, the input to the image I posted here was 56x60 pixels. The reason you want it fairly small is that it gets upscaled quite significantly as each row of pixels gets replaced with a smoothed line.
114
u/lhad89 Jan 11 '20
source code here: https://github.com/lucashadfield/speck