by Lazy Sloth
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)
Convert PDF to Black and White (Grayscale)
Created: | Last Updated:
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
Replace 'your_server_link' with the link provided by Lazy after deployment
url = 'your_server_link/upload_pdf'
files = {'file': open('path_to_your_file.pdf', 'rb')}
response = requests.post(url, files=files)
if response.status_code == 200:
with open('grayscale_pdf.pdf', 'wb') as f:
f.write(response.content)
print("Downloaded grayscale PDF")
else:
print(f"Error: {response.status_code}")
`
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.
Technologies
![PDF](https://static.vecteezy.com/system/resources/previews/023/234/826/non_2x/pdf-icon-pdf-format-file-simple-flat-trendy-modern-style-for-your-website-design-logo-and-mobile-app-free-png.png)