2016.10.06 12:54 "[Tiff] Converting TIFFs with old-style JPEG compression", by John Brown

2016.10.06 13:13 "Re: [Tiff] Converting TIFFs with old-style JPEG compression", by 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" <johnbrown105@hotmail.com>
> To: tiff@lists.maptools.org
> 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?