Showing posts with label jpeg rotation. Show all posts
Showing posts with label jpeg rotation. Show all posts

Degrading jpeg images with repeated rotation - via Bash (FFmpeg and ImageMagick)



A continuation of the decade-old topic of degrading jpeg images by repeated rotation and saving. This post briefly demonstrates the process using FFmpeg and ImageMagick in a Bash script. Previously, a Python script achieving similar results was published, which has recently been updated. There are many posts on this subject and they can all be accessed by searching for 'jpeg rotation' tag.
posts: https://oioiiooixiii.blogspot.com/search/label/jpeg%20rotation
The two basic commands are below. Both versions rotate an image 90 degrees clockwise, and each overwrite the original image. They should be run inside a loop to create progressively more degraded images.

ImageMagick: The quicker of the two, it uses the standard 'libjpg' library for saving images.
mogrify -rotate "90" -quality "74" "image.jpg"

FFmpeg: Saving is done with the 'mjpeg' encoder, creating significantly different results.
ffmpeg -i "image.jpg" -vf "transpose=1" -q:v 12 "image.jpg" -y

There are many options and ways to extend each of the basic commands. For FFmpeg, one such way is to use the 'noise' filter to help create entropy in the image while running. It also has the effect of discouraging the gradual magenta-shift caused by the mjpeg encoder.

A functional (but basic) Bash script is presented later in this blog post. It allows for the choice between ImageMagick or FFmpeg versions, as well as allowing some other parameters to be set. Directly below is another montage of images created using the script. Run-time parameters for each result are given at the end of this post.



Running the script without any arguments (except for the image file name) will invoke ImageMagick's 'mogrify' command, rotating the image 500 times, and saving at a jpeg quality of '74'. Note that when the FFmpeg version is running, the jpeg quality is crudely inverted, to use the 'q:v' value of the 'mjpeg' encoder.

The parameters for the script: [filename: string] [rotations: 1-n] [quality: 1-100] [frames: (any string)] [version: (any string for FFmpeg)] [noise: 1-100]
#!/bin/bash
# Simple Bash script to degrade a jpeg image by repeated rotations and saves,
# using either FFmpeg or ImageMagick. N.B. Starting image must be a jpeg.

# Example: rotateDegrade.sh "image.jpg" "1200" "67" "no" "FFmpeg" "21"
# Run on image.jpg, 1200 rotations, quality=67, no frames, use FFmpeg, noise=21

# source: oioiiooixiii.blogspot.com
# version: 2019.08.22_13.57.37

# All relevent code resides in this function
function rotateDegrade()
{
   local rotations="${2:-500}" # number of rotations
   local quality="${3:-74}" # Jpeg save quality (note inverse value for FFmpeg)
   local saveInterim="${4:-no}" # To save every full rotation as a new frame
   local version="${5:-IM}" # Choice of function (any other string for FFmpeg)
   local ffNoise="${6:-0}" # FFmpeg noise filter

   # Name of new file created to work on
   local workingFile="${1}_r${rotations}-q${quality}-${version}-n${ffNoise}.jpg"
   cp "$1" "$workingFile" # make a copy of the input file to work on
   # N.B. consider moving above file to volatile memory e.g. /dev/shm

   # ImageMagick and FFmpeg sub-functions
   function rotateImageMagick() {
      mogrify -rotate "90" -quality "$quality" "$workingFile"; }
   function rotateFFmpeg() {
      ffmpeg -i "$workingFile" -vf "format=rgb24,transpose=1,
         noise=alls=${ffNoise}:allf=u,format=rgb24" -q:v "$((100-quality))"\
         "$workingFile" -y -loglevel panic &>/dev/null; }

   # Main loop for repeated rotations and saves
   for (( i=0;i<"$rotations";i++ ))
   {
      # Save each full rotation as a new frame (if enabled)
      [[ "$saveInterim" != "no" ]] && [[ "$(( 10#$i%4 ))" -lt 1  ]] \
      && cp "$workingFile" "$(printf %07d $((i/4)))_$workingFile"

      # Rotate by 90 degrees and save, using whichever function chosen
      [[ "$version" == "IM" ]] \
      && rotateImageMagick \
      || rotateFFmpeg

      # Display progress
      displayRotation "$i" "$rotations"
   }
}

# Simple textual feedback of progress shown in terminal
function displayRotation() { clear;
   case "$(( 10#$1%4 ))" in
   3) printf "Total: $2 / Processing: $1 πŸ‘„  ";;
   2) printf "Total: $2 / Processing: $1 πŸ‘‡  ";;
   1) printf "Total: $2 / Processing: $1 πŸ‘†  ";;
   0) printf "Total: $2 / Processing: $1 πŸ‘…  ";;
   esac
}

# Driver function
function main { rotateDegrade "$@"; echo; }; main "$@"
download: rotateDegrade.sh

python version: https://oioiiooixiii.blogspot.com/2014/08/jpeg-destruction-via-repeated-rotate.html
original image: https://www.flickr.com/photos/flowizm/19148678846/ (CC BY-NC-SA 2.0)

parameters for top image, left to right:
original | rotations=300,quality=52,version=IM | rotations=200,quality=91,version=FFmpeg,noise=7

parameters for bottom image, left to right:
rotations=208,quality=91,version=FFmpeg,noise=7 | rotations=300,quality=52,version=FFmpeg,noise=0 | rotations=500,quality=74,version=IM | rotations=1000,quality=94,version=FFmpeg,noise=7 | rotations=300,quality=94,version=FFmpeg,noise=16

JPEG destruction: 0° vs. 90° vs. 180°



As a follow-up to an experiment involving the degradation of images, seen when rotated in Microsoft's "Image and Fax Viewer", a program was devised to investigate how much of the degradation was caused by the rotation. The image above illustrates that merely saving the image repeatedly, is not conducive to the deterioration. Also interesting is the slight, but discernible, effect 180° rotations have in the process.

context: http://oioiiooixiii.blogspot.com/2014/08/the-effects-of-rotating-images-in.html

The program was originally completed using Processing, but this proved cumbersome as it was unable to handle so many I/O file operations. A program to rotate and save images was later rewritten in Python, which was not hampered by any such issues.

context: http://oioiiooixiii.blogspot.com/2014/08/jpeg-destruction-via-repeated-rotate.html

Content from this blog post was originally published in February 2013

JPEG destruction, via repeated rotate and saves - Python 3.0+ implementation



A previous blog post detailed experimentation with Windows XP's image viewer, to degrade jpeg images. Windows Vista/7/8 fixed this issue with the native viewer, so it no longer saves after each rotation. To implement the process in operating systems outside of Windows XP, a custom algorithm had to be formed. I tried implementing this with Processing, but it was cumbersome to achieve. In Python, using the PIL module, the procedure is trivial.

UPDATE: 2019.08.03

Having recently reviewed the original Python script included in this post, it was clear it could do with serious improvements. An updated, simpler and more logical version (with comments), has been included below. The original script can be found underneath it.

from PIL import Image

# Image file to rotate
filename = "image.jpg"

# Amount of times image is rotated
for i in range(0, 1000):
   # Open image file
   img = Image.open (filename)
   # Rotate file 90 degrees and save it
   img.rotate(90,expand=True).save(filename, "JPEG")
   # If image has rotated 4 times (360 degrees) save new image
   if (i % 4) == 0:
      img.save (str(i/4)+".jpg", "JPEG")

Old version:
from PIL import Image

# Keeps track of orientation
flipBack = 90

# Bootstrapping first image
img = Image.open("original.jpg")
img.rotate(270).save("0.jpg", "JPEG")

# Loads file, rotates, saves as new file
for i in range(0, 19):
    img = Image.open(str(i)+".jpg")
    img.rotate(270).save(str(i+1)+".jpg", "JPEG")

    # Rotates back to original orientation, saves over file
    img.rotate(flipBack).save(str(i)+".jpg", "JPEG")
    if flipBack <= 180:
        flipBack += 90
    else:
        flipBack = 0



related: http://oioiiooixiii.blogspot.ie/2014/08/the-effects-of-rotating-images-in.html

The effects of rotating images in Window XP


A few years ago, I experimented with Windows XP's "Image and Fax Viewer", to see what repeated rotations, numbering hundreds and thousands, would do to a jpeg image. Each rotation would cause the image to be automatically saved, which caused substantial degradation to the image. The procedure was automated by writing a vbscript which interacted with the image viewer. These are some images from the original tests.


Emilee Cox is a girl that once sent over 30,000 text messages from her phone in a month. I thought it only right to rotate her image the same amount of times, but as you can see from the results, she didn't even last for a thousand.

source: http://emileecox.blogspot.com/



Bill Gates proved a tough nut to crack. Still discernible after thirty thousand rotations.


With entropy increasing, so did file size, as more and more chaotic detail had to be compressed.

Contents of this blog post were originally published: March 05, 2009