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?
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.
Related
My main aim is to produce 1D array out of each image in the 'dengue' folder. For which I used below code to read the file using both PIL and GLOB.
from PIL import Image
import glob
image_list = []
for filename in glob.glob('./dengue/*.tiff'):
im=Image.open(filename)
image_list.append(im)
OUTPUT IS -
UnidentifiedImageError: cannot identify image file './dengue/image_2016-09-18.tiff
How to resolve this? The same error showed up for numerous other images.
Or is there any other way I can read each of these tiff images to produce 1D array out of them? Thank you so much for your time.
Use one of the following tools to see the difference between a TIFF file you can read and one you cannot:
exiftool UNHAPPY.TIF
or, with tiffinfo from libtiff:
tiffinfo UNHAPPY.TIF
or, with ImageMagick:
magick identify -verbose UNHAPPY.TIF
My guess would be you have an unsupported compression or pixel type.
I want to open a .tif image but I always get error for every library I tried to use.
I tried with PIL:
from PIL import Image
img = Image.open('filepath/img_name.tif')
but I get the following error:
UnidentifiedImageError: cannot identify image file 'filepath/img_name.tif'
(This error does not mean that I can not find the file so the directory should be good)
I tried with tifffile:
import tifffile
img = tifffile.imread('filepath/img_name.tif')
I got the following error:
NotImplementedError: unpacking 14-bit integers to uint16 not supported.
I am pretty sure the problem is that the picture because I tried to open a tif image on the internet and it work just by doing this: this is the picture
from PIL import Image
im = Image.open('a_image.tif')
Is there a way to convert my 14-bit picture to a 16-bit picture?
(I know that I could multiply by 4 to get to 16-bit but I do not know how)
I installed imagedecodecs and tifffile has been able to open it
import tifffile
img = tifffile.imread(tif_name)
The problem was that my image was in 14bits.
Perhaps your TIF file has more than one frame. That could be a problem. Try:
from PIL import Image
image = Image.open("animation.tif")
image.seek(1) # skip to the second frame
try:
while 1:
image.seek(image.tell()+1)
# do something to im
except EOFError:
pass # end of sequence
From the documentation.
I currently have a .h5 file containing grayscale imagery. I need to convert it to a .jpg.
Does anybody have any experience with this?
Note: I could possible convert the h5 file to a numpy array and then use an external library like pypng to convert that to a png. But I am wondering if there is a more efficient way to convert to an image, and preferrably a .jpg.
Key ingredients:
h5py to read the h5 file.
Determine the format of your image and use PIL.
Let us suppose it's RGB format (https://support.hdfgroup.org/products/java/hdfview/UsersGuide/ug06imageview.html)
Suppose your image is located at Photos/Image 1 then you can do.
import h5py
import numpy as np
from PIL import Image
hdf = h5py.File("Sample.h5",'r')
array = hdf["Photos/Image 1"][:]
img = Image.fromarray(array.astype('uint8'), 'RGB')
img.save("yourimage.thumbnail", "JPEG")
img.show()
I have a TIFF file with multiple frames. I found that if I open the TIFF file and call the seek(1) I can seek to the second frame. This is working, except when I go to save the image as a jpg, it only saves the first frame and not my current frame.
How can I save multiple frames to different JPG files?
from PIL import Image
im = Image.open('test.tiff')
im.save('test.jpeg')
I need to do something like...
from PIL import Image
im = Image.open('test.tiff')
im.seek(1)
im.save('test.jpeg')
and have it save the second frame and not the first.
resized_image = Image.resize((100,200));
Image is Python-Pillow Image class, and i've used the resize function to resize the original image,
How do i find the new file-size (in bytes) of the resized_image without having to save to disk and then reading it again
The file doesn't have to be written to disk. A file like object does the trick:
from io import BytesIO
# do something that defines `image`...
img_file = BytesIO()
image.save(img_file, 'png')
print(img_file.tell())
This prints the size in bytes of the image saved in PNG format without saving to disk.
You can't. PIL deals with image manipulations in memory. There's no way of knowing the size it will have on disk in a specific format.
You can save it to a temp file and read the size using os.stat('/tmp/tempfile.jpg').st_size