Jump to content

Multi-Page TIFF -> separate JPG files


paz
 Share

Recommended Posts

TIFF2JPGS.au3This is not a request for such a script, but a "proof of concept", for the ones interested.

Ok, ok. It's just a bunch of DLL calls. I admit lol!

Since I was not able to find a *simple* program to split the TIFF files into individual JPG images (that I can then use easily with AutoIT), I did some research for a DLL able to do that... I came across "The FreeImage Project", which gave me what I was looking for.

So here it is - a simple script to convert multi-page TIFF files into separate JPGs

I might have some issues / obvious errors in my DLLCalls that may cause memory leaks... I'm no expert in the subject unfortunately.

But it works for me!

Link to comment
Share on other sites

  • 3 years later...

TIFF2JPGS.au3This is not a request for such a script, but a "proof of concept", for the ones interested.

Ok, ok. It's just a bunch of DLL calls. I admit lol!

Since I was not able to find a *simple* program to split the TIFF files into individual JPG images (that I can then use easily with AutoIT), I did some research for a DLL able to do that... I came across "The FreeImage Project", which gave me what I was looking for.

So here it is - a simple script to convert multi-page TIFF files into separate JPGs

I might have some issues / obvious errors in my DLLCalls that may cause memory leaks... I'm no expert in the subject unfortunately.

But it works for me!

After I saw this example I´ve decided to change a litle here and add a litle more there and make a MultiTiff To PDF creator script.

I´ve changed a litle the Comment ... I hope you don´t mind

Who knows ... maybe we shall make a PDFCreator clone in AutoIt :huh2:

Just kiding ... Keep the good work ;)

#cs

TIFF2PDF
---------
Multi-Page TIFF to PDF file converter


THIS SCRIPT NEEDS THE FREEIMAGE LIBRARY DLL FILE (FreeImage.dll) IN THE SCRIPT DIRECTORY
AND ALSO THE PDFFORGE LYBRARY DLL FILE (pdfforge.dll) AND iText LIBRARY DLL (itextsharp.dll)
FreeImage library is at http://freeimage.sourceforge.net/download.html
PdfForge libraries are at http://pdfcreator.svn.sourceforge.net/viewvc/pdfcreator/tags/Version%201.2.1/PlugIns/pdfforge/
#ce

;Input file to split
$input = FileOpenDialog("Choose a TIFF file", @ScriptDir, "TIFF files (*.TIF)")


;Output file convention is simple: I remove the extension, append an underscore '_'.
;The loop (below) will further append the page number and add the ".jpg" extension.
;If I picked a file named "IMAGE.TIF", the output will be "IMAGE_1.JPG", "IMAGE_2.JPG" and so on.
$output = StringTrimRight($input,4) & "_"


;Filename of the FreeImage Dll.
$FI_DLL = @ScriptDir & "\FreeImage.dll"
;Create PDFForge.dll instance
$PdfForge = ObjCreate("pdfforge.pdf.pdf")


;Theses are constants used by FreeImage.
;I got the values from the header file (FreeImage.h) included with the FreeImage dll download.
;Global const $FIF_JPEG = 2
Global const $FIF_TIFF  = 18


;Some basic check... does the TIFF file exists?
if FileExists($input) then
    
    ;Yet another check - does the FreeImage DLL is there?
    if FileExists($FI_DLL) then
                
        ;At this point, I start the process!
        
        
        ;Opens the FreeImage DLL
        $FI = DllOpen($FI_DLL)

        ;Initialize the library (mandatory according to the documentation0
        DllCall($FI,"none","_FreeImage_Initialise@4","dword",1)

        ;Opens the input TIFF file.
        $TIFF_IMAGE = DllCall($FI,"long_ptr","_FreeImage_OpenMultiBitmap@24","dword", $FIF_TIFF, "str", $input, "dword", 0, "dword", 1, "dword", 0, "dword", 0)

        ;Gets the number of pages from the TIFF image
        $PAGE_COUNT = DllCall($FI, "int", "_FreeImage_GetPageCount@4", "long_ptr", $TIFF_IMAGE[0])

        ;Page index are 0-based. For a loop, I'll need to go from 0 to (PAGE_COUNT -1)
        $PAGE_COUNT = $PAGE_COUNT[0] -1
        
                ;Declare Image2PDF array
        Dim $imageFilenames[$PAGE_COUNT+1]

        ;The way FreeImage lib works, you need to "lock" a single page at a time to work with it.
        ;I understands that this will actually copy the page into a separate bitmap, so I can play with it
        ;In my case, I just want to save each page into an individual JPG file
        for $i = 0 to $PAGE_COUNT
            
            ;Get a page from the multi-page TIFF file
            $BITMAP = DllCall($FI, "long_ptr", "_FreeImage_LockPage@8", "long_ptr", $TIFF_IMAGE[0], "dword", $i)
            
            ;Save it as a JPG file
            $ret = DllCall($FI, "dword", "_FreeImage_Save@16","dword", $FIF_TIFF, "long_ptr", $BITMAP[0], "str", $i &".tiff", "dword", 0)

            ;Save the file´s names into array
            $imageFilenames[$i] = @ScriptDir &"\" &$i &".tiff"

            ;Unlock the page.
            $ret = DllCall($FI, "none", "_FreeImage_UnlockPage@12", "long_ptr", $TIFF_IMAGE[0], "long_ptr", $BITMAP[0], "dword", 0)
            
            ;Free the RAM? AutoIT tends to crash at the end if I do not do this (Can't tell - I'm no developper)
            $BITMAP = 0
        next
        
                ;Output the PDF file
        $PdfForge.Images2PDF_2($imageFilenames, StringTrimRight($input, 4) &".pdf", 1)
        
        ;MsgBox(64,"Info","Process is done." & @crlf & $input & " were splitted into " & $PAGE_COUNT+1 & " individual TIF files")
        
        MsgBox(64,"Info","Process is done." &StringTrimRight($input, 4) &".pdf has been created with " &$PAGE_COUNT+1 &" pages from " &StringTrimRight($input, 4) &" file")
        
        ;Close the TIFF file
        $ret = DllCall($FI,"long","_FreeImage_CloseMultiBitmap@8","long_ptr", $TIFF_IMAGE[0], "dword", 0)

        ;Again, if I do not reassign some variables like this, I usually have a crash when the program ends.
        $TIFF_IMAGE = 0

        ;This will "DeInitialize" the FreeImage lib
        ;I *think* this does more or less the same as DllClose(), but I can be wrong.
        DllCall($FI,"none","_FreeImage_DeInitialise@0")

    Else
        MsgBox(16,"FreeImage DLL not found","The FreeImage DLL was not found in the script directory." & @crlf & "(" & $FI_DLL & ")" & @crlf & @crlf & "You can get if from this website:" & @crlf & "http://freeimage.sourceforge.net/download.html")
    EndIf
Else
    MsgBox(16,"File not found","You should have a TIFF file named " & $input & " in this script directory. Check this and try again")
EndIf
Edited by realshyfox

Learn, learn and ... learn

Link to comment
Share on other sites

  • 2 years later...

I tried running your script. I got an error:

>"F:Program FilesAutoIt3SciTEAutoIt3WrapperAutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "F:AutoIt scriptsTIFF2PDF realshyfoxTIFF2PDF.au3" /UserParams    
+>10:57:09 Starting AutoIt3Wrapper v.2.1.0.8    Environment(Language:0409  Keyboard:00000409  OS:WIN_XP/Service Pack 3  CPU:X86 OS:X86)
>Running AU3Check (1.54.22.0)  from:F:Program FilesAutoIt3
+>10:57:09 AU3Check ended.rc:0
>Running:(3.3.8.1):F:Program FilesAutoIt3autoit3.exe "F:AutoIt scriptsTIFF2PDF realshyfoxTIFF2PDF.au3"    
F:AutoIt scriptsTIFF2PDF realshyfoxTIFF2PDF.au3 (85) : ==> Variable must be of type "Object".:
$PdfForge.Images2PDF_2($imageFilenames, StringTrimRight($input, 4) &".pdf", 1)
$PdfForge^ ERROR
->10:57:28 AutoIT3.exe ended.rc:1
>Exit code: 1    Time: 21.238
 

Suggestions?

Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

First suggestion, don't resurrect dead thread when the author(s) haven't been logged on in over a year. Second, have you downloaded all of the DLL files needed? Third are you running this as an x86 script or an x64 script and does the DLL support whichever version you're running?

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...