def main(): parser = generate_parser() args = parser.parse_args() dpi = args.dpi print_size = (mm_to_px(args.width, dpi), mm_to_px(args.height, dpi)) images, image_size = prepare_images(parser, args.images, dpi) parts = collate_images(images, image_size, print_size, dpi) logging.debug('Printable paper size (width x height):') logging.debug('Pixels: {} x {}'.format(print_size[0], print_size[1])) logging.debug('Points: {} x {}'.format( img2pdf.px_to_pt(print_size[0], dpi), img2pdf.px_to_pt(print_size[1], dpi))) with tempfile.TemporaryDirectory() as tmp_dir: paths = [] for i, image in enumerate(parts): path = os.path.join(tmp_dir, '{:0>4}.jpg'.format(i)) paths.append(path) image.save(path) page_size = img2pdf.parse_pagesize_rectarg('{}mmx{}mm'.format( args.width, args.height)) image_size = img2pdf.parse_imgsize_rectarg('{}dpix{}dpi'.format( dpi, dpi)) layout_func = img2pdf.get_layout_fun(pagesize=page_size, imgsize=image_size) with open(args.output, 'wb') as fh: fh.write(img2pdf.convert(paths, layout_fun=layout_func))
def prepare_images(parser, image_paths, dpi): """Returns the images specified in `image_paths`, rotating them as needed for common alignment. If the images are not all the same size, or if there are an odd number of images, an error is raised and the script ends. """ if len(image_paths) % 2 != 0: parser.exit(status=1, message=UNEVEN_NUMBER_IMAGES_ERROR) images = [] image_size = None for image_path in image_paths: image = Image.open(image_path) size = image.size if image.height < image.width: image = image.rotate(90, expand=True) image = image.resize((size[1], size[0])) if image_size is None: image_size = image.size elif image.size != image_size: parser.exit(status=2, message=DIFFERENT_SIZE_IMAGES_ERROR) images.append(image) logging.debug('Image sizes (width x height):') logging.debug('Pixels: {} x {}'.format(image_size[0], image_size[1])) logging.debug('Points: {} x {}'.format( img2pdf.px_to_pt(image_size[0], dpi), img2pdf.px_to_pt(image_size[1], dpi))) return images, image_size
def images_5(request): if request.method == 'POST': with open( 'C:\\Users\\Khalid-Rami\\Desktop\\outputtzi.pdf', 'wb' ) as f: img = Image.open( 'C:\\Users\\Khalid-Rami\Desktop\\mywebsite\\static_file\\our_static\\images\\cinq.jpg' ) my_layout_fun = img2pdf.get_layout_fun( pagesize = ( img2pdf.px_to_pt( img.width, 96 ), img2pdf.px_to_pt( img.height, 96 ) ), fit = img2pdf.FitMode.into ) f.write( img2pdf.convert( [ 'C:\\Users\\Khalid-Rami\Desktop\\mywebsite\\static_file\\our_static\\images\\cinq.jpg','C:\\Users\\Khalid-Rami\Desktop\\mywebsite\\static_file\\our_static\\images\\deux.jpg', 'C:\\Users\\Khalid-Rami\Desktop\\mywebsite\\static_file\\our_static\\images\\trois.jpg', 'C:\\Users\\Khalid-Rami\Desktop\\mywebsite\\static_file\\our_static\\images\\quatre.jpg' ], layout_fun = my_layout_fun )) return render(request,"graphes.html")
def split_images(image1, image2, number_pieces, paper_size, dpi): splits = [] for i in range(number_pieces[0]): for j in range(number_pieces[1]): start_x = i * paper_size[0] start_y = j * paper_size[1] end_x = min((i + 1) * paper_size[0], image1.width) end_y = min((j + 1) * paper_size[1], image1.height) box = (start_x, start_y, end_x, end_y) part1 = image1.crop(box) part2 = image2.crop(box) splits.extend((part1, part2)) logging.debug('Image piece sizes (width x height):') logging.debug('Pixels: {} x {}'.format(part1.width, part1.height)) logging.debug('Points: {} x {}'.format( img2pdf.px_to_pt(part1.width, dpi), img2pdf.px_to_pt(part1.height, dpi))) return splits