zyddnys / manga-image-translator

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

Improve handling of manhwa #291

Open My12123 opened 1 year ago

My12123 commented 1 year ago

Can you make the text translated when they are on different images during batch processing? image

JustFrederik commented 1 year ago

This would solve the issue. You could preprocess the images when an arg like —merge-images is given: Python concat images

@My12123 instead of creating a bunch of issues couldn’t you google the issue before and solve the problem yourself and make a pull request or at least come up with an attempt to solve it. To implement it you literally need to search for it online and copy the code.

My12123 commented 1 year ago

It didn't really help me. Can I get more details? @JustFrederik You don't like me opening up problems so much?

JustFrederik commented 1 year ago

This should be for concat vertical. I didn’t test it but it should work. I literally changed under 10 letters, so what is the problem?


def concat_images(imga, imgb):
    """
    Combines two color image ndarrays side-by-side.
    """
    ha,wa = imga.shape[:2]
    hb,wb = imgb.shape[:2]
    max_width = np.max([wa, wb])
    total_height = ha+hb
    new_img = np.zeros(shape=(total_height, max_width, 3))

    new_img[:ha,:wa]=imga
    new_img[ha:ha+hb,:wb]=imgb
    return new_img

def concat_n_images(image_path_list):
    """
    Combines N color images from a list of image paths.
    """
    output = None
    for i, img_path in enumerate(image_path_list):
        img = plt.imread(img_path)[:,:,:3]
        if i==0:
            output = img
        else:
            output = concat_images(output, img)
    return output
My12123 commented 1 year ago

сделать запрос на извлечение

I can't make an extraction request, I can only compare. 2023-04-09_16-06-00

JustFrederik commented 1 year ago

It didn't really help me. Can I get more details? @JustFrederik You don't like me opening up problems so much?

@My12123 if it’s a problem, it should be pointed out, but before you should check if someone else had the problem and fix it yourself or at least give the people a hint how to solve it. You cloud habe googled how to fix it and linked the stack Overflow if you can’t implement it or when you have another issue you could state the error and what you tried. But most of your questions are duplicates or just a screenshot of your terminal. That seems kinda lazy.

JustFrederik commented 1 year ago

сделать запрос на извлечение

I can't make an extraction request, I can only compare. 2023-04-09_16-06-00

That’s another example for what I just said. My guess is that you are trying to make a pull request. Here’s a tutorial how to make a pull request: How to make a pull request. Why aren’t you trying to solve the issue yourself? It’s easier and faster to ask Google or Chatgpt.

My12123 commented 1 year ago

@My12123 if it’s a problem, it should be pointed out, but before you should check if someone else had the problem and fix it yourself or at least give the people a hint how to solve it.

There was no such problem before. I am not a programmer to suggest changing the code.

JustFrederik commented 1 year ago

@thatDudo

сделать запрос на извлечение

I can't make an extraction request, I can only compare. 2023-04-09_16-06-00

What are you trying here then?

My12123 commented 9 months ago

A script for combining photos. connecting-manga-pages-vertically.py

from PIL import Image
import os

# Получение списка файлов в директории
directory = 'manga_pages'
files = os.listdir(directory)

# Загрузка изображений и определение размеров нового изображения
images = [Image.open(os.path.join(directory, file)) for file in files]
width = images[0].width
height = sum(image.height for image in images)

# Создание нового изображения
result_image = Image.new('RGB', (width, height))

# Заполнение нового изображения загруженными изображениями вертикально
y_offset = 0
for image in images:
    result_image.paste(image, (0, y_offset))
    y_offset += image.height

# Сохранение результата
result_image.save('./input/merged_image.jpg')

A script for splitting photos. split_manga.py

import cv2
import os
import glob
from tqdm import tqdm

def split_manga(image_path, output_path, max_width=2000, max_height=2500):
  """Splits a manga image into multiple images, ensuring that no block of text is transferred to another image and the output resolution of each image is not more than max_width x max_height.

  Args:
    image_path: The path to the input manga image.
    output_path: The path to the output directory where the split images will be saved.
    max_width: The maximum width of each output image.
    max_height: The maximum height of each output image.
  """

  image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)

  # Check if the image is None.
  if image is None:
    raise ValueError("The input image is None.")

  # Get the height and width of the input image.
  height, width, _ = image.shape

  # Check if the input image is less than or equal to 2000x2500.
  if height <= max_height and width <= max_width:
    raise ValueError("The input image is too small to process. You need to increase it to be more than 2000x2500.")

  # Split the image into horizontal strips.
  strips = []
  for y in range(0, height, max_height):
    strip = image[y:y + max_height, :]
    strips.append(strip)

  # Split each strip into vertical blocks.
  blocks = []
  for strip in strips:
    for x in range(0, width, max_width):
      block = strip[:, x:x + max_width]
      blocks.append(block)

  # Save each block as a separate image.
  for i, block in enumerate(tqdm(blocks)):
    output_image_path = os.path.join(output_path, f"block_{i}.jpg")
    cv2.imwrite(output_image_path, block)

def batch_split_manga(image_paths, output_path, max_width=2200, max_height=3000):
  """Splits a batch of manga images into multiple images, ensuring that no block of text is transferred to another image and the output resolution of each image is not more than max_width x max_height.

  Args:
    image_paths: A list of paths to the input manga images.
    output_path: The path to the output directory where the split images will be saved.
    max_width: The maximum width of each output image.
    max_height: The maximum height of each output image.
  """

  for image_path in image_paths:
    split_manga(image_path, output_path, max_width, max_height)

if __name__ == "__main__":
  # Batch processing example.
  image_paths = glob.glob("./input/*.jpg")
  output_path = "./output"

  batch_split_manga(image_paths, output_path)

run_manga_processing.py

import os
import cv2

# Create the input and output folders if they don't exist.
if not os.path.exists("./input"):
  os.makedirs("./input")
if not os.path.exists("./output"):
  os.makedirs("./output")

# Set the maximum image size to unlimited.
cv2.IMREAD_UNCHANGED_SIZE = None

# Try to run the connecting-manga-pages-vertically.py script.
try:
  os.system("python ./connecting-manga-pages-vertically.py")
except FileNotFoundError:
  print("The connecting-manga-pages-vertically.py script does not exist.")

# Try to run the split_manga.py script.
try:
  os.system("python ./split_manga.py")
except FileNotFoundError:
  print("The split_manga.py script does not exist.")

# Get the command that the user led after the launch command.
user_command = input("Enter the command that you want to run: ")

# Run the user command.
os.system(user_command)

# Clear the input and output folders.
os.system("rm -rf ./input")
os.system("rm -rf ./output")

Code run_manga_processing.zip

My12123 commented 8 months ago

@thatDudo @zyddnys We need help in adding this code to the project and activating it with an argument or automatically with a large size and a large number of images.