镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Is it possible to generate or simulate italic text for any font other than an original italic font? #7292

Closed as not planned

Description

@nissansz

Is it possible to generate or simulate italic text for any font other than an original italic font?

Activity

  1. radarhere commented on Jul 19, 2023

    @radarhere
    Member

    What you're talking about is known as 'fake italics' - slanting text instead of using a properly designed italic font. Pillow doesn't offer that functionality, no, and I'm unable to find anything that says that FreeType supports it.

    Unless you're talking about variation fonts, where multiple styles are contained within a single font file. That is supported by Pillow - https://pillow.readthedocs.io/en/stable/releasenotes/6.1.0.html#variation-fonts

  2. nissansz commented on Jul 19, 2023

    @nissansz
    Author

    Check below effect, some words slanted to right, some to left. Some normal.
    image

  3. radarhere commented on Jul 19, 2023

    @radarhere
    Member

    I know what italics looks like, yes. I'm saying that no, Pillow can't generate italics if the font is not italic (unless you have a variable font), and I can't find any evidence to say that FreeType (a dependency that Pillow uses to render text) does either.

  4. nissansz commented on Jul 19, 2023

    @nissansz
    Author

    Any method to transform, like wavy?

  5. nulano commented on Jul 19, 2023

    @nulano
    Contributor

    I found the following answer on StackOverflow: https://stackoverflow.com/a/51701378/1648883

    The (now removed) getsize function there is used incorrectly, the following is a corrected version:

    def generate_text(text):
        fnt = ImageFont.truetype('arial.ttf', 72)
        mask = fnt.getmask(text, mode='1')
        img = Image.new('1', mask.size)
        img.putdata([x for x in mask])
        img = img.convert('RGB')
        return img
    
    def add_border(img, width):
        new = Image.new('RGB', (img.width + 2 * width, img.height), (0, 0, 0))
        new.paste(img, (width, 0))
        return new
    
    def shear(img, shear):
        shear = img.transform(img.size, Image.AFFINE, (1, shear, 0, 0, 1, 0))
        return shear
    
    img = generate_text('test')
    img = add_border(img, 200)
    img = shear(img, 2)
  6. nissansz commented on Jul 19, 2023

    @nissansz
    Author

    Thank you.
    Which parameter to control direction and angle?

  7. radarhere commented on Jul 19, 2023

    @radarhere
    Member

    The shear argument to shear().

    So shear(img, 2) with FreeMono.ttf gives
    2

    shear(img, 1) gives
    1

    shear(img, -1) gives
    minus1

  8. nissansz commented on Jul 19, 2023

    @nissansz
    Author

    it looks good. Thank you.

  9. nissansz commented on Jul 19, 2023

    @nissansz
    Author

    Is it possible to use ImageFont.FreeTypeFontFamily for above function?

  10. radarhere commented on Jul 19, 2023

    @radarhere
    Member

    Oh, your problem is that FreeTypeFontFamily doesn't have getmask.

    It should just be a matter of replacing

    fnt = ImageFont.truetype('arial.ttf', 72)
    mask = fnt.getmask(text, mode='1')

    with

    arial_fnt = ImageFont.truetype('arial.ttf', 72)
    fnt = ImageFont.FreeTypeFontFamily(arial_fnt)
    mask = fnt.getmask2(text, mode='1')[0]

    in your code.

    But again - you use #6926 at your own risk, as it has not been finished yet.

  11. nissansz commented on Jul 19, 2023

    @nissansz
    Author

    I tried your code, there is below error.

       mask = fnt .getmask(text, mode='1')[0]
    AttributeError: 'FreeTypeFontFamily' object has no attribute 'getmask'
    
  12. nulano commented on Jul 19, 2023

    @nulano
    Contributor

    getmask2

  13. nissansz commented on Jul 19, 2023

    @nissansz
    Author

    Ok. It works now. Thank you.

  14. 4 remaining items

  15. nissansz commented on Jul 20, 2023

    @nissansz
    Author

    Thank you.

  16. nissansz commented on Jul 20, 2023

    @nissansz
    Author

    Can bold font be realized with similar method above? Any parameters to adjust?

  17. nissansz commented on Jul 20, 2023

    @nissansz
    Author

    I also want to add background color for some random text, how to realize it with pillow?

    image

  18. radarhere commented on Jul 20, 2023

    @radarhere
    Member

    Can bold font be realized with similar method above? Any parameters to adjust?

    Without a bold font or a variation font, no, Pillow can't do this, and I don't think Freetype can either.

  19. nissansz commented on Jul 20, 2023

    @nissansz
    Author

    Can bold font be realized with similar method above? Any parameters to adjust?

    Without a bold font or a variation font, no, Pillow can't do this, and I don't think Freetype can either.

    Any fast method like opencv's erode, dilate to simulate bold?

  20. nissansz commented on Jul 20, 2023

    @nissansz
    Author

    image

    I used pixel color replace to simulate highlight, it may be slow? Any fast method?

  21. nissansz commented on Jul 20, 2023

    @nissansz
    Author

    Below seems working, any improvements needed?

    def shear(img, shear):
        shear = img.transform(img.size, Image.AFFINE, (1, shear, 0, 0, 1, 0))
        new = Image.new('RGB', (img.width, img.height), "#fff") # 白底
    
        new1 = Image.new('RGBA', (500, img.height), "#f00") # 白底
        new.paste(new1 , (0, 0), new1 )
    
        new.paste(shear, (0, 0), shear)
        return new
  22. nulano commented on Jul 20, 2023

    @nulano
    Contributor

    Any fast method like opencv's erode, dilate to simulate bold?

    Something like MaxFilter might work:
    https://pillow.readthedocs.io/en/stable/reference/ImageFilter.html#PIL.ImageFilter.MaxFilter

  23. nulano commented on Jul 20, 2023

    @nulano
    Contributor

    I also want to add background color for some random text, how to realize it with pillow?

    You can add a background by using my answer on StackOverflow: https://stackoverflow.com/a/72302587/1648883

  24. nissansz commented on Jul 20, 2023

    @nissansz
    Author

    thank you, will try

  25. github-actions commented on Jul 27, 2023

    @github-actions

    Closing this issue as no feedback has been received.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions