本帖最后由 zoenbo 于 2021-3-16 21:29 编辑
这个代码是PNG转JPG,转了的JPG是灰度的,我不想要灰度的,只想保留原图色彩,最高的质量,我该怎么改呢?
[Python] 纯文本查看 复制代码
import cv2
import img2pdf
import numpy as np
from PIL import Image
def png2jpg(file_name, binary_threshold=95, quality=95):
img = cv2.imdecode(np.fromfile(f'{file_name}.png', dtype=np.uint8), -1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
retval, dst = cv2.threshold(gray, binary_threshold, 255, cv2.THRESH_BINARY)
binim = cv2.bitwise_not(dst)
Image.fromarray(binim).save(f'{file_name}.jpg', quality=quality)
return f'{file_name}.jpg'
def jpg2pdf(file_name, jpg_name_list):
file_name = f'{file_name}.pdf'
with open(file_name, 'wb') as f:
pdf_bytes = img2pdf.convert(jpg_name_list)
f.write(pdf_bytes)
print(f'Converted to pdf at {file_name}') |