r/Python • u/Dirt2Master • Oct 16 '19
My previous post on the algorithm i made to create these images blew up, so here is another! WITH a Github link: https://github.com/TyperOfCode/image_algorithm
31
u/aviv57 Oct 16 '19
For all of those who want to click the link from mobile, I got you: https://github.com/TyperOfCode/image_algorithm
3
21
37
u/DuckSaxaphone Oct 16 '19
Thanks for posting GitHub! The output of this code looks great, I really like the smoothed out, painted look.
Just a few thoughts:
It's not a genetic algorithm for sure. It looks like you're adding a random new triangle to your best image a thousand times and taking the most improved image forward?
You could probably save a lot of run time by holding more things in memory and saving every N iterations. There's a lot of saving and loading going on every iteration.
If you add a tolerance parameter, you can count how many iterations go by with the score not changing by at least the tolerance. If that count reaches something like 5 then that's a natural point to stop iterating.
6
u/Dirt2Master Oct 16 '19
Yeah, im optmising it now, and did you take into account that it actually takes the previous best triangle's color and randomises it off that.
for sure i'll try to make it faster, less run time.
i made this program quite a while ago and i have progressed a lot since then :)
5
u/DuckSaxaphone Oct 16 '19
I did take that into account. It's honestly a clever optimization algorithm for the purpose of randomly approaching an approximation of the original image. It's just not the right algorithm if you're teaching yourself about genetic algorithms.
0
Oct 16 '19
[deleted]
2
u/dashdanw Oct 16 '19
It's not a genetic algorithm for sure. It looks like you're adding a random new triangle to your best image a thousand times and taking the most improved image forward?
im running it as we speak and I'd love to see it run faster, also pretty decent at python if i can help
1
u/Dirt2Master Oct 17 '19
i have made a beta version which runs 4x faster but turns up with a lot of noise.
1
u/dashdanw Oct 17 '19
just pulled but not sure about this error:
Corona:image_algorithm dashdanw$ python3 main.py resizing from 164/160 resized to 164/160 > Converted the image to the dimensions of: 164 x 160 Traceback (most recent call last): File "main.py", line 115, in <module> Scores.append(score(Real_IM,Best)) File "main.py", line 37, in score pix1=np.array(OrgImg, dtype=np.uint64).reshape((h, w, 3))[:, :, :3] ValueError: cannot reshape array of size 104960 into shape (160,164,3) Corona:image_algorithm dashdanw$ python3 main.py resizing from 164/160 resized to 164/160 > Converted the image to the dimensions of: 164 x 160 Traceback (most recent call last): File "main.py", line 115, in <module> Scores.append(score(Real_IM,Best)) File "main.py", line 37, in score pix1=np.array(OrgImg, dtype=np.uint64).reshape((h, w, 3))[:, :, :3] ValueError: cannot reshape array of size 104960 into shape (160,164,3)1
Oct 18 '19
You used a similar Image as I did: it has 4 Channels, could be RGBA instead of RGB.
You need to do the following :
Real_IM = Real_IM.convert("RGB")
5
u/x2f Oct 16 '19
Why not b = 10**53 at line 192?
4
u/Dirt2Master Oct 16 '19
i was inexperienced when i wrote this 😂 , thanks.
would be helpful if you can suggest to me how i can make it faster
3
u/wernersbacher Oct 16 '19
Can you explain your algorithm a little? Looks interesting!
2
u/Dirt2Master Oct 16 '19
https://www.reddit.com/r/Python/comments/dhftb0/made_a_genetic_algorithm_with_the_task_of/
Just read the comment section ;)
3
Oct 16 '19 edited Oct 16 '19
Hello: What you need to do is improve the score function, that should be the highest speed increase you can get.
def score(OrgImg, ScoreImg):
w, h = OrgImg.size
w1, h1 = ScoreImg.size
if w1 != w or h1 != h:
print("Error at Score funciton\n\n")
exit()
pix1=np.array(OrgImg).reshape((w, h, 4))[:, :, :3]
pix2 = np.array(ScoreImg).reshape((w1, h1, 3))
diff = np.sqrt(((pix1 - pix2)**2).sum(axis=-1)).sum()
return diff1
1
u/Dirt2Master Oct 17 '19
Although it is faster, it is not accurate at all, if anything, its completely random
2
Oct 17 '19
Found my mistake. I needed to first convert the arrays to uint64. Before they where uint8, which then lead to overflow with the squaring. Now it gives the exact same values for the picture i tried like your function. Here the new code:
def score(OrgImg, ScoreImg): w, h = OrgImg.size w1, h1 = ScoreImg.size if w1 != w or h1 != h: print("Error at Score funciton\n\n") exit() pix1=np.array(OrgImg, dtype=np.uint64).reshape((h, w, 4))[:, :, :3] pix2 = np.array(ScoreImg, dtype=np.uint64).reshape((h1, w1, 3)) diff = (np.sqrt(np.square(pix1 - pix2).sum(axis=-1)).sum()) return diff1
u/Dirt2Master Oct 17 '19
this still errors, but i fixed ot. instead of reshape((h,w,4) you need to put it as 3
thanks man, improved the speed so much :)
2
2
u/w33tikv33l Oct 16 '19
I once made a triangle painter too using a genetic algorithm. I never got to the level of performance I was aiming for though. Here is an screenshot: https://imgur.com/a/Edi8eSZ
2
2
u/4shtonButcher Oct 17 '19
I read 12 hours and stopped myself from downloading - dare sharing which hardware that estimate is based on?
1
u/Dirt2Master Oct 17 '19
i use a macbook air 2017. doesnt need a lot of power, make sure your fork or version is up to date with the one currently on github
2
u/4shtonButcher Oct 17 '19
Thanks! Work laptop is probably already faster then but I think I will try it at home on my desktop in order to not wait over night. Might be a cool side-by-side thing with a holiday photo.
6
2
2
2
0
55
u/Big_Witch Oct 16 '19
Now you need to feed this whole meme through it.