Convert PDF to Black and White (Grayscale)

By
 
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
April 7, 2024

More templates like this

PDF Chatbot

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

PDF

Convert PDF to CSV File Format

An app that converts uploaded PDF files to CSV file format and allows for easy downloading of the converted file.

PDF

Convert PDF to Google Doc

This app allows users to upload PDF files and convert them into Google Docs. Users can upload a PDF file through a web interface, and the app will authenticate the user with Google. Once authenticated, the app will convert the PDF file into a Google Doc and save it to the user's Google Drive. The app provides a simple and user-friendly interface for uploading and converting PDF files.

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