zyddnys / manga-image-translator

Translate manga/image 一键翻译各类图片内文字 https://cotrans.touhou.ai/
https://cotrans.touhou.ai/
GNU General Public License v3.0
5.31k stars 547 forks source link

add PDF renderer to python(already implemented) #395

Open JustFrederik opened 1 year ago

JustFrederik commented 1 year ago

Issue

I wanted a svg and pdf output so i found pango/cairo which is a c library that does that. It seemed like the library needed to be installed for the python version so i implemented it in rust so it will be compiled into the library. This is what i got: Cairo/Pango image renderer. Its pretty simple, but should work. It only needs python functions to call the code. An example for rust libaries in python would be Huggingface downloader. If you look into the code its just some macros and the pyo3 dependency.

(Also contains function to merge multiple pdfs into one file & render as svg/ps/jpeg/png/pdf/{some other image formats})
I need to know how it could be imlpemented into the python code. How should the python function look like. Example:

rust_renderer.render("svg"...)
thatDudo commented 1 year ago

You can add it by creating a new ExportFormat class in manga_translator/save.py. It will be auto registered and you can get the text regions and intermediate images from the ctx variable.

JustFrederik commented 1 year ago

@thatDudo not what I meant. The question was what the args of the function are and what default values it should use. It has quite a lot of options https://github.com/JustFrederik/image_writer/blob/master/src/input.rs

It also has existing formats, but it would need to be benchmarked to decide which is actually faster

thatDudo commented 1 year ago

I think something like this would work for our usecases:

from rust_renderer import PdfRenderer

pdf = PdfRenderer()
pdf.set_background(img)
pdf.add_text("text", (x, y, w, h), font_size=10, alignment="center", font_family="...", style="italic", color=0xFF0000, vertical=False)
pdf.save("file.pdf")

You dont have to make classes though thats just my personal preference xD.

Not sure if auto_dir will be enough or if you have to add that read_direction parameter (And infer direction from language).

JustFrederik commented 1 year ago

@thatDudo i used the default values from my original test. They most likly need some change. I created a branch with an example in main.py.

#maturin develop --release

#style, vertical
pdf = mit_tools.PangoRenderer("", False)
pdf.set_background(img)
# text, (x, y, width, height), font_size, vertical_allignment, horizontal_alignment, font_color
pdf.add_text("Hello world", (0.0, 0.0, 100.0, 100.0), 12, "center", "center", 0xFF5733)
# filename, width, height, output format
pdf.save("hello.pdf", 6000, 6000, "pdf false")
pdf.save("hello.png", 6000, 6000, "png true")
pdf.save("hello.svg", 6000, 6000, "svg")

https://github.com/JustFrederik/mit_tools/tree/render

thatDudo commented 1 year ago

Sorry it took me so long @JustFrederik . I just tested it on my local machine and it works quite fine.

I searched up whether it is possible to add python type hints (linting) to pyo3 libraries but im not sure. Is it possible?

JustFrederik commented 1 year ago

Im not quite sure what you mean. Do you mean the signature of the functions? Pyo3 docs.

thatDudo commented 1 year ago

Screenshot from 2023-06-16 22-46-04

It seems as though the function gets recognized as Any and so I dont know which functions and arguments I can use with it.

Its probably not that important though.

thatDudo commented 1 year ago
  1. You could probably rename set_background to add_image if you dont want to use the background dimensions for the resulting document (You ask for the width/height in save again).
  2. I forgot to mention above but it might be useful to have two color arguments for foreground and outline(/background) of the font
  3. The save function appends a .svg to the dest argument which is redundant i think. If the user of the library wants to make a svg file that is not ended by .svg it should be possible to do.
  4. add_text adds a colored background
  5. For the outputmode like here save(..., 'pdf false') the false bool should be optional.
JustFrederik commented 1 year ago

For the outputmode like here save(..., 'pdf false') the false bool should be optional.

What should be the default behavior? Keep it in memory and merge or saving the image? Same goes for the other values like lossless compression for png and jpeg image quality

JustFrederik commented 1 year ago

If needed setter methods for more options could be implemented. You just need to say what you want to change(char spacing, stroke width…) and what you want to keep as default.