import subprocess, os
from PIL import Image
IMG='/data/news/images/1104/'
dl={
 'social_hotspots_2.jpg':'https://img.ithome.com/newsuploadfiles/2026/11/0f9612f0-7382-458f-b503-f658b15a83c4.png',
 'social_hotspots_3.jpg':'https://img.ithome.com/newsuploadfiles/2026/11/9f506252-c4cc-4934-b3c2-e24b3527ba84.png',
 'social_hotspots_4.jpg':'https://inews.gtimg.com/news_bt/O8UZxS86Tu_dd2T-ESGJJBFI98Ii6SFfRutzd9VfkgLxoAA/1000',
}
for name,u in dl.items():
    tmp='/tmp/'+name+'.src'
    subprocess.run(['curl','-sL','--max-time','30','-e','https://www.ithome.com/','-A','Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Mobile/15E148 Safari/604.1','-o',tmp,u],check=False)
    sz=os.path.getsize(tmp) if os.path.exists(tmp) else 0
    print(name,sz, end=' ')
    if sz>5000:
        try:
            im=Image.open(tmp); print(im.size,im.mode,end=' ')
            if im.mode!='RGB':
                bg=Image.new('RGB',im.size,(255,255,255))
                if im.mode in ('RGBA','LA','P'):
                    im2=im.convert('RGBA'); bg.paste(im2,mask=im2.split()[-1]); im=bg
                else: im=bg
            w,h=im.size
            if w>1200: im=im.resize((1200,int(h*1200/w)),Image.LANCZOS)
            im.save(IMG+name,'JPEG',quality=88)
            print('SAVED',Image.open(IMG+name).size, os.path.getsize(IMG+name))
        except Exception as e: print('ERR',e)
    else: print('TOO SMALL')
