mansuf / mangadex-downloader

A command-line tool to download manga from MangaDex, written in Python.
https://mangadex-dl.mansuf.link/
MIT License
477 stars 39 forks source link

Add optional volume cover for any `volume` formats #41

Closed mansuf closed 1 year ago

mansuf commented 1 year ago

There is list of cover representing each volumes in MangaDex.

image

Maybe we can use that in first page in any volume formats, instead of placing chapter info (or cover) in first page.

However, the chapter cover (info) is still can be added after volume cover. But, it's gonna be changed to optional feature (default to be disabled). So --no-chapter-info will get deprecated and wil be replaced with --use-chapter-cover instead.

Also add option to enable creation volume cover (--use-volume-cover / -uvc)

benpoe commented 1 year ago

may be nice to specify that the chapter info should still be used to depict chapters in a volume, so it's the volume cover And chapter info for each chapter to get the best volumes possible

mansuf commented 1 year ago

may be nice to specify that the chapter info should still be used to depict chapters in a volume, so it's the volume cover And chapter info for each chapter to get the best volumes possible

iirc, the pages order should be like this right ?

volume cover -> chapter cover (info) 1 -> main chapter 1 -> chapter cover (info) 2 -> main chapter 2 -> etc...
NidokingMaster commented 1 year ago

Wanted to suggest the same and found this issue, yes it would be totally awesome! Had to do this by hand now everytime, and used exactly your suggested order

volume cover -> chapter cover (info) 1 -> main chapter 1 -> chapter cover (info) 2 -> main chapter 2 -> etc...

Thanks!

Edit: Another suggestion would be to also change the cover inside the "chapter cover (info)" (right side) to the respective volume cover. Because currently, the chapter cover (info) shows the mangadex series cover (which is always the latest volume of the series) Example: chapter cover (info) "Vol. 1 Ch. 1" shows the cover of volume 9. I would prefer to see "Vol. 1 Ch. 1" and the cover of volume 1.

mansuf commented 1 year ago

Another suggestion would be to also change the cover inside the "chapter cover (info)" (right side) to the respective volume cover.

@NidokingMaster See https://github.com/mansuf/mangadex-downloader/issues/40#issuecomment-1366601996

kmfstudio commented 1 year ago

Another suggestion would be to also change the cover inside the "chapter cover (info)" (right side) to the respective volume cover.

@NidokingMaster See #40 (comment)

I'm not too familiar with the image manupilation possibilities of python, but isn't it a simple "duplicate image, crop out chapter info/ chapter cover and save ?" It's a simple manupilation with PHP 10 years ago, with python this must be simplier.

of course, it might not be worth the investment in time.

NidokingMaster commented 1 year ago

I'm not too familiar with the image manupilation possibilities of python, but isn't it a simple "duplicate image, crop out chapter info/ chapter cover and save ?" It's a simple manupilation with PHP 10 years ago, with python this must be simplier.

of course, it might not be worth the investment in time.

had the same idea, but the problem is that covers inside this chapter info have different width sizes to keep their correct aspect ratio, so you would have to crop more than the cover from right side. Not sure if there is a risk of cropping the info text, maybe mansuf knows. But basically cropping x pixels from the right and inserting the volume cover should be possible with Pillow (PIL) as described here: https://note.nkmk.me/en/python-pillow-paste/

Another approach would be to let python generate a new chapter info from the downloaded volume cover. I think mansuf had a similar idea but there were problems with linux and fonts. I created this attached mockup with only Arial font together with simple blur and slight darken effect, which should be possible with PIL:

0001

mansuf commented 1 year ago

but isn't it a simple "duplicate image, crop out chapter info/ chapter cover and save ?" It's a simple manupilation with PHP 10 years ago, with python this must be simplier.

Yes it is simple, but the blurred background still containing old volume cover not the new one. I have tried this in my test code

import requests
from PIL import Image

# NOTE: 0 means from the beginning

width_cropped_ch_info = 757
height_cropped_ch_info = 630
ch_info_res = (1200, 630)
vol_cover_new_size = (ch_info_res[0] - width_cropped_ch_info, height_cropped_ch_info)

# See https://pillow.readthedocs.io/en/stable/handbook/concepts.html#coordinate-system
coordinate_crop_ch_info = (0, 0, width_cropped_ch_info, height_cropped_ch_info)

# Get the chapter info
# https://mangadex.org/title/eb48c615-1c00-4726-b839-850d01d774b0/you-weren-t-my-sister-but-my-fianc-e
r = requests.get("https://og.mangadex.org/og-image/chapter/6b8cea19-30db-44cd-8486-06d1fce881fe", stream=True)

ch_info_orig = Image.open(r.raw)

# Remove the old cover
ch_info_orig = ch_info_orig.crop(coordinate_crop_ch_info)

# Make new blank image (1200, 630) and paste the chapter info to new image
ch_info = Image.new("RGB", ch_info_res)
ch_info.paste(ch_info_orig)

# Get the new volume cover
# https://mangadex.org/title/0bd72573-7512-4a38-b9fe-ae00a4776fdd/amarimono-isekaijin-no-jiyuu-seikatsu-yuusha-ja-nai-node-katteni-yarasete-moraimasu
r = requests.get("https://mangadex.org/covers/0bd72573-7512-4a38-b9fe-ae00a4776fdd/9aab4ff4-68da-4a74-8036-987a1f69d6b8.jpg", stream=True)

vol_cover = Image.open(r.raw)

# Resize it first to make it fit in chapter info
new_vol_cover = vol_cover.resize(vol_cover_new_size)

# Then paste the new volume cover to chapter info
ch_info.paste(new_vol_cover, box=(width_cropped_ch_info, 0))

# Save it
ch_info.save("ch_info.png")

The result:

ch_info

mansuf commented 1 year ago

Another approach would be to let python generate a new chapter info from the downloaded volume cover. I think mansuf had a similar idea but there were problems with linux and fonts.

Indeed, a lot of users have trouble to generate chapter info due to missing fonts. Maybe shipping a font can be fixed, but i need a font that has open source license. Arial font is proprietary license, so i cannot ship them.

Another problem for using self-generated chapter info is "out-of-frame text" problem in Pillow module. Of course it can be dynamically resized the fonts with this answer -> https://stackoverflow.com/questions/41528576/how-can-i-write-text-on-an-image-and-not-go-outside-of-the-border.

Using @NidokingMaster idea might be a good thing, but it takes a lot of efforts and time. I don't have full time to work on this project due to college. And there is tons of stuffs that has not been finished in this project yet.

benpoe commented 1 year ago

Another approach would be to let python generate a new chapter info from the downloaded volume cover. I think mansuf had a similar idea but there were problems with linux and fonts.

Indeed, a lot of users have trouble to generate chapter info due to missing fonts. Maybe shipping a font can be fixed, but i need a font that has open source license. Arial font is proprietary license, so i cannot ship them.

Another problem for using self-generated chapter info is "out-of-frame text" problem in Pillow module. Of course it can be dynamically resized the fonts with this answer -> https://stackoverflow.com/questions/41528576/how-can-i-write-text-on-an-image-and-not-go-outside-of-the-border.

Using @NidokingMaster idea might be a good thing, but it takes a lot of efforts and time. I don't have full time to work on this project due to college. And there is tons of stuffs that has not been finished in this project yet.

I'd suggest to add @NidokingMaster as a feature which might or might not be implemented in the future and just do what you have suggested yourself with the ordering of covers/chapter info which would good enough to close this very issue.

NidokingMaster commented 1 year ago

Here you go, maybe this can be implemented easily. I don't have a Linux machine to test currently, so you might want to change the font if Arial (Standard/Bold/Italic) does not work there. Might need some fine tuning, but worked for the covers and title lengths I tested.

from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageEnhance
import requests
import textwrap

def draw_multiple_line_text(image, text, font, text_color, text_start_height):
    '''
    From unutbu on [python PIL draw multiline text on image](https://stackoverflow.com/a/7698300/395857)
    '''
    draw = ImageDraw.Draw(image)
    image_width, image_height = image.size
    y_text = text_start_height
    lines = textwrap.wrap(text, width=19)
    for line in lines:
        line_width, line_height = font.getsize(line)
        draw.text(((image_width - line_width) / 2, y_text), 
                  line, font=font, fill=text_color)
        y_text += line_height

def main():
    # get image from MangaDex
    r = requests.get("https://mangadex.org/covers/0bd72573-7512-4a38-b9fe-ae00a4776fdd/9aab4ff4-68da-4a74-8036-987a1f69d6b8.jpg", stream=True)
    image = Image.open(r.raw)

    # resize image to fixed 1000px width (keeping aspect ratio) so font sizes and text heights match for all covers
    aspect_ratio = image.height / image.width
    new_width = 1000
    new_height = new_width * aspect_ratio

    image = image.resize((int(new_width), int(new_height)), Image.LANCZOS)

    # apply blur and darken filters
    image = image.filter(ImageFilter.GaussianBlur(6))
    image = ImageEnhance.Brightness(image).enhance(0.3)

    text_color = (255, 255, 255)

    title_font = ImageFont.truetype("arialbd.ttf", size=100)
    title_text_start_height = 50
    title_text = "You Weren't My Sister, but My Fiancée?"

    chinfo_font = ImageFont.truetype("arial.ttf", size=60)
    chinfo_text_start_height = 650
    chinfo_text = "Vol. 1 Ch. 3"

    scanlated_font = ImageFont.truetype("ariali.ttf", size=25)
    scanlated_text_start_height = 1100
    scanlated_text = "scanlated by:"

    group_font = ImageFont.truetype("arialbd.ttf", size=50)
    group_text_start_height = 1140
    group_text = "ToruScans"

    # draw Title text
    draw_multiple_line_text(image, title_text, title_font, text_color, title_text_start_height)
    # draw Volume/chapter info
    draw_multiple_line_text(image, chinfo_text, chinfo_font, text_color, chinfo_text_start_height)
    # draw "scanlated by:" text
    draw_multiple_line_text(image, scanlated_text, scanlated_font, text_color, scanlated_text_start_height)
    # draw scanlation group
    draw_multiple_line_text(image, group_text, group_font, text_color, group_text_start_height)

    image.save('pil_text.png')

if __name__ == "__main__":
    main()

Result:

pil_text

Would love to see a ordering like this for cbz Volumes: Volume Cover -> New Chapter Info Cover -> Main Chapter 1 -> New Chapter Info Cover 2 -> Main Chapter 2 -> etc...

mansuf commented 1 year ago

Optional volume cover feature has been added in https://github.com/mansuf/mangadex-downloader/commit/e3f6ac0cbe530d7c8947419a298600afe78354df

These feature can be installed in development version

pip install git+https://github.com/mansuf/mangadex-downloader.git@e3f6ac0cbe530d7c8947419a298600afe78354df

And it can be accessed with --use-volume-cover

mangadex-dl "https://mangadex.org/title/4265c437-7d57-4d31-9b1d-0e574a07b7b7" --use-volume-cover

As for chapter cover (info) it will be changed as optional feature, which means it is disabled by default, so you must enable it with --use-chapter-cover

mangadex-dl "https://mangadex.org/title/4265c437-7d57-4d31-9b1d-0e574a07b7b7" --use-volume-cover --use-chapter-cover
mansuf commented 1 year ago

For "New chapter cover (info)" idea, it will be discussed in https://github.com/mansuf/mangadex-downloader/issues/44