Jump to content
fenhanxue

GDI When I'm dealing with a lot of pictures, the program gets very slow

Recommended Posts

fenhanxue

I need convert png to jpg ,my code is as follow:

#PRE_UseX64=n
#include <GDIPlus.au3>

_GDIPlus_Startup()

$j = 100000;<<<<<<<<<<<<<<<<<<<<<<<<<  png needed to convert to jpg
Local $png_file[$j+1]
For $i = 1 To $j
    $png_file[$i] = @ScriptDir & '\'&$i'.png'
Next



For $i = 1 To $j
    $sFileName = $png_file[$i]
    $sFileName_save = @ScriptDir & '\' & $i & '.jpg'
    $hImage = _GDIPlus_ImageLoadFromFile($sFileName)

    Local $sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
    Local $tParams = _GDIPlus_ParamInit(1)
    Local $tData = DllStructCreate("int Quality")
    DllStructSetData($tData, "Quality", 100)
    _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, DllStructGetPtr($tData))
    
    _GDIPlus_ImageSaveToFileEx($hImage, $sFileName_save, $sCLSID, DllStructGetPtr($tParams))
    _GDIPlus_ImageDispose($hImage)
Next


_GDIPlus_Shutdown()

 

the number of png needed to convert is 100,000 

and i find the code runs very slowly ,

if anyone can help me ?

HOW to speed up ?

 

 

 

 

Share this post


Link to post
Share on other sites
BrewManNH

What exactly did you expect when using a scripting language to manipulate 100,000 pictures? How long is too long?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
UEZ

@fenhanxue

This should be a little bit faster but I agree with BrewManNH:

#include <GDIPlus.au3>

_GDIPlus_Startup()

$j = 100000;<<<<<<<<<<<<<<<<<<<<<<<<<  png needed to convert to jpg
Local $png_file[$j+1]
For $i = 1 To $j
    $png_file[$i] = @ScriptDir & '\'&$i'.png'
Next

Local $sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
Local $tParams = _GDIPlus_ParamInit(1)
Local $tData = DllStructCreate("int Quality")
DllStructSetData($tData, "Quality", 100)
_GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, DllStructGetPtr($tData))

For $i = 1 To $j
    $hImage = _GDIPlus_ImageLoadFromFile($png_file[$i])
    _GDIPlus_ImageSaveToFileEx($hImage, @ScriptDir & '\' & $i & '.jpg', $sCLSID, $tParams)
    _GDIPlus_ImageDispose($hImage)
Next


_GDIPlus_Shutdown()

 

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
fenhanxue

When the number of png files is large, the script gets slower and slower , for example:

 

the code costs about 100 seconds to  convert the first 10000 png files  ( 1.png  2.png .... 10000.png)

and then it will cost much more time than 100 seconds to convert the second 10000 png files (10001.png   10002.png  ...... 20000.png) 。。。

My English is not very good, what I want to express is as follow :

 

_GDIPlus_Startup()


Local $time_cost[1+10]

For $i = 1 To 10
    Local $t = TimerInit()
    _Convert_1000_png_files($i);
    $time_cost[$i] = TimerDiff($t);==============================
Next

#cs=============    here is the problem  i meet
    i fond 
    $time_cost[10] > $time_cost[9] > $time_cost[8] ..... $time_cost[1]
    
    
    in my test , 
    
    $time_cost[1]   =  about 5mins
    
    $time_cost[10] =  about  1hour

    $time_cost[10] is to much bigger than $time_cost[1]  
    
    this is not what i want 
    
    i want $time_cost[10] is  Approximately equal  to  $time_cost[1] 
    
    
    so  how to fix my code ?
#ce=============

_GDIPlus_Shutdown()


Func _Convert_10000_png_files($index);
    For $i = ($index-1)*10000 + 1  To $index * 10000
        $sFileName = @ScriptDir & '\' & $i & '.png'
        $sFileName_save = @ScriptDir & '\' & $i & '.jpg'
        $hImage = _GDIPlus_ImageLoadFromFile($sFileName)

        Local $sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
        Local $tParams = _GDIPlus_ParamInit(1)
        Local $tData = DllStructCreate("int Quality")
        DllStructSetData($tData, "Quality", 100)
        _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, DllStructGetPtr($tData))
        
        _GDIPlus_ImageSaveToFileEx($hImage, $sFileName_save, $sCLSID, DllStructGetPtr($tParams))
        _GDIPlus_ImageDispose($hImage)
    Next    
EndFunc

 

Share this post


Link to post
Share on other sites
fenhanxue

@UEZ

i wonder if my script have forgot any code to Release resources(Memory ? )

 

Share this post


Link to post
Share on other sites
fenhanxue

for example 

if the png number is 10000   ,    it takes about 5mins to convert 10000 png files to jpg  .

if the png number is 10 times (100000) , it will cost much more time than 50mins ( 5mins * 10  = 50 min )

why ?

 

Share this post


Link to post
Share on other sites
Jos

Please slow down a little and wait till somebody comes around that can and is willing to help you.
Your stated case is clear.

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
UEZ

@fenhanxue

Did you notice from my previous post what I did in the for/next loop? But there is not a release resource issue. I can imagine that the CPU is getting very hot and slow down your system and/or your HD performance is affected. You can check with the window resource monitor about bottle necks while your script runs.


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
fenhanxue
_GDIPlus_Startup()

    
    $iJPGQual = 100
    $sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
    $tParams = _GDIPlus_ParamInit(10)
    $tData = DllStructCreate("int Quality")
    DllStructSetData($tData, "Quality", $iJPGQual)
    $pData = DllStructGetPtr($tData)
    _GDIPlus_ParamAdd($tParams, $GDIP_EPGQUALITY, 1, $GDIP_EPTLONG, $pData)
    $pParams = DllStructGetPtr($tParams)
    
    
    For $i = 1 To 200000;=============20w 个 
        If Mod($i,1000) = 0 Then ConsoleWrite($i);每处理1000个,ConsoleWrite 一次,好对当前的进度有个大致了解
        
        $scaled = _GDIPlus_ImageLoadFromFile ( 'D:\20w个图\'&$i&'.png' )  
        _GDIPlus_ImageSaveToFileEx($scaled, 'D:\20w个PNG到JPG\'&$i&'.jpg', $sCLSID, $pParams)
        _GDIPlus_BitmapDispose($scaled)
        _GDIPlus_ImageDispose($scaled)
    Next
    

    _GDIPlus_Shutdown()

i tried the code above , it  still  runs  slowly.

it takes about 10 seconds to convert the first 1000 png 

however, it cost about 4 hours to convert all the 200000 png  

(10 seconds  *  200000png / 1000 png = 2000 seconds = 0.55 hours )

4 hours is too much bigger than 0.55 hours

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×