I have a large tiff file contains multiple JPEG image.When I want to get the image from tiff,it will take a lots of time because of decompression from jpeg to rgb. If i want to get the jpeg image without decompression, how should I do with the tiff. Can I parse the TIFF file to get some image data and directly generate a JPEG image?
Asked
Active
Viewed 228 times
1
-
1You tagged with `python` and `c`; which language do you intend? Or is it OK if there is some ready-made tool that does this (which would probably be off-topic on SO)? – Thomas Jun 09 '22 at 09:46
-
Does this answer your question? [converting tiff to jpeg in python](https://stackoverflow.com/questions/28870504/converting-tiff-to-jpeg-in-python) – fam Jun 09 '22 at 09:47
-
@fam That question does not include the constraint of not having to re-encode the JPEG (which, apart from the slower performance and higher memory use, might also degrade image quality). – Thomas Jun 09 '22 at 09:48
-
I am currently using LibTiff to process TIFF files. It has a python library to use libtiff or openslide. If possible, I hope I can use Python to process tiff. – simpia Jun 09 '22 at 09:50
-
1https://pypi.org/project/tifftools/ can probably be used to extract the raw JPEG data from the TIFF file. So that gets you halfway there. Not sure how to put those bytes into the JPEG container format though. – Thomas Jun 09 '22 at 09:57
-
1Try [opentile](https://github.com/imi-bigpicture/opentile), which allows "compressed tiles to be losslessly read from wsi tiffs using 2D coordinates". GDAL also tries lossless conversions if possible. JPEG in TIFF is not that trivial. TIFF can contain separately stored JPEG tables, images segmented into strips or tiles, lossless JPEG, CFA color space, 12-bit samples (color components), separate/planar samples, and JPEG compressed segments with dimensions > 65535. – cgohlke Jun 09 '22 at 21:47
1 Answers
0
This is the final implementation method with opentile:
from opentile import OpenTile
import os
import traceback
os.environ.setdefault('TURBOJPEG', 'C:/lib/')
try:
tiler = OpenTile.open('name.svs')
except:
traceback.print_exc()
tile_leve=tiler.levels
print("{}".format(len(tile_leve)))
s=tiler.get_level(0)
print(s.compression.split('.')[1])
print(s.photometric_interpretation)
print(s.samples_per_pixel)
print(s.pixel_spacing)
tile_size=str(s.tiled_size).split("x")
print(s.tile_size)
print(tile_size)
y={}
for i in range(int(tile_size[0])):
for j in range(int(tile_size[1])):
tile = tiler.get_tile(0,0,0, (i, j))
y[(i,j)]=tile
with open("im/im_{}_{}.jpg".format(i,j),"wb") as f:
f.write(tile)
Tifffile is also feasible.

simpia
- 65
- 7
-
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 04 '22 at 15:55