Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PIL生成图像的像素化_Python_Opencv_Image Processing_Python Imaging Library_Qr Code - Fatal编程技术网

Python PIL生成图像的像素化

Python PIL生成图像的像素化,python,opencv,image-processing,python-imaging-library,qr-code,Python,Opencv,Image Processing,Python Imaging Library,Qr Code,我正在尝试从生成的图像生成PDF文件。生成的PDF文件在放大时具有高水平的像素化,这会在打印过程中产生阴影 从PDF放大的qrcode图像 显示qrcode模块和像素周围的灰色区域(灰色),否则应为白色。所需的_分辨率是否匹配或低于创建原始图像时的分辨率并不重要 问题和可能的解决方案是什么 qr_size_mm = 8 MM2PX_FACTOR = 94.48 # 2400 dpi def create_code(prefix, number, postfix=None): mes

我正在尝试从生成的图像生成PDF文件。生成的PDF文件在放大时具有高水平的像素化,这会在打印过程中产生阴影

从PDF放大的qrcode图像

显示qrcode模块和像素周围的灰色区域(灰色),否则应为白色。所需的_分辨率是否匹配或低于创建原始图像时的分辨率并不重要

问题和可能的解决方案是什么

qr_size_mm = 8
MM2PX_FACTOR = 94.48  # 2400 dpi

def create_code(prefix, number, postfix=None):
    message = prefix + str(number)
    message += postfix if postfix is not None else ""
    series_qrcode = pyqrcode.create(message, error='H', version=3, mode='binary')
    # print(series_qrcode.get_png_size())
    binary = BytesIO()
    desired_scale = int(qr_size_px / series_qrcode.get_png_size())
    series_qrcode.png(binary, scale=desired_scale, module_color=(0, 0, 0),
                      background=(255, 255, 255), quiet_zone=3)

    tmpIm = Image.open(binary).convert('RGB')
    qr_dim = tmpIm.getbbox()
    # print(qr_dim)
    return tmpIm, qr_dim

qr_size_px = int(qr_size_mm * MM2PX_FACTOR)
# create A4 canvas
paper_width_mm = 210
paper_height_mm = 297
start_offset_mm = 10
start_offset_px = start_offset_mm * MM2PX_FACTOR

canvas_width_px = int(paper_width_mm * MM2PX_FACTOR)
canvas_height_px = int(paper_height_mm * MM2PX_FACTOR)
pil_paper_canvas = Image.new('RGB', (canvas_width_px, canvas_height_px), (255, 255, 255))

# desired pixels for 1200 dpi print
required_resolution_px = 94.48  # 47.244     # 23.622
required_resolution = 2400

print("Page dimension {page_width} {page_height} offset {offset}".format(page_width=canvas_width_px, page_height=canvas_height_px, offset=start_offset_px))
start_range = 10000100000000
for n in range(0, 5):
    print("Generating ", start_range+n)
    qr_image, qr_box = create_code("TLTR", number=start_range+n)
    # qr_image.show()
    print("qr_box ", qr_box)
    qr_x = int(start_offset_px + ((n+1) * qr_box[2]))
    qr_y = int(start_offset_px)
    print("pasting at ", qr_x, qr_y)
    pil_paper_canvas.paste(qr_image, (qr_x, qr_y))

    # create a canvas just for current qrcode
    one_qr_canvas = Image.new('RGB', (int(10*MM2PX_FACTOR), int(10*MM2PX_FACTOR)), (255, 255, 255))
    qrXY = int((10*MM2PX_FACTOR - qr_box[2]) / 2)
    one_qr_canvas.paste(qr_image, (qrXY, qrXY))
    one_qr_canvas = one_qr_canvas.resize((int(qr_size_mm*required_resolution_px),
                                          int(qr_size_mm*required_resolution_px)))
    one_qr_canvas.save(form_full_path("TLTR"+str(start_range+n)+".pdf"), dpi=(required_resolution, required_resolution))


pil_paper_canvas = pil_paper_canvas.resize((int(paper_width_mm*required_resolution_px),
                                            int(paper_height_mm*required_resolution_px)))
# pil_paper_canvas.show()
pil_paper_canvas.save(form_full_path("TLTR_qr_A4.pdf"), dpi=(required_resolution, required_resolution))

我合并了3项更改以修复/解决该问题:

  • 切换到比例(m*n),而不是指定用于调整大小的固定数字
  • 按照@physicalattraction的建议,使用lineType=cv2.LINE_AA进行抗锯齿

  • 还有一个问题没有解决,那就是PIL生成的PDF@96dpi不适合打印。无法找到使用打印就绪PDF(PDF/A或这些行上的其他内容)的选项。因此,我们转而生成PNG以生成高质量的QRCODE。

    如果您对此有任何评论或需要寻找的方向,我们将不胜感激。谢谢。对我来说,看起来像是PDF格式固有的抗锯齿,但我不是这方面的专家。感谢您对@physicalattraction的评论。尝试将大小从固定大小更改为比例/因子,但没有帮助。然后意识到PDF固有地以96dpi的速度保存,而这正是弄乱我的打印的原因。您的
    resize()
    命令是否有选项使用“最近邻”采样而不是双线性或双三次采样?@MarkSetchell是的,它(PIL)有,当前使用双线性。对于从二进制到PIL的qrcode,我正在放大图像,在后期保存时,我可能会根据MM2PX_因子保留比例或缩小比例。我意识到最大的问题是保存PDF文件,这会大大降低使用PIL的图像分辨率(DPI)。我还没有找到在PIL中保存打印就绪PDF的方法。