{picLeft, picRight, picTop, picBottom} = Si( 1, picURL = "
https://upload.wikimedia.org/wikipedia/commons/1/1e/Rooster_-_Public_Domain.jpg"; // Lisafern, CC0, via Wikimedia Commons --
https://commons.wikimedia.org/wiki/Special:ListFiles/lisafern {600000, 6000000, 35, 5};// , // picURL = "
https://upload.wikimedia.org/wikipedia/commons/3/3a/Foster_graph_hamiltonian.png"; // domaine public :
https://commons.wikimedia.org/wiki/File:Foster_graph_hamiltonian.png {450000, 10000000, 32, 8};// ); gTempFiles = Convert File Path( "$temp\pil_webp\", "windows" ); Delete Directory( gTempFiles ); // attention ! Create Directory( gTempFiles ); // Le répertoire existe et est vide // Charger Python et s'assurer que PIL est disponible... rc = Python Execute( {}, {}, "\[from PIL import Image, ImageSequence import numpy as np import os import time def rmse(imageA, imageB): if imageA.size != imageB.size: raise ValueError("Les images doivent avoir les mêmes dimensions pour calculer l'erreur quadratique moyenne") imgA_np = np.asarray(imageA).astype("float") imgB_np = np.asarray(imageB).astype("float") err = np.sum((imgA_np - imgB_np) ** 2) err /= float(imageA.size[0] * imageA.size[1]) return np.sqrt(err) ]\", echo( 0 ) ); Si (rc != 0, Throw("erreur d'initialisation Python rc=" || Char(rc)), "ok"); // ajoute une image JSL à une liste de fichiers pour .webp, .jpg et .gif addNewFrame = Function({filename, img}, img << saveimage( gTempFiles || filename || ".png", "png"); // le png est sans perte et le rmse l'utilise pour la comparaison File Size( gTempFiles || filename || ".png");); // les fonctions saveFrameTo... et saveFramesTo... // reflètent la façon dont les formats sont généralement utilisés ; // vous ne feriez normalement pas un GIF à image unique // et vous ne pouvez pas vraiment faire un JPG multi-images. saveFrameToWebP = Function( {outputfilename, qual}, {filelist, webpname, rc}, // filelist = Transform Each( {f}, Files In Directory( gTempFiles ), // Convert File Path( gTempFiles || f, "windows" );// ); If( N Items( filelist ) != 1, // Throw( "saveFrameToWebP nitems=" || Char( N Items( filelist ) ) );// ); webpname = Convert File Path( outputfilename, "windows" ); rc = Python Execute( {filelist, webpname, qual}, // inputs {_rmse, _ossize, _timew, _timer}, // outputs "\[#
https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#webp imgs = [] for f in filelist: imgs.append(Image.open(f).convert('RGB')) img=imgs[0] _timew = time.perf_counter() img.save(webpname,save_all=True,format="WEBP", minimize_size=True, lossless=False,quality=int(qual)) _timew = time.perf_counter() - _timew _timer = time.perf_counter() ir = Image.open(webpname).convert('RGB') _timer = time.perf_counter() - _timer _rmse = rmse(img,ir) _ossize = os.path.getsize(webpname) imgs = None ]\", echo( 0 ) ); If( rc != 0, // Throw( "erreur saveFrameToWebP rc=" || Char( rc ) );// , // Eval List( {_rmse, _ossize, _timew, _timer} );// valeur de retour ); ); saveFrameToPng = Function( {outputfilename, qual}, {filelist, pngname, rc}, // filelist = Transform Each( {f}, Files In Directory( gTempFiles ), // Convert File Path( gTempFiles || f, "windows" );// ); If( N Items( filelist ) != 1, // Throw( "saveFrameToPng nitems=" || Char( N Items( filelist ) ) );// ); pngname = Convert File Path( outputfilename, "windows" ); rc = Python Execute( {filelist, pngname, qual}, // inputs {_rmse, _ossize, _timew, _timer}, // outputs "\[# imgs = [] for f in filelist: imgs.append(Image.open(f).convert('RGB')) img=imgs[0] _timew = time.perf_counter() img.save(pngname, save_all=True, format="PNG", optimize=True, quality=int(qual)) _timew = time.perf_counter() - _timew _timer = time.perf_counter() ir = Image.open(pngname).convert('RGB') _timer = time.perf_counter() - _timer _rmse = rmse(img, ir) _ossize = os.path.getsize(pngname) imgs = None ]\", echo( 0 ) ); If( rc != 0, // Throw( "erreur saveFrameToWebP rc=" || Char( rc ) );// , // Eval List( {_rmse, _ossize, _timew, _timer} );// valeur de retour ); ); saveFrameToJPG = Function( {outputfilename, qual}, {filelist, jpgname, rc}, // filelist = Transform Each( {f}, Files In Directory( gTempFiles ), // Convert File Path( gTempFiles || f, "windows" );// ); If( N Items( filelist ) != 1, // Throw( "saveFrameToJPG nitems=" || Char( N Items( filelist ) ) );// ); jpgname = Convert File Path( outputfilename, "windows" ); rc = Python Execute( {filelist, jpgname, qual}, // inputs {_rmse, _ossize, _timew, _timer}, // outputs "\[#
https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#webp imgs = [] for f in filelist: imgs.append(Image.open(f).convert('RGB')) img=imgs[0] _timew = time.perf_counter() img.save(jpgname,format="JPEG", optimize=True, quality=int(qual)) _timew = time.perf_counter() - _timew _timer = time.perf_counter() ir = Image.open(jpgname).convert('RGB') _timer = time.perf_counter() - _timer _rmse = rmse(img,ir) _ossize = os.path.getsize(jpgname) imgs = None ]\", echo( 0 ) ); If( rc != 0, // Throw( "erreur saveFrameToJPG rc=" || Char( rc ) );// , // Eval List( {_rmse, _ossize, _timew, _timer} );// valeur de retour ); ); // Lisafern, CC0, via Wikimedia Commons --
https://commons.wikimedia.org/wiki/Special:ListFiles/lisafern img = New Image( picURL ); start = HP Time(); pngSize1 = addNewFrame( "1jsl.png", img ); JSLpngTime = (HP Time() - start) / 1e6; dt = New Table( // New Column( "extension", "character" ), New Column( "size" ), New Column( "timewrite" ), New Column( "timeread" ), New Column( "rmse" ) ); dt << addrows( 1 ); extension = "JSLPng"; size = pngSize1; rmse = 0; timewrite = JSLpngTime; dt << addrows( 1 ); extension = "PILPng"; {rmse, size, timewrite, timeread} = saveFrameToPng( "$temp/1.png", 95 ); For( i = 0, i <= 100, i += 1, dt << addrows( 1 ); extension = "WebP"; {rmse, size, timewrite, timeread} = saveFrameToWebP( "$temp/1.webp", i ); Wait( .01 ); dt << addrows( 1 ); extension = "Jpg"; {rmse, size, timewrite, timeread} = saveFrameToJpg( "$temp/1.jpg", i ); Wait( .01 ); ); dt << Graph Builder( Size( 996, 713 ), Show Control Panel( 0 ), Legend Position( "Inside Floating" ), Variables( X( :size ), Y( :rmse ), Overlay( :extension ), Size( :timewrite ) ), Elements( Points( X, Y, Legend( 47 ) ) ), SendToReport( Dispatch( {}, "size", ScaleBox, {Scale( "Log" ), Format( "Engineering SI", 12 ), Min( 1e4 ), Max( 6.1e6 ), Minor Ticks( 1 ), Label Row( {Show Major Grid( 1 ), Show Minor Grid( 1 ), Show Minor Labels( 1 )} )} ), Dispatch( {}, "rmse", ScaleBox, {Min( -0.5 ), Max( 32 ), Inc( 5 ), Minor Ticks( 0 )} ), Dispatch( {}, "400", ScaleBox, {Legend Model( 47, Type Properties( 0, "Marker", {Marker( "FilledCircle" )} ), Properties( 1, {Line Color( 3 ), Marker( "FilledCircle" )}, Item ID( "Jpg", 1 ) ), Properties( 2, {Line Color( 5 ), Marker( "FilledCircle" )}, Item ID( "JSLPng", 1 ) ), Properties( 3, {Line Color( 6 ), Marker( "FilledCircle" )}, Item ID( "PILPng", 1 ) ), Properties( 4, {Line Color( 8 ), Marker( "FilledCircle" )}, Item ID( "WebP", 1 ) ), Properties( -1, {Marker( "FilledSquare" )}, Item ID( "Png", 1 ) ) )} ), Dispatch( {}, "titre du graphique", TextEditBox, {Set Text( "WebP vs Jpg vs Png" )} ), Dispatch( {}, "taille", TextEditBox, {Set Text( "Taille sur disque, octets" )} ), Dispatch( {}, "rmse", TextEditBox, {Set Text( "RMSE sur 255" )} ), Dispatch( {}, "Constructeur de graphique", FrameBox, {Mode de dessin des marqueurs( "Normal" ), Ordre des lignes de référence( 6 ), Ajouter une image( 2, // calque 2 au-dessus des lignes de la grille image( Nouvelle image( picURL ) ), Limites( Gauche( picLeft ), Droite( picRight ), Haut( picTop ), Bas( picBottom ) ) ), Ajouter un script graphique( 7, Description( "" ), Empty() )} ), Dispatch( {}, "400", LegendBox, {Position de la légende( {47, [4, 0, 3, 2, 1]} )} ) ) ); dt << Graph Builder( Size( 638, 370 ), Show Control Panel( 0 ), Variables( X( :size ), Y( :timeread ), Overlay( :extension ) ), Elements( Points( X, Y, Legend( 9 ) ) ), SendToReport( Dispatch( {}, "400", ScaleBox, {Legend Model( 9, Properties( 0, {Line Color( 3 )}, Item ID( "Jpg", 1 ) ), Properties( 1, {Line Color( 5 )}, Item ID( "JSLPng", 1 ) ), Properties( 2, {Line Color( 6 )}, Item ID( "PILPng", 1 ) ), Properties( 3, {Line Color( 8 )}, Item ID( "WebP", 1 ) ) )} ), Dispatch( {}, "400", LegendBox, {Legend Position( {9, [0, 3, 2, 1]} )} ) ) );