Converting TIFFs with old-style JPEG compression

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting TIFFs with old-style JPEG compression

John Brown

I have a set of multi-page TIFFs. Each file may contain monochrome
*and* colour pages. The colour pages are unfortunalely using
old-style JPEG compresssion. I need to convert the files so that the
monochrome pages remain as Group IV and the nasty JPEGs are
converted to good JPEGs.

tiffcp and tiffsplit both fail when they encounter old-style JPEGs so
the best that I can come up with so far is:

for each multi-page TIFF x.tif
    tiffinfo x.tif | grep "Compression Scheme:" > compression.txt
    uncompress x.tif using tiffcp -c none
    tiffsplit uncompressed.tif ~tmp
    for each uncompressed page ~tmp*.tif
        if compression was old-style JPEG
            compress using new-style JPEG
        else
            compress using original scheme
        end if    
       concatenate compressed pages using tiffcp
       rm ~tmp*.tif
    end for
end for

Is there an easier way, such as a simple modification to tiffcp?

Regards,
John Brown.
       
_______________________________________________
Tiff mailing list: [hidden email]
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
Reply | Threaded
Open this post in threaded view
|

Re: Converting TIFFs with old-style JPEG compression

Ewan Christie
Hi


Not sure if this helps, but I had a problem that required extracting old style jpeg from multipage TIF files.  I found I could just rip the JPEG data right out. I make no excuses for my code :)

I guess then you can convert to what ever format you want.  Uses python.


import media
from PIL import Image
import sys
import os.path
from sys import argv

# remove_header('xaba.tif')

def bytes_from_file(filename, chunksize=8192):
    with open(filename, "rb") as f:
        while True:
            chunk = f.read(chunksize)
            if chunk:
                for b in chunk:
                    yield b
            else:
                break

# example:

prevb = 0
prevprevb = 0
pagecount = 0
copyflag = 0
for b in bytes_from_file('00000017.TIF'):
    # do_stuff_with(b)

    if (copyflag == 1 ):
        new_path = '%s.jpg' % pagecount
       
        test = b'\xFF'
        test += b'\xD8'
        test += b'\xFF'
        fh = open(new_path, 'ab')
        fh.write(test)
        copyflag = 2

    if (copyflag == 2 ):
        #with open(new_path, 'wb') as output_file:
        fh.write(b)

    if ( prevprevb == 255 ):
        if ( prevb == 216 ):
            if ( ord(b) == 255 ):
                print "string is FF D8 FF - start"
                pagecount = pagecount + 1
                copyflag = 1
   
    if ( prevb == 255 ):
        if ( ord(b) == 217 ):
            print "string is FF D9 - END"
            test = b'\xFF'
            test += b'\xD9'
            #with open(new_path, 'wb') as output_file:
            fh.write(test)
            fh.close()
            copyflag = 0
   
    prevprevb = prevb
    prevb = ord(b)


#        prevrat = imgdata[previdx][4]
#        prevpic = Image.




--
Ewan Christie

----- Original Message -----
> From: "John Brown" <[hidden email]>
> To: [hidden email]
> Sent: Thursday, 6 October, 2016 13:54:19
> Subject: [Tiff] Converting TIFFs with old-style JPEG compression

> I have a set of multi-page TIFFs. Each file may contain monochrome
> *and* colour pages. The colour pages are unfortunalely using
> old-style JPEG compresssion. I need to convert the files so that the
> monochrome pages remain as Group IV and the nasty JPEGs are
> converted to good JPEGs.
>
> tiffcp and tiffsplit both fail when they encounter old-style JPEGs so
> the best that I can come up with so far is:
>
> for each multi-page TIFF x.tif
>    tiffinfo x.tif | grep "Compression Scheme:" > compression.txt
>    uncompress x.tif using tiffcp -c none
>    tiffsplit uncompressed.tif ~tmp
>    for each uncompressed page ~tmp*.tif
>        if compression was old-style JPEG
>            compress using new-style JPEG
>        else
>            compress using original scheme
>        end if
>       concatenate compressed pages using tiffcp
>       rm ~tmp*.tif
>    end for
> end for
>
> Is there an easier way, such as a simple modification to tiffcp?
>
> Regards,
> John Brown.
>        
> _______________________________________________
> Tiff mailing list: [hidden email]
> http://lists.maptools.org/mailman/listinfo/tiff
> http://www.remotesensing.org/libtiff/
_______________________________________________
Tiff mailing list: [hidden email]
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
Reply | Threaded
Open this post in threaded view
|

Re: Converting TIFFs with old-style JPEG compression

John Brown
In reply to this post by John Brown
Bob Friesenhahn wrote:

> I am able to reproduce the problem using GraphicsMagick and have been
> able to find a simple solution for it.
>
> There is a fix in GraphicsMagick Mercurial and it will be in the next
> source snapshot on the ftp site.

Hello Bob,

I built the Mercurial source and it works as advertised. Thanks.

Regards,
John Brown.
_______________________________________________
Tiff mailing list: [hidden email]
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
Reply | Threaded
Open this post in threaded view
|

Re: Converting TIFFs with old-style JPEG compression

John Brown
In reply to this post by John Brown
Bob wrote:

> I am able to reproduce the problem using GraphicsMagick and have been
> able to find a simple solution for it.
>
> There is a fix in GraphicsMagick Mercurial and it will be in the next
> source snapshot on the ftp site.

I don't know what happened to my message, but I meant to write that
I built GraphicsMagick from Mercurial and it worked as advertised. Thanks.

Regards,
John Brown.


_______________________________________________
Tiff mailing list: [hidden email]
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/