Simon Kellet 2 weeks ago
parent 351b6df779
commit 9c5d586bd3
  1. 4
      .gitignore
  2. 60
      glitch.go
  3. BIN
      glitch_img
  4. 3
      go.mod
  5. 51
      main.go
  6. 17
      util.go

4
.gitignore vendored

@ -0,0 +1,4 @@
*.gif
*.jpeg
*.png
.DS_Store

@ -0,0 +1,60 @@
package main
import (
"math/rand/v2"
"os"
"path/filepath"
)
func makeOutputDir() {
// Default to just "output"
err := os.Mkdir("output", 0755)
check(err, "cannot make directory")
}
func glitchImage(inputFile string, outputFile string, shitSize int64, iterations int64) error {
// Ensure the output directory exists
makeOutputDir()
// 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")
// fmt.Printf("Successfully wrote %d bytes of shit at offset %d in file %s\n", shitSize, offset, outputFilePath)
}
return nil
}

Binary file not shown.

@ -0,0 +1,3 @@
module glitch_img
go 1.22.3

@ -0,0 +1,51 @@
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
)
func main() {
if len(os.Args) < 5 {
fmt.Println("Usage: go run main.go <source-file> <number-of-copies> <shit-size> <iterations>")
return
}
sourceFile := os.Args[1]
numCopies, err := strconv.Atoi(os.Args[2])
if err != nil || numCopies <= 0 {
fmt.Println("Invalid number of copies. Must be a positive integer.")
return
}
shitSize, err := strconv.ParseInt(os.Args[3], 10, 64) // Parse shitSize as int64
if err != nil || shitSize <= 0 {
fmt.Println("Invalid shit size. Must be a positive integer.")
return
}
iterations, err := strconv.ParseInt(os.Args[4], 10, 8)
for i := 1; i <= numCopies; i++ {
outputFile := fmt.Sprintf("%s_glitched_%d%s",
stripExtension(sourceFile),
i,
filepath.Ext(sourceFile),
)
err := glitchImage(sourceFile, outputFile, shitSize, iterations)
if err != nil {
fmt.Printf("Error glitching file %d: %v\n", i, err)
}
}
fmt.Println("GENREATING GIF YEAHH ")
// now run ffmpeg to make a gif!
cmd := exec.Command("ffmpeg", "-y", "-framerate 60", "-i", "./output/img_glitched_%d.jpeg output.gif")
err = cmd.Run()
check(err, "cannot run ffmpeg command!")
//
}

@ -0,0 +1,17 @@
package main
import (
"fmt"
"path/filepath"
)
func check(e error, s string) {
if e != nil {
fmt.Errorf(s)
}
}
func stripExtension(filename string) string {
ext := filepath.Ext(filename)
return filename[:len(filename)-len(ext)]
}
Loading…
Cancel
Save