Python saving multiframe TIFF as multiple JPEG - python

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.

Related

How to get jpeg image from a tiff file?

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.

how to convert pdf to image and return back to pdf

i am trying to convert pdf to image need to some manipulation on image and again need to convert back manipulated file to pdf using python.
I have tried to convert pdf to image but i don't need to save file in local instead of this need to manipulate on the image file and again need to convert back to pdf file.
# import module
from pdf2image import convert_from_path
# Store Pdf with convert_from_path function
images = convert_from_path('example.pdf')
for i in range(len(images)):
# Save pages as images in the pdf
images[i].save('page'+ str(i) +'.jpg', 'JPEG')
// here it saving locally but i need to apply some background change like operation again i need to convert back it to pdf.

How do I save particular frames of video into folders using python?

I have got a code that does motion detection. It takes video as an input and then when an object comes to the scene, background changes and code publishes "object is seen" as text on that detected frame.
My question is that how do I save frames "when objects are seen" into folders( both for colorful and gray images)? I understand that I can write that frames using " cv2.imwrite("frame%d.jpg" % count, resizedFrame)" but unfortunately it does not work.
How do I save those detected frames to "colorful" and "gray" folders?
You have to read the image first by cv2.imread() and then cv2.imwrite()
here is a small example
import
cv2
# read image as grey scale
grey_img=cv2.imread('/home/img/python.png',cv2.IMREAD_GRAYSCALE)
# save image
status=cv2.imwrite('/home/img/python_grey.png',grey_img)
print("Image written to file-system : ",status)
If you're don't have the folder path then use this
status=cv2.imwrite('python_grey.png',grey_img)
it will save the photo in default folder in which you save your .py file
if you want to save different images here is the code
import cv2
import time
# for number of images you need different names so set name automatically
name=str(time.time())+str(.png))
# read image as grey scale
grey_img=cv2.imread('/home/img/python.png',cv2.IMREAD_GRAYSCALE)
# save image
status=cv2.imwrite('%s'%name,grey_img)
print("Image written to file-system : ",status)

Manipulate a tiff image file containing a Z stack with PIL

I have an RGB tiff file containing a Z stack of 44 images. I want to open the file, convert it to grayscale, and save the resulting image as a new file.
The code I have right now is:
from PIL import Image
myimg = Image.open('1R - C1.tif')
myimg = myimg.convert('L')
myimg.save('1R - C1 BW.tif')
When I open the saved file, it contains only the first one of the 44 images in the stack. How do I manipulate and save the entire stack?

Django: Download Image from URL, Resize it, append "small" to the end of the filename

I'm looking for a way to download an 640x640 image from a URL, resize the image to 180x180 and append the word small to the end of the resized image filename.
For example, the image is located at this link
http://0height.com/wp-content/uploads/2013/07/18-japanese-food-instagram-1.jpg
Once resized, I would like to append the world small to the end of the filename like so:
18-japanese-food-instagram-1small.jpeg
How can this be done? Also will the downloaded image be saved to memory or will it save to the actual drive? If it does save to the drive, is it possible to delete the original image and keep the resized version?
Why don't you try urllib?
import urllib
urllib.urlretrieve("http://0height.com/wp-content/uploads/2013/07/18-japanese-food-instagram-1.jpg", "18-japanese-food-instagram-1.jpg")
Then, to resize this you can use PIL or another library
import Image
im1 = Image.open("18-japanese-food-instagram-1.jpg")
im_small = im1.resize((width, height), Image.ANTIALIAS)
im_small.save("18-japanese-food-instagram-1_small.jpg")
References:
http://www.daniweb.com/software-development/python/code/216637/resize-an-image-python
Downloading a picture via urllib and python

Categories