Commit 63293f07 authored by Ingo Meyer's avatar Ingo Meyer

GIF images are converted to PNG in LaTeX output mode (embedded images)

parent 71ce551b
...@@ -21,13 +21,16 @@ from .output_mode import get_output_mode ...@@ -21,13 +21,16 @@ from .output_mode import get_output_mode
TMP_IMAGE_FILENAME = "_downloaded_image{i:04d}.{ext}" TMP_IMAGE_FILENAME = "_downloaded_image{i:04d}.{ext}"
TMP_QPDF_OUT_FILENAME = "_qpdf_out.pdf" TMP_QPDF_OUT_FILENAME = "_qpdf_out.pdf"
OUT_PDF_FILENAME = "_converted_image{i:04d}.pdf" OUT_PDF_FILENAME = "_converted_image{i:04d}.pdf"
OUT_PNG_FILENAME = "_converted_image{i:04d}.png"
def is_url(url): def is_url(url):
return urlparse.urlparse(url).scheme not in ("", "file") return urlparse.urlparse(url).scheme not in ("", "file")
def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, convert_svg_to_pdf=False): def load_and_convert_image(
image_filepath_or_url, convert_to_base64=True, convert_svg_to_pdf=False, convert_gif_to_png=False
):
self = load_and_convert_image self = load_and_convert_image
if not hasattr(self, "image_number"): if not hasattr(self, "image_number"):
self.image_number = 1 self.image_number = 1
...@@ -35,9 +38,12 @@ def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, conver ...@@ -35,9 +38,12 @@ def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, conver
image_content = None image_content = None
image_filepath = None image_filepath = None
image_is_pdf = None image_is_pdf = None
image_is_png = None
image_is_svg = image_filepath_or_url.lower().endswith(".svg") image_is_svg = image_filepath_or_url.lower().endswith(".svg")
image_is_gif = image_filepath_or_url.lower().endswith(".gif")
image_will_be_converted = (image_is_svg and convert_svg_to_pdf) or (image_is_gif and convert_gif_to_png)
image_is_remote = is_url(image_filepath_or_url) image_is_remote = is_url(image_filepath_or_url)
if image_is_svg and convert_svg_to_pdf and (image_is_remote or convert_to_base64): if image_will_be_converted and (image_is_remote or convert_to_base64):
tmp_dir = tempfile.mkdtemp() tmp_dir = tempfile.mkdtemp()
else: else:
tmp_dir = None tmp_dir = None
...@@ -46,8 +52,8 @@ def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, conver ...@@ -46,8 +52,8 @@ def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, conver
response = requests.get(image_url) response = requests.get(image_url)
response.raise_for_status() response.raise_for_status()
image_content = response.content image_content = response.content
if not convert_to_base64 or (image_is_svg and convert_svg_to_pdf): if not convert_to_base64 or image_will_be_converted:
image_filepath = TMP_IMAGE_FILENAME.format(i=self.image_number, ext=os.path.splitext(image_url)[1]) image_filepath = TMP_IMAGE_FILENAME.format(i=self.image_number, ext=os.path.splitext(image_url)[1][1:])
if tmp_dir is not None: if tmp_dir is not None:
image_filepath = os.path.join(tmp_dir, image_filepath) image_filepath = os.path.join(tmp_dir, image_filepath)
with open(image_filepath.encode(sys.getfilesystemencoding()), "wb") as image_file: with open(image_filepath.encode(sys.getfilesystemencoding()), "wb") as image_file:
...@@ -55,7 +61,8 @@ def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, conver ...@@ -55,7 +61,8 @@ def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, conver
else: else:
image_filepath = image_filepath_or_url image_filepath = image_filepath_or_url
try: try:
if image_is_svg and convert_svg_to_pdf: if image_will_be_converted:
if image_is_svg:
image_content = None image_content = None
out_pdf_filename = OUT_PDF_FILENAME.format(i=self.image_number) out_pdf_filename = OUT_PDF_FILENAME.format(i=self.image_number)
if convert_to_base64: if convert_to_base64:
...@@ -77,6 +84,16 @@ def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, conver ...@@ -77,6 +84,16 @@ def load_and_convert_image(image_filepath_or_url, convert_to_base64=True, conver
image_filepath = out_pdf_filename image_filepath = out_pdf_filename
image_is_svg = False image_is_svg = False
image_is_pdf = True image_is_pdf = True
elif image_is_gif:
image_content = None
out_png_filename = OUT_PNG_FILENAME.format(i=self.image_number)
if convert_to_base64:
out_png_filename = os.path.join(tmp_dir, out_png_filename)
with open(os.devnull, "w") as devnull:
subprocess.check_call(["convert", image_filepath, out_png_filename], stdout=devnull, stderr=devnull)
image_filepath = out_png_filename
image_is_gif = False
image_is_png = True
if image_content is None: if image_content is None:
with open(image_filepath.encode(sys.getfilesystemencoding()), "rb") as image_file: with open(image_filepath.encode(sys.getfilesystemencoding()), "rb") as image_file:
image_content = image_file.read() image_content = image_file.read()
...@@ -131,7 +148,10 @@ class EmbeddedImage(Directive): ...@@ -131,7 +148,10 @@ class EmbeddedImage(Directive):
messages = [] messages = []
image = None image = None
image, image_reference = load_and_convert_image( image, image_reference = load_and_convert_image(
image_filepath, convert_to_base64=is_output_mode_html, convert_svg_to_pdf=not is_output_mode_html image_filepath,
convert_to_base64=is_output_mode_html,
convert_svg_to_pdf=not is_output_mode_html,
convert_gif_to_png=not is_output_mode_html,
) )
reference = directives.uri(image_reference) reference = directives.uri(image_reference)
self.options["uri"] = reference self.options["uri"] = reference
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment