TotallyNotChase / glitch-this

:camera: Glitchify images and GIF - with highly customizable options!
MIT License
1.75k stars 63 forks source link

(Feature Request) Glitch an image without the horizontal shifted bars and seed based glitching #25

Closed Tremeschin closed 3 years ago

Tremeschin commented 3 years ago

Hey I'll be using this project on a project of mine about music visualization and yes adding glitch effects worked pretty nicely however the horizontal cropped bars got a bit too much distracting applying this image glitcher on every frame.

Another issue with it is that there is no "seed based glitching" (don't know if that's possible with current code base, haven't looked at it yet) and it gets too fast for the eye to understand what is happening.

This seed based idea thingy is that when you set a seed value to the glitcher, the same glitch "position and content" will be the same but the intensity changes only.

So I'd like an option to turn off the horizontal bars, only apply glitch on color transpose and if possible to implement this seed generation.

I'll be looking at this repo code base at some point in the future (perhaps fork it and PR these features?), thanks!!

TotallyNotChase commented 3 years ago

The horizontal shift is actually the basis of this glitching algorithm. The shift of color that you want to keep is the cause of the horizontal bar shift. Essentially a channel is chosen at random and shifted to a random offset. This causes the color transpose which will look like a horizontal bar as it is applied across the entire row. You can see that here and here. I don't think it'd be possible to remove it.

Seed based glitching is an interesting idea, but I'm not sure whether it's applicable. The algorithm already uses sort of a seed. That's your glitch amount. How much to offset the horizontal bars by is determined here. It might be possible to implement seed there but I'm not quite sure what'd be the best approach.

Have you tried with lesser glitch amounts? It supports floats too for finer control. Maybe that'll give you a less "moving" glitched GIF.

Tremeschin commented 3 years ago

We could add a random.seed() if you only use Python's builtin random module I guess?

At least that's the way I have to do something like, sending a new seed id for multiprocessing workers so they do not generate the same thingy for me when they run.

No I have not tried and tested much your project, I was fixing and reorganizing code on this project I'm working on before moving on :)

Edit: here's the random.seed function I referred to for feeding other python Processes to generate different images

I actually just found out and fixed, I was doing random.seed(image_id) and in multiple runs it generated the same set of images

Tremeschin commented 3 years ago

Hey I just forked your repo and tested it really quick, yes I can generate same images on same seeds but not on different amount of glitches (sorta), here's the commits

The images with changing values they almost work, their arrays get transposed to the left or right as time goes on progressively but not randomly, as expected, but the color_offset doesn't quite work

I'll have to look at the code where that is applied.

Tremeschin commented 3 years ago

Yes! this really worked, I had to create a self.seed class variable and do a random.seed(self.seed) at the start of __get_random_channel (for consistent color shifting) and glitch_image (for cropping and __glitch_left __glitch_right at the same spot but different intensities) :)

I can't really follow your code style and how you'd implement this your own way so I won't PR or something

TotallyNotChase commented 3 years ago

I checked your code. If you want, I think you can PR the seed as an optional argument like you've now. And essentially set the random.seed only when that argument is present. That way, the library will still have the same the logic as it now has without that argument but will also have a new feature.

I can set up the commandline script and the docs soon afterwards

Tremeschin commented 3 years ago

I'll PR tomorrow then, it's kinda late for me.

I'm wondering right now if only setting the seed on glitch image function will work with GIFs as well, will check this code flow and test on a gif file later on as I'm heading to bed :)

Tremeschin commented 3 years ago

Created pull request!! (first time doing so, sorry if I did anything wrong lol)

Code seemed to work, commented and followed your code style as much as I could

TotallyNotChase commented 3 years ago

@Tremeschin This feature is currently available on the preview-release branch. Along with some more goodies. Mostly clean ups but also full typing support for a better experience overall for the library users.

Everything in preview-release will be rolling out to the main release soon, after I get the docs and changelog ready a bit. If you'd like, you can test out the new feature (and maybe even play around with the old features) on the preview-release branch by doing python3 ./setup.py develop :)

Once again, thanks for the feature.

Tremeschin commented 3 years ago

I'm a bit busy doing full code review and commenting / type hinting on a project, I'll be testing those new stuff you said in that branch when I'm done with these (1, 2 days or so) :)

To be honest I might even code my own glitch effect at some point with some concepts from your code, it's a really specific use case I need, yours work really good for a glitch on the full final frame but I was looking for something less aggressive and more progressive / smooth on sub images within a image.

I'll be sure to link your project as I'm taking a lot of inspiration from it, I'll be using this for quite some time I think (that is until I start to R&D and break stuff again). Thanks!!

I'll let you know if I end up venturing myself on writing such a thing, I think your project can get some ideas of the implementation I'm thinking on making.