Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Andrew Murray
    @radarhere
    @Peaceful-Whale https://github.com/python-pillow/Pillow/issues/3115#issuecomment-385713336 is someone with a similar problem in the past, if that’s of any help
    Ak4zh
    @Ak4zh

    Is there a way I can read only selected text froom image that has a colored background?

    I am trying to read price from tradingview charts image.

    For example I want to read only the text with red (0.66133) , green(0.65608) , gray (0.65995) or blue (0.65773) background from this image: https://www.tradingview.com/x/e37LLsNZ Using pytesseract to get text from image
    Andrew Murray
    @radarhere
    To give you a quick answer - if I rephrase your request, you want to crop out parts of an image within a rectangle outline of a certain color. This problem is solved for a different situation in python-pillow/Pillow#4049, using https://pillow.readthedocs.io/en/stable/reference/PixelAccess.html to read individual pixel colors
    Andrew Murray
    @radarhere
    Ok, here is code to process your image and save cropped rectangles with the text. The red, green, gray and blue colors are specified. It will take a minute or two to run. It is searching for a rectangle of one of the colors, of a reasonable size.
    from PIL import Image
    
    colors = ((0,160,0),(33,150,243),(88,88,88),(255,0,0))
    
    with Image.open('tradingview.png') as im:
        px = im.load()
        x, y = 0, 0
        currentColor = None
        processedPixels = []
        i = 0
    
        # Start searching through all the pixels
        for y in range(0, im.height):
            for x in range(0, im.width + 1):
                def getColor(x, y):
                    try:
                        return px[x, y]
                    except IndexError:
                        return
    
                if (x, y) in processedPixels:
                    continue
                color = getColor(x, y)
                if color != currentColor:
                    if currentColor is not None:
                        max_x = x
    
                        # The color line has ended, from min_x to max_x
                        x_range = min_x, max_x
    
                        # The left side of our rectangle must also be a solid color
                        # So search from the top left until we have a solid left side
                        for y2 in range(y+1, im.height+1):
                            for x2 in range(min_x, max_x+1):
                                if getColor(x2, y2) == currentColor:
                                    min_x = x2
                                    break
                            else:
                                # No color in this line, so we have hit the bottom of the rectangle
                                max_y = y2
                                break
    
                        # The same for the right side
                        for y2 in range(y+1, max_y+1):
                            for x2 in range(max_x+1, min_x, -1):
                                if getColor(x2, y2) == currentColor:
                                    max_x = x2
                                    break
    
                        # Check that this rectangle is of a reasonable size
                        if max_x - min_x > 10 and max_y - y > 10:
                            # Record that we've processed these pixels, to save time
                            for y2 in range(y, max_y+1):
                                for x2 in range(x_range[0], x_range[1]+1):
                                    processedPixels.append((x2, y2))
    
                            # Check that the box is not purely one color
                            singleColor = True
                            for y2 in range(y, max_y+1):
                                for x2 in range(min_x, max_x+1):
                                    if getColor(x2, y2) != currentColor:
                                        singleColor = False
                                        break
                                if not singleColor:
                                    break
                            if not singleColor:
                                i += 1
    
                                # Save the cropped image
                                im.crop((min_x, y, max_x, max_y)).save("out_"+str(i)+".png")
                    if color in colors:
                        # Start a line with a relevant color
                        currentColor = color
                        min_x = x
                    else:
                        currentColor = None
    Actually, I’ve just realised that you’ll want to increase the minimum size of the rectangles, otherwise it will identify parts of the TradingView logo. You get the idea though
    Ojas Kale
    @iojas

    Hello, when I am running
    im = Image.open(path)
    I am getting error
    ValueError: Decompressed Data Too Large

    I know I am suppose to set MAX_TEXT_CHUNK or MAX_TEXT_MEMORY, but I do not know where to set it. Does anyone have an idea where.

    WorstGamr
    @xWorstGamr_twitter
    Hello, I'm new to using PIL, and I need to draw text. However I'll be using this with discord.py, and the text will be the name of a user. However the user's name can be long, and so I'm wondering if there is a way to set a maximum width to the text, so that if it exceeds, it'll resize the font to fit.
    Andrew Murray
    @radarhere
    For the record, MAX_TEXT_CHUNK and MAX_TEXT_MEMORY are set on PngImagePlugin
    >>> from PIL import PngImagePlugin
    >>> PngImagePlugin.MAX_TEXT_CHUNK
    >>> PngImagePlugin.MAX_TEXT_MEMORY
    Andrew Murray
    @radarhere
    As for setting a maximum width for text, you would be interested in getsize.
    from PIL import Image, ImageFont
    font=ImageFont.truetype('Tests/fonts/FreeMono.ttf', size=14)
    print(font.getsize('Username'))  # (68, 12)
    
    font=ImageFont.truetype('Tests/fonts/FreeMono.ttf', size=28)
    print(font.getsize('Username'))  # (135, 23)
    Jason Raveling
    @webunraveling_twitter
    Hello. What is the difference between PIL and Pillow?
    Andrew Murray
    @radarhere

    Pillow is a fork of PIL.

    PIL has its last release in 2009
    Pillow had its last release a week ago. So there are a variety of features, bug fixes and security fixes that have been performed since PIL. However, Pillow is largely backwards compatible with the PIL API.

    PIL does not support Python 3
    Pillow supports Python 3, and dropped support for Python 2 a few releases ago.

    Hummer12007
    @Hummer12007
    How can a OSError: image file is truncated (0 bytes not processed) error occur?
    It occurs when I load an image (successfully and then try to copy or show it)
    from PIL import Image
    im = Image.open('test.jpg') # completes successfully
    im.copy() # fails with OSError: image file is truncated (0 bytes not processed)
    Here is the image: https://l.sr.ht/pBH2.jpg
    Hummer12007
    @Hummer12007
    LOAD_TRUNCATED_FILES fixes the issue indeed, but I'm interested in the underlying reason
    Codie (蒂诺)
    @lasso-codie
    Hello, I was brough here by PEP 3118 wanting to learn more about the memory model of PIL. How does the library handle a picture consisting of different, non contiguous memory blocks, so that it seems like a single block in python? I was digging a little bit the codebase but didn't find what I was searching for.
    2 replies
    Ronald Lencevičius
    @392781
    Hi, I'm not very experienced with contributing to large projects like Pillow. I put together a solution to issue #4634; however, I'm a bit stuck setting up the test environment to build my tests. Is there some sort of guide any of you can point me to? I don't want to make a pull request without having made a good effort to cover all requirements for a new feature.
    Also, if you’d like to run tests the same way that they will be run when the PR is created, you could use GitHub Actions - it’s possible you will just see results at https://github.com/392781/Pillow/actions once your push to your repo - or sign up for https://travis-ci.org/ or https://www.appveyor.com/
    Ronald Lencevičius
    @392781
    Yeah, I read through the readme. I'll look at github actions. Thanks for the tip!
    Technerder
    @Technerder
    How can I draw text between two points on an image so that the font scales perfectly and it automatically splits into a multiline string if it needs to?
    Andrew Murray
    @radarhere

    Your description leaves a bit of room for interpretation - you would like scaling, but also splitting into a multiline string... so there's a minimum font size involved, and once you hit that, it starts splitting the string rather than scaling?

    Essentially, https://pillow.readthedocs.io/en/stable/reference/ImageFont.html#example shows how to draw that on an image

    You will have to use https://pillow.readthedocs.io/en/stable/reference/ImageFont.html#PIL.ImageFont.FreeTypeFont.getsize to calculate the space that your text will occupy, and change the font size accordingly. Pillow does not provide built-in scaling. Similarly, you will have to split the string into multiple lines yourself.

    Kieran Boyle
    @k-boyle

    Hello, I'm trying to take a gif and split it into 4 pieces, I've got all of my frames split but I'm having an issue when it comes to saving the actual gifs.
    I've got

    for frames in gifs:
        first_frame = frames[0]
        first_frame.save (f'{gif_index}.gif', 'GIF', save_all=True, append_images=frames[1:], optimize=False, loop=0, duration=original_duration)

    and the length of frames is 2, it seems that when the 2 frames are identical the gif gets saved as a single frame (which i've verified by reading the saved gif back in and checking n_fames). It looks like it's this issue python-pillow/Pillow#2314 but that looks like it was fixed?

    when the two frames are different it saves them just fine
    Andrew Murray
    @radarhere
    I think this is considered a feature, rather than a bug. If the two frame are identical, why not save space by marking them as just one frame?
    Random-Fun
    @Random-Fun

    Hello so i did & C:/Users/lior/AppData/Local/Programs/Python/Python38-32/python.exe -m pip install Pillow

    But its still showing

    Traceback (most recent call last):
      File "c:/Users/lior/Documents/e3.py", line 8, in <module>
        from pillow import Image, ImageDraw, ImagePalette, ImageFont
    ModuleNotFoundError: No module named 'pillow'
    why is this
    Andrew Murray
    @radarhere
    Although the package is called Pillow, you should import from PIL
    So
    from PIL import Image, ImageDraw, ImagePalette, ImageFont
    JaivyDaam
    @JaivyDaam

    Hey Everyone,

    I am getting a little bit frustrated here hoping someone could point me in the right direction as I am unable to find it myself.

    I have images at the moment of which I want the full summary of “MetaData” within the file. This includes also other summary data found within the file such as ones that are left behind by other software like Photoshop. But at this moment I am unable to find any solution. Would anyone be able to point me in the right direction?

    Looking forward to your reply!

    Rolands Jegorovs
    @Rolandisimo
    Hello, I'm having troubled converting CMYK image to RGB with PIL. My colors get changed. Could someone help me with this question, please? Info in stackoverlfow
    https://stackoverflow.com/questions/65034220/how-to-keep-same-colors-when-converting-cmyk-image-to-rgb-using-pil