Jpeg Generation Loss Script

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

VinceLestrade47
Posts: 3
Joined: Sat Aug 13, 2016 4:01 pm

Jpeg Generation Loss Script

Post by VinceLestrade47 »

Hello!

I would like to make a video showing the generation loss of .jpeg file
due to rotation. The script will have to read an image, rotate it and save it
with a new name, then open the latest image and do the same again and
again for a specific amount of times. In the end the script will have created
files in the following form: image_00001, image_00002, etc...

Actually I found some code about it on Vimeo (link below) but I am not
familiar with the terminology and I am not sure if it's in JavaScript.

https://vimeo.com/3750507

Thanks for reading!

User avatar
Kukurykus
Posts: 494
Joined: Mon Jul 25, 2016 12:36 pm

Re: Jpeg Generation Loss Script

Post by Kukurykus »

I did the script that produces 360 .jpg files with the names you asked. Every next saved file is right rotated of 1 degree, but adjusted to size and form of original image. A video you can make by any other program using 360 or less (if you decrease number in a script) images you got. You may also change a quality by the script changing 0 up to 12 value. Make sure you created Rotation fodler on your desktop and dropped there 'start' file. A script you can run from Photoshop if you before copied it to Scripts folder of your Ps app., or launch by DRMB clicking .jsx file. There's also option in the script to change a degree value of rotation with full 360 degree rotation, simply change value of r variable.

The only thing I couldn't do the way you described (however it didn't make difference for the correct result) was saving rotated .jpg file after another opening and rotation becuase (by reason of photoshop bug) it would give unexpected result. I did it that original file is saved as new one each time rotation is made, but meantime original one is not closed, but used (for every next repeted process) instead of just saved .jpg's. So not another savement of same .jpg file makes its worse quiality, but another rotation - it's why a final result should meet your expections.

Additionally I attached to the post 6 .jpg files to show how quality was changing every 60 rotations (with preceding 59 of course).

ImageImageImageImageImageImage

Code: Select all

#target photoshop
bringToFront(), displayDialogs = DialogModes.NO

if ((sky = File(fle = '~/Desktop/Rotation/')).exists && sky = sky.getFiles(/.*\.jpg$/)[0]) {
open(sky), r = 1, w = (aD = activeDocument).width, h = aD.height
for(i = 0; i < (360 / r) + 1; i++) {
if (i == 100000) {alert('Limit of ' + i + ' images reached!'); break}
aD.rotateCanvas(r), aD.trim(), aHS = aD.activeHistoryState
aD.rotateCanvas(-(i + 1) * r), aD.resizeCanvas(w, h)
jpg = new JPEGSaveOptions(), jpg.quality = 0
aD.saveAs(File(fle + 'image_' + '0000'.slice(String(i).length - 1) + i + '.jpg'), jpg)
aD.activeHistoryState = aHS
}
aD.close(SaveOptions.DONOTSAVECHANGES)
}
else {alert('To start process you need only 1 .jpg file in: ' + fle + ' folder!')}
Attachments
Generation Loss.zip
(113.21 KiB) Downloaded 382 times

VinceLestrade47
Posts: 3
Joined: Sat Aug 13, 2016 4:01 pm

Re: Jpeg Generation Loss Script

Post by VinceLestrade47 »

Kukurykus, thanks for your effort.

I tried the script and it works fine, but I believe, and correct me if I am wrong,
that these artifacts are the result of rotating the image in a non-multiple of 90 rather than cause
of jpeg re-compression. Let me add that I find it very interesting nevertheless and I will make of
use of it too.

I tried the following:
I changed r to 90 and the 360 to 200000 and I ended up with about 2000 images and I compared the
last one to the first and I couldn't notice any difference. File size confirmed it also. One should expect
a slight increase in file size with every subsequent saving and I believe this is where the problem lies.

The script should close the image that just saved and open it anew. I think it has to do with Photoshop's
non-destructive workflow. Previously this didn't become evident because the script would change the orientation
to the initial one to facilitate the video creation. It doesn't have to correct the orientation of the image. If it rotates,
say 90 degrees, then I only have to keep every 4th image.

You mentioned that there was some bug? Is there a workaround?
Also, will these scripts work with future Photoshop releases?

User avatar
Kukurykus
Posts: 494
Joined: Mon Jul 25, 2016 12:36 pm

Re: Jpeg Generation Loss Script

Post by Kukurykus »

I made a limit of 100000 rotations, so your twice more input couldn't happen. It would give you rather alert 'Limit of 100000 images reached!' after 100000 images and process would stop. But you say you got about 2000 images rotated so maybe process didn't stop (?). 200000 / 90 gives 2222,(2) so on the condition you changed also limit of 100000 to higher that 2000 rotatated files were possible. I tested it before on less amount of images and process stopped. So it's strange...
The reason I limited loop to 100000 rotations was because you wanted image result name to be in 5 long number. If there was no limit then first and every next image after 100000 would be over-saved getting each time the same name: image_00000. If you want to process more files you need to increase limit to avoid breaking the loop and in the code-line where image is savedAs add one or more zeros to '0000'

EDIT: about that i, I was wrong, so above part is cancelled ;)

"...that these artifacts are the result of rotating the image in a non-multiple of 90 rather than cause of jpeg re-compression."
Yes they are like I mentioned previous post. Read further to get to know why...

"One should expect a slight increase in file size with every subsequent saving and I believe this is where the problem lies. "
My knowledge isn't so big, but I guess that even when quality decreses, every another save makes file size is bigger?

"The script should close the image that just saved and open it anew."
You are right and it's a way I tried to write this script. True is I tested few methods and all failed because of weird behaviour of Photoshop. So I thought hard and tricked Photoshop by method I provided. Yes I named it a bug, because I can't explain why after some part of rotations (no matter 1 or more degrees, rotation gets stuck for many photos to restore later and gets stuck to the end...). I tried to resolve it with transparent psd files, with more space around main image, with layer (not canvas) rotation and everytime saved .jpg was opened for another rotation the result was like I described. For some reason smooth rotation works and broken for reopening doesn't. Yes there is workaround, read below for more information.

"Previously this didn't become evident because the script would change the orientation to the initial one to facilitate the video creation. It doesn't have to correct the orientation of the image. If it rotates, say 90 degrees, then I only have to keep every 4th image."
When you compare another images with first their position won't be perfectly fit to original. I had to think up how to change orientation of images not rotated to right angle, so I straightened images they not be askew. Of course if they have to be rotated to right angle, so every 90 degrees then I can change the script in two essential parts. There won't be need to straighten them and there will be 99% chance they can be saved from just saved copy, so the way you wished.
"Also, will these scripts work with future Photoshop releases?"
I think it should though Photoshop changes with every version. It was made in CS, but because I used the easiest available method then CC should handle it too.



I'll test script with your directions to see myslef how it works and then rewrite it to meet them however I'm affraid that the last image will look alike first as there won't be warp effect what gives UN- 90, 180, 270 or 360 rotation. I just recalled myself that at the beggining I made 360 degree rotation of 600 images . Every next image was mirror-like of every previous.

User avatar
Kukurykus
Posts: 494
Joined: Mon Jul 25, 2016 12:36 pm

Re: Jpeg Generation Loss Script

Post by Kukurykus »

Well, I rewrote a script that it processed on repoened saved .jpg files with no adjustment and did it with r = 90 and 200000 instead of 360, but unfortunatelly you can sligtly sees the change. From about 15th they look worse but without change to over 2000 of them. As to size, it's with no change too. So I'm at least happy I could gain the same look as author of that script on video I watched. Frankly I don't see any code with rotation or something like it. He firmly got that result with only recompression. I can't even say is this feature possible by Photoshop with/out scripting...

Code: Select all

#target photoshop
bringToFront(), displayDialogs = DialogModes.NO, r = 90

if ((sky = File(fle = '~/Desktop/Rotation/')).exists && sky.getFiles(/.*\.jpg$/)[0]) {
for(i = 1; i < (200000 / r) + 1; i++) {
open(sky.getFiles(/.*\.jpg$/)[i - 1])
if (i == 100000) {alert('Limit of ' + i + ' images reached!'); break};
(aD = activeDocument).rotateCanvas(r), aD.trim(), jpg = new JPEGSaveOptions(), jpg.quality = 0
aD.saveAs(File(fle + 'image_' + '0000'.slice(String(i).length - 1) + i + '.jpg'), jpg)
aD.close(SaveOptions.DONOTSAVECHANGES)
}
}
else {alert('To start process you need only 1 .jpg file in: ' + fle + ' folder!')}

EDIT: On 119 page of https://www.adobe.com/content/dam/Adobe ... eGuide.pdf you find more options of saving jpeg files. I never used nothing than quality, but maybe by defaul recompression is prevented so images can't change their appearance. Use those properties a way I did with jpg.quality and maybe something from that will help...
Last edited by Kukurykus on Tue Aug 16, 2016 4:21 pm, edited 1 time in total.

VinceLestrade47
Posts: 3
Joined: Sat Aug 13, 2016 4:01 pm

Re: Jpeg Generation Loss Script

Post by VinceLestrade47 »

Hello again.

I tried the new script, but this time the images are all rotated at 90.
So if the initial image is this:
image_0001.jpg
image_0001.jpg (28.06 KiB) Viewed 8317 times
the images that the script creates all look like this:
image_0002.jpg
image_0002.jpg (28.3 KiB) Viewed 8317 times
I will explain what exactly I did when I started doing this manually.
First I had an image, say 0001, I made a copy of it, 0001 - Copy, which I then renamed to 0002 and then I opened the 0002 image in PS. I had created an action that would rotate the image 90 degrees clockwise, Save As 0 quality, and close. So I would play the action and then I would go to windows explorer, make a copy of 0002, (0002 - Copy), rename that copy to 0003, open it in PS, hit the action button, and you get the point...

Therefore my images in windows explorer were in the following pattern:
one with the original orientation
image_0001.jpg
image_0001.jpg (28.06 KiB) Viewed 8317 times
,
one at 90 clockwise
image_0002.jpg
image_0002.jpg (28.3 KiB) Viewed 8317 times
,
one upside-down (180)
image_0003.jpg
image_0003.jpg (28.5 KiB) Viewed 8317 times
,
one 90 anti-clockwise [wouldn't allow me to add a 4th image] and then again original orientation, etc ,etc...

You are right that it's the re-compression that induces the artifacts and that rotation is not really necessary.
I was influenced by the following: https://en.wikipedia.org/wiki/Generation_loss
It also confirms the file size increase.

In the end the problem is not the rotation but how to open each subsequent image from explorer with a script.

User avatar
Kukurykus
Posts: 494
Joined: Mon Jul 25, 2016 12:36 pm

Re: Jpeg Generation Loss Script

Post by Kukurykus »

I forgot to change part of code in last post in two lines from:

Code: Select all

   for(i = 0; i < (200000 / r) + 1; i++) {
open(sky.getFiles(/.*\.jpg$/)[i])
to:

Code: Select all

   for(i = 1; i < (200000 / r) + 1; i++) {
open(sky.getFiles(/.*\.jpg$/)[i - 1])
So i = 0 to i = 1 in first line and to .

This way a process won't stop after first roation and every next image get properly increased number in its name.

I read the rest of your post and with correction I made you don't need your action. It'll open every saved .jpg file, hoever you'll need another correction in the script if I have to comply with the theory I read in introduction of that Wikipedia article (plus that I undestood from your action and manual work point). It means I can remove rotation part from the script and replace that opening every next saved .jpg file (previously making copy of it) for another save.

Well that's easy to do (incl. or not rotation). I'm circious the result it'll give, but personally I think even 1000th copy will have appearance of first image (no matter size of it).


EDIT:

In the following code there is no rotation, but the loop of:
- copying original file with a new name
(just added 'c' after current number)
- removing a file it was copied from
- opening copied one in a Ps
- saving in the same folder

Unfortunatelly all the next files differes from the original one only a quality that was set up.

Code: Select all

#target photoshop
bringToFront(), displayDialogs = DialogModes.NO
function SKY() {return sky.getFiles(/.*\.jpg$/)}

if ((sky = File(fle = '~/Desktop/Rotation/')).exists && SKY()[0]) {
for(i = 1; i < 1000; i++) {
File(LST = (lst = SKY())[lst.length -1]).copy(String(LST).replace(/\./, 'c.')), File(LST).remove()
open(SKY()[i - 1]), jpg = new JPEGSaveOptions(), jpg.quality = 0;
(aD = activeDocument).saveAs(File(fle + 'image_' + '0000'.slice(String(i).length - 1) + i + '.jpg'), jpg)
aD.close(SaveOptions.DONOTSAVECHANGES)
}
}
else {alert('To start process you need only 1 .jpg file in: ' + fle + ' folder!')}
Attachments
Files Moving.zip
(8.44 KiB) Downloaded 366 times