Convert PDF to Black and White (Grayscale)

 
import logging
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import HTMLResponse, StreamingResponse
from pydantic import BaseModel
import fitz  # PyMuPDF
from PIL import Image, ImageOps
import io

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI()

def convert_pdf_to_grayscale(pdf_stream):
    try:
        pdf = fitz.open("pdf", pdf_stream)  # Specify the filetype when opening the stream
        gray_pdf = fitz.open()  # Create a new PDF to store grayscale pages
        for page_number in range(len(pdf)):
            page = pdf[page_number]
            pix = page.get_pixmap()
            # Convert to grayscale
            gray_pix = fitz.Pixmap(pix, 0) if pix.alpha else fitz.Pixmap(fitz.csGRAY, pix)

About this template

An app that converts uploaded PDF files to black and white (grayscale) and allows for easy downloading of the converted file.

Introduction to the PDF to Grayscale Conversion Template

Welcome to the Lazy template guide for converting PDF files to grayscale. This template provides a simple and efficient way to upload a PDF file and receive a converted grayscale version. It's perfect for builders looking to integrate PDF processing capabilities into their applications without worrying about the complexities of deployment and environment setup.

Getting Started with the Template

To begin using this template, click on "Start with this Template" in the Lazy builder interface. This will pre-populate the code in your Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.

Test: Deploying the App

Once you have the template loaded, press the "Test" button to start the deployment of your app. The Lazy CLI will handle the deployment process, and you won't need to install any libraries or set up your environment. The deployment process is fully managed by Lazy.

Using the App

After the deployment is complete, Lazy will provide you with a dedicated server link. You can use this link to access the FastAPI server that hosts your PDF to grayscale conversion service. Additionally, Lazy will provide a link to the FastAPI documentation, where you can find more details about the API endpoints and how to interact with them.

The main page of your app will serve as a simple user interface for uploading PDF files. Here's how to use it:

  • Open the provided server link in your web browser to view the main page.
  • You will see a form where you can upload a PDF file.
  • Select a PDF file from your device and click on "Convert to Grayscale".
  • Once the file is processed, a download link will appear, allowing you to save the converted grayscale PDF to your device.

Integrating the App

If you wish to integrate this PDF conversion service into another application or tool, you can use the API endpoints provided by the FastAPI server. Here's a sample request you might use to interact with the API programmatically:


import requests<br>
<br>
# Replace 'your_server_link' with the link provided by Lazy after deployment<br>
url = 'your_server_link/upload_pdf'<br>
files = {'file': open('path_to_your_file.pdf', 'rb')}<br>
<br>
response = requests.post(url, files=files)<br>
<br>
if response.status_code == 200:<br>
    with open('grayscale_pdf.pdf', 'wb') as f:<br>
        f.write(response.content)<br>
    print("Downloaded grayscale PDF")<br>
else:<br>
    print(f"Error: {response.status_code}")<br>

Remember to replace 'your_server_link' with the actual server link provided by Lazy and 'path_to_your_file.pdf' with the path to the PDF file you want to convert.

By following these steps, you can easily integrate the PDF to grayscale conversion service into your application, providing a valuable feature to your users with minimal effort.

Category
Technology
Last published
July 26, 2024

More templates like this

Convert PDF or WORD files to EXCEL files

PDF

GPT-4 Exam Generator

This app uses GPT-4 to generate a PDF of a new exam based on an uploaded PDF of a past exam.

PDF
OpenAI
Python

PDF Chatbot

The PDF Chatbot app connects to uploaded PDFs and answers questions about them using AI.

PDF
Home
/
Convert PDF to Black and White (Grayscale)