Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    I tried that and it didn't look right. But the problem was in the images. Thanks.
    I'm running into an issue while trying to overlay text over a gif stored in a BytesIO object
                        edited_image_bytes = BytesIO()
                        image = Image.open(BytesIO(await request.read()))
                        frames = []
                        for frame in ImageSequence.Iterator(image):
                            d = ImageDraw.Draw(frame)
                            d.text((10, 100), "Hello World")
                            del d
                            b = BytesIO()
                            frame.save(b, format="GIF")
                        frames[0].save(edited_image_bytes, format='GIF', save_all=True, append_images=frames[1:])
    The file is empty
    Andrew Tolmie
    Hi, have anyone seeing DLL not found error when piliow is install via conda-forge as dependency of matplotlib or bokeh?
    Andrew Murray
    I think you’ll be interested in conda-forge/pillow-feedstock#45 and python-pillow/Pillow#4155
    Andrew Tolmie
    Thanks, I have commented on the recently opened issue conda-forge/pillow-feedstock#66
    Michael Adam Cabatingan
    I am trying to use Pillow with eclipse, but I keep getting this error:
    from PIL import Image
    ImportError: No module named 'PIL'
    I have PIL specified in the external library thing
    Project > Properties > PYDEV - PYTHONPATH
    I have it pointed to the folder I extracted from the download on the Pillow website
    Do I need to point to Pillow - 7.0.0 or PIL in the scr folder of Pillow - 7.0.0
    Michael Adam Cabatingan
    I get the same error if I just delete that reference
    attempting to copy just the PIL folder and use that changes nothing
    I think if I do that and restart I get a different error though
    In the "project references" it shows that it is indeed referencing PIL
    Michael Adam Cabatingan
    I tried to copy all of the files in PIL into my project, but it gives me this error when I try to import them:
    Parent module '' not loaded, cannot perform relative import
    When I stick these in a folder and then call from there
    using from folder import Image
    i get this: ImportError: cannot import name '_imaging'
    Michael Adam Cabatingan
    Image.py is failing on these import lines
    from . import ImageMode, TiffTags, UnidentifiedImageError, __version__, _plugins
    from ._binary import i8, i32le
    from ._util import deferred_error, isPath
    Parent module '' not loaded, cannot perform relative import
    Which i think is just from running Image.py when its inside the package / whatever
    but I thought that might help
    Michael Adam Cabatingan
    I have made a test .py in the folder with everything else and imported it and ran a test func
    so thats working
    it appears to be an error with something in Image.py
    and idk what to do about it. I'm assuming it's becuase its just the PIL folder and not anything else
    i have now imported the entire pillow folder
    have made a test py function, and have imported it
    so I know that works and that I can import files from the PIL folder
    when I try to import Image though i get this
    ImportError: cannot import name '_imaging'
    Michael Adam Cabatingan
    I'm guessing it's something with Image then, and I don't know how to fix it
    Nitin Pasumarthy

    Is there a way to identify the exact settings for Image.save from original image (to avoid image size on disk inflation)?

    img_path = Path("my_image.jpeg")
    o = Image.open(img_path)
    print(f"Original image size: {img_path.lstat().st_size}")
    ... # perform lossless transformations like crop
    o.save("transformed_img.jpeg", quality=100, subsampling=0)

    This approach leads to an increase in size. I'm not sure how to achieve this.

    Complete code which for now does not do any transformations but opens and saves the image back to disk - https://colab.research.google.com/drive/1pCtAcbZD6YsIzA7vKn7xktF7Hp-leqo7
    Andrew Murray
    @Nithanaroy considering that even saving and opening a JPG can result in different pixel values - https://github.com/python-pillow/Pillow/issues/3015#issuecomment-368218971 - I wouldn’t think this is possible at the moment with Pillow
    Andrew Murray
    @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

    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
    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
    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):
                        return px[x, y]
                    except IndexError:
                if (x, y) in processedPixels:
                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
                                # No color in this line, so we have hit the bottom of the rectangle
                                max_y = y2
                        # 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
                        # 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
                                if not singleColor:
                            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
                        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

    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.

    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
    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
    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)