1

Below I have some code that gets a .exe file and get the image from it and saves it as a .bmp file. Which is great but I need to saave the .bmp witht he original transparent background that the .exe file icon had. Is there a way to modify the code below to do that?

Code:

def image2(path):
    path = path.replace("\\", "/")
    icoX = win32api.GetSystemMetrics(win32con.SM_CXICON)
    icoY = win32api.GetSystemMetrics(win32con.SM_CXICON)

    large, small = win32gui.ExtractIconEx(path, 0)
    win32gui.DestroyIcon(small[0])

    hdc = win32ui.CreateDCFromHandle(win32gui.GetDC(0))
    hbmp = win32ui.CreateBitmap()
    hbmp.CreateCompatibleBitmap(hdc, icoX, icoX)
    hdc = hdc.CreateCompatibleDC()

    hdc.SelectObject(hbmp)
    hdc.DrawIcon((0,0), large[0])

    savePath = "Octo Organizer (Hybrid Edition)/Files/"
    #hbmp.SaveBitmapFile(hdc, savePath + "None.bmp")
    bmpinfo = dataBitMap.GetInfo()
image2("C/Users/None/Desktop/Mozilla.exe") #This is just a example file path.
Nima
  • 323
  • 4
  • 13
cdw100100
  • 192
  • 1
  • 13
  • I'm not sure if I remember correctly, but the first byte of a 32bit _.bmp_ is the _alpha_ (transparency) coefficient, (followed by regular _RGB_). – CristiFati Sep 01 '15 at 22:18
  • @CristiFati when i save the icon though it has a black background I want the same transparent background as the icon. – cdw100100 Sep 01 '15 at 22:30
  • Who says the bmp isn't transparent? Few viewers respect bmp transparency. Few programs save bmp files with alpha channels. Didn't you want a png anyway? – David Heffernan Sep 01 '15 at 22:59
  • @DavidHeffernan To be honest it does not matter to me what type of image file it is as the saved file has the same transparency as the icon instead of having a black background. – cdw100100 Sep 01 '15 at 23:03
  • @cdw100100: Again (sorry if I add confusion), I (it has been a while since I worked with _GDI_), As @David Heffernan said, the black bacground could come from the image itself - it's black (_000_ _RGB_) - or from the image viewer (which doesn't know how to handle transparency bits, so it shows them either black or gray, or ...). Take a look at the `.exe` file (resources) with [PE.Explorer](http://www.heaventools.com/overview.htm) and if it's not "_transparency_ compatible" then you'll have to do the translation yourself (either to _.png_ or 32bit _.bmp_). What is `win32ui`?(It's not `pywin32`) – CristiFati Sep 01 '15 at 23:36
  • @CristiFati it is a part of the pywin32 api. Is there a way to make my code do that? – cdw100100 Sep 02 '15 at 00:29
  • I'm sorry, you're right, I have my "own" _pywin32_ version where I stripped out most of _UI_ (or at least _win32ui_). As an answer to your question: yes (as a parenthesis: everyting __is__ possible), i'm noot sure if there are any specialized libraries, but definitely you could convert from one _.bmp (or whatever you have at this point)_ to another. – CristiFati Sep 02 '15 at 00:42
  • @CristiFati I just want it to look exactly like the icon. I know how to convert it to other formats. – cdw100100 Sep 02 '15 at 00:44
  • There are automated conversions that take place. Does _icon_ format support transparency? Didn't study it recently, but apparently **yes** (windows icons transparent zone is properly displayed). [Here's a closed question](http://stackoverflow.com/questions/7746016/ico-and-transparency). – CristiFati Sep 02 '15 at 01:30
  • Excuse me why don't you call `DestroyIcon(large[0])` ? – Mr_and_Mrs_D Feb 20 '17 at 22:04

1 Answers1

3

Recently I had the same problem. Here's the code will save image with an alpha-channel using the PIL:

write instead line

bmpinfo = dataBitMap.GetInfo()

it code:

from PIL import Image
bmpstr = hbmp.GetBitmapBits(True)
img = Image.frombuffer(
    'RGBA',
    (32,32),
    bmpstr, 'raw', 'BGRA', 0, 1
)

img.save('icon.png')

it works and with this extended decision.

Community
  • 1
  • 1
Vyaches
  • 401
  • 7
  • 20