林释 发表于 2026-2-12 18:06

【小工具】图片转ico

本帖最后由 林释 于 2026-2-12 18:11 编辑

【工具需求:】转换一下ico文件,有时改下文件夹图标或者网址图标等 用得到
【使用方法:】拖入文件夹或者文件,即可转换。


         请将文件或文件夹拖放到exe程序上执行转换。
         或者使用py文件,转换单个文件: python <py文件路径> <图片路径>,转换整个文件夹: python <py文件路径> <文件夹路径>
【下载地址:】程序,打包的单文件所以比较大,也可以自行打包。

【注意:】exe软件全程没有提示,除非使用代码,打包只是方便网友。

通过网盘分享的文件:图片转ico.exe
链接: https://pan.baidu.com/s/1i9xJIHouS2jnwowpoL44vw?pwd=9qsb 提取码: 9qsb 复制这段内容后打开百度网盘手机App,操作更方便哦


from PIL import Image
import os
import sys

def convert_to_ico(image_path, sizes=[(256, 256), (128, 128), (64, 64), (32, 32), (16, 16)]):
    try:
      img = Image.open(image_path).convert('RGBA')
      icon_sizes = []
      for size in sizes:
            resized = img.resize(size, Image.LANCZOS)
            icon_sizes.append(resized)
      filename, _ = os.path.splitext(image_path)
      output_path = f"{filename}.ico"
      icon_sizes.save(
            output_path,
            format="ICO",
            sizes=,
            append_images=icon_sizes
      )
      
      print(f"已转换: {os.path.basename(output_path)}")
      return True
      
    except Exception as e:
      print(f"转换失败 {os.path.basename(image_path)}: {e}")
      return False

def process_path(path):
    supported_formats = ('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.webp')
   
    if os.path.isfile(path):
      if path.lower().endswith(supported_formats):
            convert_to_ico(path)
      else:
            print(f"不支持的格式: {os.path.basename(path)}")
            print(f"支持的格式: {', '.join(supported_formats)}")
   
    elif os.path.isdir(path):
      print(f"扫描文件夹: {path}\n")
      
      image_files = [
            f for f in os.listdir(path)
            if f.lower().endswith(supported_formats)
      ]
      
      if not image_files:
            print("未找到支持的图片文件")
            return
      
      print(f"找到 {len(image_files)} 个图片文件\n")
      
      success_count = 0
      for filename in image_files:
            file_path = os.path.join(path, filename)
            if convert_to_ico(file_path):
                success_count += 1
      
      print(f"转换完成: {success_count}/{len(image_files)} 成功")
   
    else:
      print(f"路径不存在: {path}")

if __name__ == "__main__":
    if len(sys.argv) > 1:
      input_path = sys.argv
      process_path(input_path)
    else:
      print("用法:")
      print("请将文件或文件夹拖放到此程序上执行转换。")
      print("或者使用py文件,转换单个文件: python script.py <图片路径>,转换整个文件夹: python script.py <文件夹路径>")

289 发表于 2026-2-13 14:16

本帖最后由 289 于 2026-2-15 07:56 编辑

我用DELPHI13 纯API 写了一个 才 130多行代码 即拖即用 方便快捷
我用夸克网盘分享了「图 TO ICO.exe」,点击链接即可保存。打开「夸克APP」,无需下载在线播放视频,畅享原画5倍速,支持电视投屏。
链接:https://pan.quark.cn/s/12e0de0a5581

program Image2Ico;

uses
Winapi.Windows,
Winapi.Messages,
Winapi.ShellAPI,
Winapi.GDIPAPI,
Winapi.GDIPOBJ,
Winapi.ActiveX, // 解决 CLSIDFromString zhihu.com/people/union-29
System.SysUtils;

var
hggg: HWND;
GdiplusToken: ULONG_PTR;
Gps: TGdiplusStartupInput;

// --- 核心转换逻辑 ---
function ProcessImageToIcon(const InFile: string): Boolean;
var
GPImg: TGPImage;
Bmp: TGPBitmap;
OutFile: string;
Graphics: TGPGraphics;
Clsid: TGUID;
begin
Result := False;
OutFile := ChangeFileExt(InFile, '.ico');

GPImg := TGPImage.Create(InFile);
try
    if GPImg.GetLastStatus <> Ok then Exit;

    // 创建 32位 ARGB 位图
    Bmp := TGPBitmap.Create(GPImg.GetWidth, GPImg.GetHeight, PixelFormat32bppARGB);
    try
      Graphics := TGPGraphics.Create(Bmp);
      try
      Graphics.DrawImage(GPImg, 0, 0, GPImg.GetWidth, GPImg.GetHeight);
      finally
      Graphics.Free;
      end;

      // 使用 BMP 编码器 CLSID 落地文件   zhihu.com/people/union-29
      if CLSIDFromString('{557cf400-1a04-11d3-9a73-0000f81ef32e}', Clsid) = S_OK then
      begin
      if Bmp.Save(OutFile, Clsid) = Ok then
      begin
          MessageBox(hggg, PChar('转换成功!' + #13#10 + OutFile), '提示', MB_OK);
          Result := True;
      end;
      end;
    finally
      Bmp.Free;
    end;
finally
    GPImg.Free;
end;
end;

// --- 窗口过程 ---
function WndProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
DropHandle: HDROP;
FileName: array of Char;
ps: TPaintStruct;
dddddddddddddc: HDC; // 您的专属命名   zhihu.com/people/union-29
begin
case Msg of
    WM_CREATE:
    begin
      DragAcceptFiles(hWnd, True);
      Result := 0;
    end;
    WM_DROPFILES:
    begin
      DropHandle := HDROP(wParam);
      if DragQueryFile(DropHandle, 0, FileName, MAX_PATH) > 0 then
      ProcessImageToIcon(FileName);
      DragFinish(DropHandle);
      Result := 0;
    end;
    WM_PAINT:
    begin
      dddddddddddddc := BeginPaint(hWnd, ps);
      SetBkMode(dddddddddddddc, TRANSPARENT);
      TextOut(dddddddddddddc, 20, 40, '请将图片拖放到此处转换 (.ico)', 21);
      EndPaint(hWnd, ps);
      Result := 0;
    end;
    WM_ERASEBKGND:
      Result := 1; // 双倍缓冲:阻止背景擦除   zhihu.com/people/union-29
    WM_DESTROY:
    begin
      PostQuitMessage(0);
      Result := 0;
    end;
    else
      Result := DefWindowProc(hWnd, Msg, wParam, lParam);
end;
end;

// --- 入口 ---
var
wc: Winapi.Windows.TWndClass; // 明确指定 Windows 单元,解决 E2010      zhihu.com/people/union-29
msgLoop: TMsg;
winW, winH: Integer;
begin
// DPI 自适应 V2
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);

GdiplusStartup(GdiplusToken, @Gps, nil);

winW := 400; winH := 160;
FillChar(wc, SizeOf(wc), 0);
wc.lpfnWndProc := @WndProc;
wc.hInstance := hInstance;
wc.hbrBackground := COLOR_BTNFACE + 1;
wc.lpszClassName := 'Delphi13_Image2Ico';
wc.hCursor := LoadCursor(0, IDC_ARROW);

Winapi.Windows.RegisterClass(wc);

// 居中逻辑,无闪烁创建         zhihu.com/people/union-29
hggg := CreateWindowEx(WS_EX_ACCEPTFILES or WS_EX_TOPMOST, wc.lpszClassName,
    '图片转 ICO 工具', WS_OVERLAPPED or WS_CAPTION or WS_SYSMENU or WS_VISIBLE,
    (GetSystemMetrics(SM_CXSCREEN) - winW) div 2,
    (GetSystemMetrics(SM_CYSCREEN) - winH) div 2,
    winW, winH, 0, 0, hInstance, nil);

while GetMessage(msgLoop, 0, 0, 0) do
begin
    TranslateMessage(msgLoop);
    DispatchMessage(msgLoop);
end;

GdiplusShutdown(GdiplusToken);
end.

cai2532 发表于 2026-2-12 22:36

搞成有界面的效果会更好

疯狂的小蜗牛521 发表于 2026-2-13 08:36

有源码搞UI很好弄了

yyx28033 发表于 2026-2-13 10:39

我去 这可太有用了收藏了收藏了

zjtzjt 发表于 2026-2-13 14:18

图标获取更方便了

zhengkejie 发表于 2026-2-13 15:53

289 发表于 2026-2-13 14:16
我用DELPHI13 纯API 写了一个 才 130多行代码 即拖即用 方便快捷




老铁,编译的传一个呗

289 发表于 2026-2-15 07:55

zhengkejie 发表于 2026-2-13 15:53
老铁,编译的传一个呗

好的兄弟 !

我用夸克网盘分享了「图 TO ICO.exe」,点击链接即可保存。打开「夸克APP」,无需下载在线播放视频,畅享原画5倍速,支持电视投屏。
链接:https://pan.quark.cn/s/12e0de0a5581

yzqdev 发表于 2026-2-15 13:12

好东西,我要抄一个csharp版本的{:1_918:}

sceptic 发表于 2026-2-18 08:49

可以自定义图标了
页: [1] 2
查看完整版本: 【小工具】图片转ico