Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    Andrew Murray

    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

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

    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
    Although the package is called Pillow, you should import from PIL
    from PIL import Image, ImageDraw, ImagePalette, ImageFont

    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
    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
    Igor Kalat
    Hi! I have a DDS image in memory. I can't open it with Pillow as a BytesIO file-like object, because Image.open() complains that it can't identify the format. But if I write the bytes into a file with .dds extension, Image.open() works. Is there a way to tell Image.open() what format it is dealing with and skip the temporary file creation step?
    Andrew Murray
    If I attempt to do so with one of the Pillow test images, it works, so I have to imagine that your code isn’t quite right.
    from io import BytesIO
    from PIL import Image
    with open("Tests/images/dxt1-rgb-4bbp-noalpha_MipMaps-1.dds", "rb") as fp:
        data = fp.read()
    b = BytesIO(data)
    with Image.open(b) as im:
    Igor Kalat
    Indeed. I managed to get it to work.
    hello, how do I know what coordinate system my exif GPS coordinates are in?
    but it returns 3-tuples and I'm unsure what the indices represent
    Andrew Murray
    actually it seems it gives it in just minutes and seconds
    while degrees = 0.0
    is this odd?
    Kipkurui Mutai
    Can someone please check on Issue #5286
    Andrew Murray
    I've added a comment, asking for some more information
    Kipkurui Mutai
    @radarhere I have provided more information
    Kipkurui Mutai
    @radarhere Can you please answer my last question on #5286
    Aahnik Daw

    I want to do a simple thing. can anyone send a link, or a short code snippet, or a tutorial

    Put an image watermark on a GIF in positions like top,bottom,left,right, top-right, top-left, middle, etc.

    So, I have an image and a GIF. I want to put the image on the GIF.

    Andrew Murray
    @Kipkurui-mutai hugovk and I have made comments
    Andrew Murray
    @aahnik I presume you're talking about an animated GIF? If so, see what you think of this
    from PIL import Image, ImageSequence
    def addOverlay(frame):
        im = Image.open('Tests/images/hopper.png')
        position = 'topLeft'
        coords = {
            'topLeft': (0, 0),
            'bottomRight': (frame.width - im.width, frame.height - im.height)
        frame.paste(im, coords[position])
        return frame
    im = Image.open('Tests/images/iss634.gif')
    # Run 'addOverlay' on each frame in the image
    frames = ImageSequence.all_frames(im, addOverlay)
    # Save the frames as a new image
    frames[0].save('out.gif', save_all=True, append_images=frames[1:])
    Aahnik Daw

    @radarhere thanks a lot, that is perfect. But one, thing I cant figure out.

    I had image.gif on which I want to put image.png-> out.gif

    Now, image.png had a transparent background.

    but in the out.gif, the image.png is having a white background

    the watermark, had a transparent background
    but after putting it on the gif, its background became white
    Aahnik Daw
    it is working fine, except colors getting distorted
    how can i fix this ?
    Andrew Murray
    Could you provide a copy of the two input images, so that we can replicate the problem?
    If I'm reading correctly that is, and you're still having a problem with the colors
    Hi there, a user of an open-source program I maintain is getting an error I've never seen before and don't know what the cause is:
    2021-03-03 22:34:52,004:ERROR:An unknown error occurred while creating a notification image: Traceback (most recent call last): File "trade_worker.py", line 92, in send_trade File "notification_builder.py", line 81, in build_image File "notification_builder.py", line 132, in stitch_text File "PIL\ImageDraw.py", line 373, in text File "PIL\ImageDraw.py", line 324, in draw_text File "PIL\ImageFont.py", line 667, in getmask2 OSError: invalid face handle
    stitch_text and above is my own functions, everything below is PIL.
    Any help is greatly appreciated.
    Andrew Murray
    We have an open issue for that error at python-pillow/Pillow#3066
    Yeah, I've read through that issue already. Is there a temporary fix I could do at all for this user? The program he's using is compiled to an executable using pyinstaller.
    And why does the issue only occur for him? Out of 50+ active users, he's the only one to encounter the issue.
    ^ That's a question of curiosity, not of necessity.
    Andrew Murray
    Looking through the issue, I think it boils down to a difference between the version of raqm built into the Pillow wheel and the version on the operating system. My suggestion would be for your user to install Pillow from source.
    Qian Hong
    Hi, what's the recommended way to detect an image size without loading the full image into memory using Pillow?
    ok i'm an idiot, I think Image.Open() does what i need
    thanks anyway
    Is there a way to make an image fit in a given file size?
    like get the maximum height/width for a given fiel size
    Andrew Murray
    Interesting question.
    I presume you have a large image, and are looking to scale it down? If so, here's a rough solution.
    import io, os
    from PIL import Image
    def resize(im, factor):
        return im.resize((int(im.width * factor), int(im.height * factor)), Image.ANTIALIAS)
    def getsize(im, factor):
        im_resized = resize(im, factor)
        # Save to memory, rather than disk, and check size
        with io.BytesIO() as fp:
            im_resized.save(fp, "PNG")
            return fp.getbuffer().nbytes
    def scale_down_to_size(im, destination_size):
        size = getsize(im, 1)
        if size < destination_size:
            print("Already small enough: "+str(size))
            return im
        # Binary search
        factor = 0.5
        gap = 0.5
        while gap > 0.0005:
            gap *= 0.5
            size = getsize(im, factor)
            if size > destination_size:
                factor -= gap
                factor += gap
        if size > destination_size:
            factor -= gap
            size = getsize(im, factor)
        print("Final size: "+str(size))
        return resize(im, factor)
    with Image.open("Tests/images/hopper.png") as im:
        im = scale_down_to_size(im, 500 * 1024) # 500 kb
    Frankie Dintino
    Hi! I've been waiting for feedback on #5201 for a while. I'm not complaining, I know how it is maintaining an open source project, and it's a pretty sizable change. I was wondering if there is anything I can do to make it easier to review, or to help out in any other way. Thanks.