Fix and Enhance ImageCompress

This commit is contained in:
2024-09-10 08:57:27 +08:00
parent c468188b95
commit 1982894b79
2 changed files with 144 additions and 8 deletions

View File

@ -15,17 +15,17 @@ def main(file:Path,targetPath:Path,TARGET_SIZE,MINSIZE,SINGLE):
print(f"{targetPath} file already exists. Check it.")
return None
inp=file.read_bytes()
proc=Popen(f"ffmpeg.exe -hide_banner -i - -vf \"scale=-1:1600:bilinear\" -f mjpeg -",stderr=PIPE,stdout=PIPE,stdin=PIPE)
out,_=proc.communicate(inp)
suffix=file.suffix[1:]
mapper={
"jpg":"mjpeg",
"jpeg":"mjpeg",
"png":"image2pipe",
"png":"image2pipe -vcodec png",
"webp":"webp",
"gif":"gif"
}
targetFmt=mapper.get(suffix,"mjpeg")
proc=Popen(f"ffmpeg.exe -hide_banner -i - -vf \"scale=-1:1600:bilinear\" -f {targetFmt} -",stderr=PIPE,stdout=PIPE,stdin=PIPE)
out,_=proc.communicate(inp)
if SINGLE:print(".",end="",flush=True)
else:cnt+=1
@ -57,11 +57,14 @@ def main(file:Path,targetPath:Path,TARGET_SIZE,MINSIZE,SINGLE):
out,err=proc.communicate(inp)
if SINGLE:print(".",end="",flush=True)
else:cnt+=1
if targetFmt=="webp":
proc=Popen(f"ffmpeg -hide_banner -i - {str(targetPath.resolve())}",stdin=PIPE,stderr=PIPE)
proc.communicate(out)
else:
targetPath.write_bytes(out)
# if targetFmt=="webp":
# proc=Popen(f"ffmpeg -hide_banner -i - {str(targetPath.resolve())}",stdin=PIPE,stderr=PIPE)
# proc.communicate(out)
# else:
print(suffix,mapper.keys())
if suffix not in mapper.keys():
targetPath=targetPath.with_suffix(".jpg")
targetPath.write_bytes(out)
if SINGLE:print(" Success")
else:return file,cnt