-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtiff.py
More file actions
55 lines (48 loc) · 1.52 KB
/
tiff.py
File metadata and controls
55 lines (48 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Do file io - TIFF."""
from __future__ import annotations
from PIL import Image
from layeredimage.io.common import expandLayersToCanvas
from layeredimage.layeredimage import LayeredImage
from layeredimage.layergroup import Layer
#### TIFF ####
def openLayer_TIFF(file: str) -> LayeredImage:
"""Open a .tiff or a .tif file into a layered image."""
project = Image.open(file)
layers = []
dimensions = [0, 0]
for index in range(project.n_frames):
# Load the correct image
project.seek(index)
# Update the project dimensions
for indx, dimension in enumerate(dimensions):
if project.size[indx] > dimension:
dimensions[indx] = project.size[indx]
ifd = project.ifd.named()
# Offsets
offsetX = 0
offsetY = 0
if "XPosition" in ifd:
offsetX = int(
ifd["XPosition"][0][0] / ifd["XPosition"][0][1] * ifd["XResolution"][0][0]
)
if "YPosition" in ifd:
offsetY = int(
ifd["YPosition"][0][0] / ifd["YPosition"][0][1] * ifd["YResolution"][0][0]
)
# Add the layer
layers.append(
Layer(
name=ifd["PageName"][0],
image=project.copy(),
dimensions=(ifd["ImageWidth"][0], ifd["ImageLength"][0]),
offsets=(offsetX, offsetY),
opacity=1,
visible=True,
)
)
project.close()
return LayeredImage(layers, (dimensions[0], dimensions[1]))
def saveLayer_TIFF(fileName: str, layeredImage: LayeredImage) -> None:
"""Save a layered image as .tiff or .tif."""
layers = expandLayersToCanvas(layeredImage, "TIFF")
layers[0].save(fileName, compression=None, save_all=True, append_images=layers[1:])