Open Image preview

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: xbytor, DavideBarranca, Paul MR

luis miguel
Posts: 16
Joined: Thu Oct 27, 2016 9:56 am

Open Image preview

Postby luis miguel » Sun Dec 24, 2017 9:04 am

good morning
I have this script that allows me to select a folder
and see the previews of the files contained within

There are 2 problems that I would like to solve
the first problem and that sometimes I get this error
and I do not know why this happens

Image

The second is not a real problem
I would like to open the file displayed in the window in photoshop
then create a button to do this.

// SCRIPT

Code: Select all

function main(){
inputFolder = Folder.selectDialog("SELECT FOLDER IMAGE:");
var fileList = inputFolder.getFiles(/\.(jpg|png)$/i);
Image.prototype.onDraw = function(){



// "this" is the container; "this.image" is the graphic
if( !this.image ) return;
var WH = this.size,
wh = this.image.size,
k = Math.min(WH[0]/wh[0], WH[1]/wh[1]),xy;
// Resize proportionally:
wh = [k*wh[0],k*wh[1]];
// Center:
xy = [ (WH[0]-wh[0])/2, (WH[1]-wh[1])/2 ];
this.graphics.drawImage(this.image,xy[0],xy[1],wh[0],wh[1]);
WH = wh = xy = null;
}
var win = new Window ("dialog", "PREVIEW");
win.pnl1 = win.add('panel', undefined, undefined, {borderStyle:'black'});
win.pnl1.preferredSize=[400,400];
win.Im1 = win.pnl1.add ("image", undefined,fileList[0]);
win.Im1.size = [400,400];



win.pnl2 = win.add('panel', undefined, undefined, {borderStyle:'black'});
win.pnl2.orientation = "row";
win.pnl2.bu2 = win.pnl2.add('button',[110,10,200,30],'BACK');
win.pnl2.bu1 = win.pnl2.add('button',[10,10,100,30],'NEXT');


win.grp100 = win.add('group');
win.grp100.bu1 = win.grp100.add('button', undefined, 'CLOSE', { name: "cancel" });
var PIC = 0;
win.pnl2.bu1.onClick=function(){
//Next picture
if(PIC == fileList.length -1) return;
PIC++;
win.Im1.image = fileList[PIC];
}
win.pnl2.bu2.onClick=function(){
//Previous
if(PIC == 0 ) return;
PIC--;
win.Im1.image = fileList[PIC];
}
win.show();
}
main();

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

Re: Open Image preview

Postby Kukurykus » Mon Dec 25, 2017 3:40 am

That error occurs only when a file dimension is over 256 * 256 * 256 pixels so 16'777'216. To check is your file over this size, just multiply its pixel height and width. Anyway I added additional button to open images in Photoshop you asked, I tried also to open images by clicking at preview image, but unfortunatelly that works only for 'iconbutton' which can't be resized and centered in contrary to 'image'. Anyway you can see other script I fixed that should be useful for you, where instead of one image you have more (image) icons of the same size, however if they have to be displayed properly there is need to resize them earlier in Photoshop: https://www.ps-scripts.com/viewtopic.php?f=67&t=24398

Code: Select all

function brwsr() {
u = undefined, Image.prototype.onDraw = function() {
WH = [(k = Math.min(wh[0] / (WH = this.image.size)[0], wh[1] / WH[1])) * WH[0], k * WH[1]]
xy = [(wh[0] - WH[0]) / 2, (wh[1] - WH[1]) / 2], this.graphics.drawImage(this.image, xy[0], xy[1], WH[0], WH[1])
};

wh=(upr=(win=new Window('dialog', 'PREVIEW')).add('panel', u, u, {borderStyle:'black'})).preferredSize=[400, 400];
(img = upr.add('image', u, fL[0])).size = wh; (btm = win.add('panel', u, u, {borderStyle:'black'})).orientation='row'

p = 0; function IF(v) {return v ? 'if (p) img.image = fL[--p]' : 'if (fL[p + 1]) img.image = fL[++p]'}
obj = {CLOSE: 'win.close()', BACK: IF(true), NEXT: IF(false), OPEN: 'open(File(fL[p])), refresh()'}

for(i in obj) eval("btm.add('button', [107, 10, 200, 33], i).onClick = function() {" + obj[i] +"}"); win.show()
}

if ((fL = Folder.selectDialog('SELECT IMAGE\'S FOLDER:').getFiles(/\.(jpg|png)$/i)).length) brwsr()
Attachments
Image Browser.rar
(862 Bytes) Downloaded 118 times

luis miguel
Posts: 16
Joined: Thu Oct 27, 2016 9:56 am

Re: Open Image preview

Postby luis miguel » Mon Dec 25, 2017 10:08 am

Kukurykus
Thank you for your very interesting script
unfortunately I use images of nikon d810 and canon 5ds
which go beyond 256 * 256 * 256 pixels then 16'777'216

if I resize the images first, it's not good

I do not know if you can change this way:

I in the same folder I have jpg and nef files

i jpg resize them so the panel does not create error
but when I click on open
opens the nef file that has the same jpg file number

this would be perfect for me.

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

Re: Open Image preview

Postby Kukurykus » Tue Dec 26, 2017 12:18 am

I changed previous version of 'Image Browser' script that you can have a preview of .jpg files equel or larger than 256 x 256 x 256 pixels! This vers. doesn't support .png though (perhaps next one). When you select a folder with .jpg files and dimension of first file will be beyond 16'777'215 px, a 'tmp' subfolder will be created in your .jpg files folder. Then file will be opened and resized to maximal dimension that is not higher than result of 256 base with exponent of 3 and decreased of 1. So if there was square document (same width & height) with minimum not allowed dimension so 4096 * 4096 it will be changed to 4095 * 4095 (16'769'025). if that was rectangle for ex. 5000 * 8000, so 40'000'000 then new dimension will be 3238 * 5181 (16'776'078). Other words saying, each time the dimension is higher than maximally allowed (16'777'215) image will be resized to the closest dimension but without crossing the limit; and always with maintained aspect ratio! After resizing, an image will be saved in 'tmp' subfolder and its original path (parent folder) replaced with new one. Every next time the same picture will be displayed program will be using new path instead of loading, resazing and saving image again. If 'OPEN' button will be clicked, script will be using a path from original array of images in parent folder. That means you keep your originals in their shape. When finally 'CLOSE' button is clicked 'tmp' subfolder will be deleted from disk with all content.

VERSION II:

Code: Select all

function brwsr() {
Image.prototype.onDraw = function() {
xy = this.image.size, m = Math.min(wh[0] / xy[0], wh[1] / xy[1]), WH = [m * xy[0], m * xy[1]]
function dis(v, v1, v2) {return (v1[v] - v2[v]) / 2} xy = [dis(0, wh, WH), dis(1, wh, WH)]
this.graphics.drawImage(this.image, xy[0], xy[1], WH[0], WH[1])
}

prv = "'Photoshop Image Preview' by Marc Autret, Paul Riggott && Marek Omszański:" /*Kukurykus*/
wh=(upr=(win=new Window('dialog', prv)).add('panel', u, u, {borderStyle:'black'})).preferredSize=[400, 400];
(img = upr.add('image', u, fL[0])).size = wh; (btm = win.add('panel', u, u, {borderStyle:'black'})).orientation='row'

p = 0, cls = 'while((fls = fld.getFiles()).length) fls[fls.length - 1].remove(); fld.remove()'
function IF(v) {return v ? 'if (p) img.image = fL[--p]' : 'if (fL[p + 1]) xmp(++p), img.image = fL[p]'}
obj = {CLOSE: cls + ', win.close()', BACK: IF(true), NEXT: IF(false), OPEN: 'open(File(ofL[p])), refresh()'}
for(i in obj) eval("btm.add('button', [107, 10, 200, 33], i).onClick = function() {" + obj[i] +"}"); win.show()
}

$.level = 0; try {
if ((fL = Array().slice.call(ofL = Folder.selectDialog('SELECT IMAGE\'S FOLDER:').getFiles(/\.jpg$/i))).length) {
function xmp(v) {
if (!ExternalObject.AdobeXMPScript) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript')
XMP = new XMPFile(fL[v].fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ).getXMP()
function XY(v) {return XMP.getProperty(XMPConst.NS_EXIF, 'Pixel' + v + 'Dimension')}

if ((x = XY('X')) * (y = XY('Y')) > max = Math.pow(256, 3) - 1) {
sqrt = Math.sqrt(max / (div = Math.max(x, y) / min = Math.min(x, y)))
function mm(v) {return Math.floor((v == min ? 1 : div) * sqrt)}
if (!(fld = Folder((pth = ofL[v].path + '/tmp'))).exists) fld.create();
(jpg = new JPEGSaveOptions()).quality = 12, open(File(ofL[v])),
(aD = activeDocument).resizeImage(mm(x), mm(y))
aD.saveAs((fle = File(pth + '/' + ofL[v].name)), jpg)
fL.splice(v, 1, fle), dnsc = 'DONOTSAVECHANGES'
aD.close(eval('SaveOptions.' + dnsc))
}
}
fld = Folder(), u = undefined, xmp(0), brwsr()
}
}
catch(err){}
Ps. I noticed there is problem with images enlarged manually to over 17666215px(just for test). If additionally they got ninety degrees rotated but outside of Ps and not saved then XMP still saw their previous width and height, so when they were opened as preview they got deformed (width to height and height to width). However even when enlarged but not rotated then dimension information left unchanged. Sometimes when outside of Photoshop rotated images are opened in Ps and then oversaved some images are displayed properly while others not. When dimension of images is over 17666215 pix it seems there is need script to check their height and width while opening them in Ps. Not by XMP, but directly by Photoshop. I just noticed it and didn't try it much, but after all it shouldn't be problem as noone will change "fresh" images dimension before previewing, while sole rotation without changing size rather doesn't impact on their proper displaying. Still it's something to fix...

Okey I tested it today and that I wrote higher is not completetly true. Now I know for sure( ? ) preview will display original orientation of image as far as that wasn't rotated and saved (no matter it's under/over16777215 px dimension). If that was rotated but unsaved then orienation dimension will be applied to its current (seen visually in system) orientation dimension. I have no idea how to determine image was rotated with no saving. If someone can help please post it so I update current code.
Attachments
Image Browser.rar
(1.45 KiB) Downloaded 115 times

luis miguel
Posts: 16
Joined: Thu Oct 27, 2016 9:56 am

Re: Open Image preview

Postby luis miguel » Tue Dec 26, 2017 4:29 pm

Kukurykus

This is a bit better, but I'm having problems with vertical photos
another question
I would like to see the file name in the ui window if possible

thanks again for your help


Return to “Photoshop Scripting - General Discussion”

Who is online

Users browsing this forum: No registered users and 5 guests