Skip to content

Commit d0730d4

Browse files
AddisonSchillerfelliott
authored andcommitted
Adding support for .psd files to MFR
Also added an ext variable to base_exporter. Extensions get swallowed in the image exporter. This serves as a work around to it.
1 parent b3f0f7d commit d0730d4

File tree

5 files changed

+27
-3
lines changed

5 files changed

+27
-3
lines changed

mfr/core/extension.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@
55

66
class BaseExporter(metaclass=abc.ABCMeta):
77

8-
def __init__(self, source_file_path, output_file_path, format):
8+
def __init__(self, ext, source_file_path, output_file_path, format):
9+
10+
"""Initialize the base exporter.
11+
12+
:param ext: the name of the extension to be exported
13+
:param source_file_path: the path of the input file
14+
:param output_file_path: the path of the output file
15+
:param format: the format of the exported file (e.g. 1200*1200.jpg)
16+
"""
17+
18+
self.ext = ext
919
self.source_file_path = source_file_path
1020
self.output_file_path = output_file_path
1121
self.format = format

mfr/core/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def make_exporter(name, source_file_path, output_file_path, format):
4848
namespace='mfr.exporters',
4949
name=normalized_name,
5050
invoke_on_load=True,
51-
invoke_args=(source_file_path, output_file_path, format),
51+
invoke_args=(normalized_name, source_file_path, output_file_path, format),
5252
).driver
5353
except RuntimeError:
5454
raise exceptions.MakeExporterError(

mfr/extensions/image/export.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22
import imghdr
3+
import warnings
34

45
from PIL import Image
6+
from psd_tools import PSDImage
57

68
from mfr.core import extension
79
from mfr.extensions.image import exceptions
@@ -23,7 +25,16 @@ def export(self):
2325
'max_size_h': max_size[1],
2426
})
2527
try:
26-
image = Image.open(self.source_file_path)
28+
if self.ext in ['.psd']:
29+
# silence warnings from psd-tools
30+
# Warnings warn of outdated depedency that is a pain to install
31+
# and about colors being possibly wrong
32+
with warnings.catch_warnings():
33+
warnings.simplefilter("ignore")
34+
image = PSDImage.load(self.source_file_path).as_PIL()
35+
else:
36+
image = Image.open(self.source_file_path)
37+
2738
if max_size:
2839
# resize the image to the w/h maximum specified
2940
ratio = min(max_size[0] / image.size[0], max_size[1] / image.size[1])

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pydocx==0.7.0
2222

2323
# Image
2424
Pillow==2.8.2
25+
psd-tools==1.4
2526

2627
# IPython
2728
ipython==5.2.2

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def parse_requirements(requirements):
5353
'.png = mfr.extensions.image:ImageExporter',
5454
'.bmp = mfr.extensions.image:ImageExporter',
5555
'.gif = mfr.extensions.image:ImageExporter',
56+
'.psd = mfr.extensions.image:ImageExporter',
5657
'.tif = mfr.extensions.image:ImageExporter',
5758
'.tiff = mfr.extensions.image:ImageExporter',
5859

@@ -669,6 +670,7 @@ def parse_requirements(requirements):
669670
'.gif = mfr.extensions.image:ImageRenderer',
670671
'.ico = mfr.extensions.image:ImageRenderer',
671672
'.png = mfr.extensions.image:ImageRenderer',
673+
'.psd = mfr.extensions.image:ImageRenderer',
672674
'.tif = mfr.extensions.image:ImageRenderer',
673675
'.tiff = mfr.extensions.image:ImageRenderer',
674676

0 commit comments

Comments
 (0)