You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
glitch_img/glitch.go

89 lines
2.5 KiB

1 month ago
package main
import (
"fmt"
1 month ago
"math/rand/v2"
"os"
"path/filepath"
)
func glitchImage(inputFile string, outputFile string, shitSize int64, iterations int64) error {
// Prepend the "output" directory to the output file
outputFilePath := filepath.Join("output", outputFile)
// Open the source file for reading
source, err := os.Open(inputFile)
check(err, "failed to open source file: %w")
defer source.Close()
// Create a new file for writing the glitched copy
destination, err := os.Create(outputFilePath)
check(err, "failed to create output file: %w")
defer destination.Close()
// Copy the contents of the source file to the destination
_, err = destination.ReadFrom(source)
check(err, "failed to copy file contents: %w")
// Get the Stat() of the destination file
fileInfo, err := destination.Stat()
check(err, "failed to get file info: %w")
// Get the size of the destination file
fileSize := fileInfo.Size()
// Generate a random offset within the file
var i int64
for i = 0; i < iterations; i++ {
offset := rand.Int64N(fileSize)
// Seek to the random offset in the destination file
_, err = destination.Seek(offset, 0)
check(err, "failed to seek to random offset: %w")
// Generate random data and write it in
shit := make([]byte, shitSize)
_, err = destination.Write(shit)
check(err, "failed to write shit data: %w")
}
return nil
}
func copyImage(inputFile string, outputFile string) error {
// Prepend the "output" directory to the output file
outputFilePath := filepath.Join("output", outputFile)
// Open the source file for reading
source, err := os.Open(inputFile)
check(err, "failed to open source file: %w")
defer source.Close()
// Create a new file for writing the copy
destination, err := os.Create(outputFilePath)
check(err, "failed to create output file: %w")
defer destination.Close()
// Copy the contents of the source file to the destination
_, err = destination.ReadFrom(source)
check(err, "failed to copy file contents: %w")
return nil
}
1 month ago
func generateGlitchedSequence(inputFile string, fileExt string, totalImages int, shitSize int64, iterations int64, chanceToRandom int32) {
for i := 1; i <= totalImages; i++ {
1 month ago
outputFileName := fmt.Sprintf("img_glitched_%d.%s", i, fileExt)
if rand.Int32N(100) < chanceToRandom {
// Glitched image
err := glitchImage(inputFile, outputFileName, shitSize, iterations)
check(err, "failed to glitch image: %w")
} else {
// Normal image
err := copyImage(inputFile, outputFileName)
check(err, "failed to copy normal image: %w")
}
}
}