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
    @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
    @aahnik

    @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

    image.png
    the watermark, had a transparent background
    but after putting it on the gif, its background became white
    Aahnik Daw
    @aahnik
    it is working fine, except colors getting distorted
    how can i fix this ?
    Andrew Murray
    @radarhere
    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
    Jartan
    @JartanFTW
    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
    @radarhere
    We have an open issue for that error at python-pillow/Pillow#3066
    Jartan
    @JartanFTW
    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.
    Jartan
    @JartanFTW
    ^ That's a question of curiosity, not of necessity.
    Andrew Murray
    @radarhere
    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
    @fracting
    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
    Mattlau04
    @Mattlau04
    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
    @radarhere
    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
            else:
                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
        im.save("out.png")
    Frankie Dintino
    @fdintino
    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.
    ovrays
    @ovrays
    Hi there, when I'm trying to save image as webp (larger than 9mb) I getting an error, anyone can help me?
    Error: https://hastebin.com/ecokilaray.sql
    Code: https://hastebin.com/oteteyexus.lua
    Andrew Murray
    @radarhere

    You might running into https://github.com/python-pillow/Pillow/issues/5461#issuecomment-831114416 - error code 6 from webp, PARTITION0_OVERFLOW - "partition is bigger than 512k"

    Without a copy of your image, I can't say for sure. The next version of Pillow due out in a month will help this a bit, by adding in an error code alongside 'encoding error'.

    ovrays
    @ovrays
    Thank you for response. Changing method to 4 and reducing image quality didn't resolve problem.
    Is there any other way to fix error?
    Andrew Murray
    @radarhere
    Are you able to share the image?
    phillmac
    @phillmac
    This seems like a false positive from windows defender, anybody else reporting similar issues?
    image.png
    Specifically downloading this file will tigger defender, as well as the release tarball.
    I filled a false positive report with micro$oft but in the meantime it's a real nuisance
    anybody got any ideas about what's in that file thats setting it off??
    Andrew Murray
    @radarhere
    The file is a reproduction of a previous CVE - we add it to the repository so that we test it and make sure it stays fixed. So I wouldn't say it's exactly a false positive, it's just only a security problem for Pillow < 8.2.0.
    If you'd like, we can move the file out of the main repository and into a separate one. That's how we've dealt with similar reports in the past.
    phillmac
    @phillmac
    Yeah no clue why it's got defender's knickers in a knot, much less why it's reporting it as a trojan type threat
    But if getting it out of the release tarball fixes the problem then :thumbsup:
    Ryan Laursen
    @RLaursen
    Hello! Lately I've been figuring out why Pillow handles some GIFs poorly, and I've got it pretty much all figured out. That said there are many different ways to fix these issues, all with compromises, and I'm not sure which to pursue.
    recent updates to how GIFs were handled compounded the issue, as disposal methods 0/1 now leave behind a portion of the image8 array where the current frame is transparent, but if the current frame has a different color table (palette), the indices left in image8 will display the wrong colors
    now, generally, GIF frames are meant to be loaded individually and the disposal method signals the app displaying the frames regarding how to handle them as they're loaded... this would mean not stacking frames as a decoder, and returning frames independently. This would be the cleanest fix, but might confuse some users even more than the current implementation already does.
    I think the best compromise would be keeping colors from the previous frame that exist in the current frame and making colors that don't exist transparent, as these will be visible when the GIF is displayed
    Ryan Laursen
    @RLaursen
    I have several ideas regarding how to achieve this, but I wanted feedback about whether that'd be satisfactory behavior for GifImageFiles
    nothing would change except in the currently bugged case of disposal methods 0/1 and subsequent frames with local color tables which differ from previous frames' color tables
    Ryan Laursen
    @RLaursen
    another option is a utility function or method which extracts frames independently without applying disposal for use with problem GIF files
    Andrew Murray
    @radarhere
    @phillmac ok, I've started the ball rolling with python-pillow/pillow-depends#39
    Ryan Laursen
    @RLaursen
    @radarhere I responded trying to explain why that example gif itself isn't handled correctly at the moment
    phillmac
    @phillmac
    @radarhere Much appreciated