Photo ka background White kr do
cd /tmp/workspace && ls attachments/923c16bc-d7dc-4503-b558-d0f04e6c8b8c/
python3 << 'EOF' import fitz # PyMuPDF import os pdf_path = '/tmp/workspace/attachments/923c16bc-d7dc-4503-b558-d0f04e6c8b8c/photo_compress(1) (1).pdf' doc = fitz.open(pdf_path) print(f"Pages: {len(doc)}") for page_num in range(len(doc)): page = doc[page_num] print(f"Page {page_num}: size={page.rect}") images = page.get_images(full=True) print(f" Images: {len(images)}") for img in images: print(f" xref={img[0]}, name={img[7]}, width={img[2]}, height={img[3]}, colorspace={img[5]}") EOF
python3 << 'EOF' import pypdf from pypdf import PdfReader import io pdf_path = '/tmp/workspace/attachments/923c16bc-d7dc-4503-b558-d0f04e6c8b8c/photo_compress(1) (1).pdf' reader = PdfReader(pdf_path) print(f"Pages: {len(reader.pages)}") for i, page in enumerate(reader.pages): print(f"Page {i}: {page.mediabox}") # Try to get images if '/XObject' in page['/Resources']: xobjects = page['/Resources']['/XObject'].get_object() for name, obj in xobjects.items(): xobj = obj.get_object() print(f" XObject: {name}, Subtype: {xobj.get('/Subtype')}, W={xobj.get('/Width')}, H={xobj.get('/Height')}") EOF
python3 << 'EOF' from pypdf import PdfReader from PIL import Image import io, os pdf_path = '/tmp/workspace/attachments/923c16bc-d7dc-4503-b558-d0f04e6c8b8c/photo_compress(1) (1).pdf' reader = PdfReader(pdf_path) page = reader.pages[0] xobjects = page['/Resources']['/XObject'].get_object() obj = xobjects['/Im1'].get_object() data = obj.get_data() colorspace = obj.get('/ColorSpace') width = obj['/Width'] height = obj['/Height'] bpc = obj.get('/BitsPerComponent', 8) print(f"ColorSpace: {colorspace}, W={width}, H={height}, BPC={bpc}") print(f"Data length: {len(data)}") # Try to open as image try: img = Image.open(io.BytesIO(data)) print(f"Format: {img.format}, Mode: {img.mode}, Size: {img.size}") img.save('/tmp/workspace/extracted_photo.png') print("Saved as PNG") except Exception as e: print(f"Direct open failed: {e}") # Try raw RGB try: if colorspace == '/DeviceRGB': img = Image.frombytes('RGB', (width, height), data) elif colorspace == '/DeviceCMYK': img = Image.frombytes('CMYK', (width, height), data) elif colorspace == '/DeviceGray': img = Image.frombytes('L', (width, height), data) else: img = Image.frombytes('RGB', (width, height), data) img.save('/tmp/workspace/extracted_photo.png') print(f"Saved raw as PNG, mode={img.mode}, size={img.size}") except Exception as e2: print(f"Raw open failed: {e2}") EOF
pip install --user rembg 2>&1 | tail -5
python3 -c "from rembg import remove; print('rembg ok')"
pip install rembg 2>&1 | tail -5
python3 << 'EOF' from rembg import remove from PIL import Image import io # Open the extracted photo input_img = Image.open('/tmp/workspace/extracted_photo.png') print(f"Input: {input_img.mode}, {input_img.size}") # Remove background (returns RGBA image with transparent bg) output_img = remove(input_img) print(f"Output (after rembg): {output_img.mode}, {output_img.size}") # Create white background and paste white_bg = Image.new('RGB', output_img.size, (255, 255, 255)) white_bg.paste(output_img, mask=output_img.split()[3]) # use alpha channel as mask # Save as JPEG (white background photo) white_bg.save('/tmp/workspace/photo_white_bg.jpg', 'JPEG', quality=95) print("Saved: /tmp/workspace/photo_white_bg.jpg") # Also save as PDF from reportlab.lib.pagesizes import A4 from reportlab.platypus import SimpleDocTemplate, Image as RLImage from reportlab.lib.units import cm # Save white bg photo as PNG first white_bg.save('/tmp/workspace/photo_white_bg.png', 'PNG') # Make PDF doc = SimpleDocTemplate('/tmp/workspace/photo_white_bg.pdf', pagesize=A4) rl_img = RLImage('/tmp/workspace/photo_white_bg.png', width=15*cm, height=19*cm) doc.build([rl_img]) print("Saved: /tmp/workspace/photo_white_bg.pdf") EOF
photo white bg
Image ยท JPG
photo white bg
Image ยท PNG
photo white bg
PDF Document ยท PDF
Thank you