building tiff stack with wand - python

How can I achieve this with Wand library for python:
convert *.png stack_of_multiple_pngs.tiff
?
In particular, how can I read every png image, pack them into a sequence and then save the image as tiff stack:
with Image(filename='*.tiff') as img:
img.save(filename='stack_of_multiple_pngs.tiff')
I understand how to do it for gifs though, i.e. as described in docs. But what about building a sequence as a list and appending every new image I read as a SingleImage()?
Having trouble figuring it out right now.
See also

With wand you would use Image.sequence, not a wildcard filename *.
from wand.image import Image
from glob import glob
# Get list of all images filenames to include
image_names = glob('*.tiff')
# Create new Image, and extend sequence
with Image() as img:
img.sequence.extend( [ Image(filename=f) for f in image_names ] )
img.save(filename='stack_of_multiple_pngs.tiff')
The sequence_test.py file under the test directory will have better examples of working with the image sequence.

Related

Issue in producing arrays out of tiff images

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.

How to convert the pdf file to jpeg images

Here is my program, I want to convert pdf file into jpeg images, I wrote below code I am getting the PIL.PpmImagePlugin object how can I convert to jpeg format can you please help me. Thank you in advance.
from pdf2image import convert_from_path
images = convert_from_path('/home/cioc/Desktop/testingFiles/pdfurl-guide.pdf')
print images
You can add an output path and an output format for the images. Each page of your pdf will be saved in that directory in the specified format.
Add these keyword arguments to your code.
images = convert_from_path(
'/home/cioc/Desktop/testingFiles/pdfurl-guide.pdf',
output_folder='img',
fmt='jpeg'
)
This will create a directory named img and save each page of your pdf as a jpeg image inside img/
Alternatively, you can save each page using a loop by calling save() on each image.
from pdf2image import convert_from_path
images = convert_from_path('/home/cioc/Desktop/testingFiles/pdfurl-guide.pdf')
for page_no, image in enumerate(images):
image.save(f'page-{page_no}.jpeg')
You could use pdf2image parameter fmt='jpeg' to make it return JPEG instead.
You can also just manipulate the PPM as a you would a normal JPEG as this is only the backend file type. If you do Image.save('path.jpg') it will save it as a JPEG.

Merge multiple base64 images into one

If I have multiple base64 strings that are images (one string = one image). Is there a way to combine them and decode to a single image file? i.e. from multiple base64 strings, merge and output a single image file.
I'm not sure how I would approach this using Pillow (or if I even need it).
Further clarification:
The source images are TIFFs that are encoded into base64
When I say "merge", I mean turning multiple images into a multi-page image like you see in a multi-page PDF
I dug through the Pillow documentation (v5.3) and found something that seems to work. Basically, there are two phases to this:
Save encoded base64 strings as TIF
Append them together and save to disk
Example using Python 3.7:
from PIL import Image
import io
import base64
base64_images = ["asdfasdg...", "asdfsdafas..."]
image_files = []
for base64_string in base64_images:
buffer = io.BytesIO(base64.b64decode(base64_string))
image_file = Image.open(buffer)
image_files.append(image_file)
combined_image = images_files[0].save(
'output.tiff',
save_all=True,
append_images=image_files[1:]
)
In the above code, I first create PIL Image objects from a bytes buffers in order to do this whole thing in-memory but you can probably use .save() and create a bunch of tempfiles instead if I/O isn't a concern.
Once I have all the PIL Image objects, I choose the first image (assuming they were in desired order in base64_images list) and append the rest of the images with append_images flag. The resulting image has all the frames in one output file.
I assume this pattern is extensible to any image format that supports the save_all and append_images keyword arguments. The Pillow documentation should let you know if it is supported.

Get list of result files converted via Wand

Imagemagick is open source software suite for displaying, converting, and editing raster image files. Wand is a ctypes-based ImageMagick binding for Python.
How do i get the list of image files, which i got as a result of using Wand?
For example there is a 2-page PDF file file.pdf and i converted it to 2 JPEG files file-0.jpg and file-1.jpg. How do i get the list ['file-0.jpg', 'file-1.jpg']?
Currently i simply use glob:
with Image(filename='file.pdf') as original:
with original.clone() as converted:
converted.format = 'jpeg'
converted.save(filename='file.jpg')
images = glob('*.jpg')
But maybe there is a more idiomatic way through Wand library itself.
You can use Image.sequence. Each sequence item has index.
from wand.image import Image
with Image(filename='file.pdf') as img:
img.save(filename='file.jpg')
if len(img.sequence) > 1:
images = ['file-{0.index}.jpg'.format(x) for x in img.sequence]
else:
images = ['file.jpg']

Wand Python multi-size icon

i'm trying to use Wand to create a multi-size ico, but i don't find anything talking about that, only normal conversion, to ico... i've found "Sequences":
https://wand.readthedocs.org/en/latest/roadmap.html
and sequences look like what i need, but i only see samples trying to read the multiple images, but not how to create, am i missing something? or is not possible?
or is it possible to do using PIL/PILLOW?
You can append() a single image to Image.sequence list. For example:
from wand.color import Color
from wand.image import Image
with Image(width=32, height=32, background=Color('red')) as ico:
with Image(width=16, height=16, background=Color('green')) as s16:
ico.sequence.append(s16)
ico.save(filename='multisized.ico')
Result (multisized.ico):
I had a similar problem, but with creating a multi-page PDF from multiple JPEG files. In Imagemagick i used command -adjoin. In Wand i did the following:
from glob import glob
from wand.image import Image
files = glob('*.jpg')
with Image() as orig: # create empty Image object
for f in files:
page = Image(filename=f)
orig.sequence.append(page)
orig.save(filename='result.pdf')

Categories