title: How do apps like Discord convert clipboard bitmaps to PNG?
body: <p>I am currently working on a small project to resize images over 8MB (Discord file upload limit) to be just under 8MB so I don't have to manually. I've noticed Discord seems to convert all clipboard bitmaps to PNGs, so I convert my current bitmap to PNG as well and save the picture to get the file size. I've tried multiple different resampling algorithms but settled on Nearest as it gave me the largest size so I could resize for the worst case.</p>
  <p>If the image is too big, resize it by taking the ratio of the current file size to 8MB. This would successfully change the picture to be under 8MB on disk and in memory. The weird part was I could manually click and drag the image into Discord and it would upload, but if I sent the image back to the clipboard and paste it then I would usually be told the image was still too large. I also saw images that were too big for Discord, but my program would detect they were under 8MB.</p>
  <p>This is my Python3 code to check image size (using PIL):</p>
  <pre><code>def get_size_saved(img):
      img.save(temp_file)
      if os.path.isfile(temp_file):
          return round(os.stat(temp_file).st_size / MB, 2)
  
  img = ImageGrab.grabclipboard()
  res = img.resize(img.size, resample=Image.NEAREST)
  size = get_size_saved(res)
  </code></pre>
  <p>Then send the image to the clipboard as a BMP again (using win32clipboard):</p>
  <pre><code>def send_image_to_clipboard(img):
      with BytesIO() as output:
          img.convert(&quot;RGB&quot;).save(output, &quot;BMP&quot;)
          data = output.getvalue()[14:]
  
      win32.OpenClipboard()
      win32.EmptyClipboard()
      win32.SetClipboardData(win32.CF_DIB, data)
      win32.CloseClipboard()
  </code></pre>