<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Diya Malhotra</title>
    <description>The latest articles on Forem by Diya Malhotra (@diya23).</description>
    <link>https://forem.com/diya23</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1356813%2Fee2ff663-e8c7-4b48-b615-eabcb2976a6e.png</url>
      <title>Forem: Diya Malhotra</title>
      <link>https://forem.com/diya23</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/diya23"/>
    <language>en</language>
    <item>
      <title>A Deep Dive into PDF Processing and Image Extraction</title>
      <dc:creator>Diya Malhotra</dc:creator>
      <pubDate>Sun, 17 Mar 2024 07:37:01 +0000</pubDate>
      <link>https://forem.com/diya23/a-deep-dive-into-pdf-processing-and-image-extraction-7bl</link>
      <guid>https://forem.com/diya23/a-deep-dive-into-pdf-processing-and-image-extraction-7bl</guid>
      <description>&lt;p&gt;Hey there, Feeling like you’re lost in a maze of static PDFs, with those embedded images taunting you from their digital prison? Well, fret no more! This guide is your key to unlocking the secrets of PDF processing and image extraction with the power of Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Beyond the Static: Unveiling the Potential of PDFs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We all know PDFs as reliable document carriers, but beneath their seemingly unassuming exterior lies a hidden world of potential. By harnessing the magic of Python libraries like PyMuPDF, we can transform these PDFs from static snapshots into dynamic sources of data and visuals.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Extracting the Visual Gems: Taming the Image Extraction Beast&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
Those captivating images embedded within PDFs? They hold valuable information just waiting to be unleashed. Extracting them programmatically, however, can feel like wrestling a digital dragon. But fear not, brave coders! We’ll break down the process of capturing, decoding, and processing these images using Python, turning you into an image extraction master.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introducing PyMuPDF: Your Gateway to PDF Mastery&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Think of PyMuPDF as your Excalibur for conquering the PDF realm. This versatile library empowers you to manipulate PDFs with ease. Through hands-on examples and clear explanations, we’ll demystify PyMuPDF’s capabilities, equipping you with the tools to navigate the complexities of PDF processing with confidence.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unlocking Real-World Applications: Beyond the Code&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This journey isn’t just about code snippets and tutorials. We’ll delve into the practical applications of PDF processing and image extraction. From streamlining document workflows to enhancing data analysis pipelines, the possibilities are endless. By embracing new approaches and leveraging Python’s flexibility, you can unlock a world of creative problem-solving in your coding endeavors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Adventure Begins: Embark on Your Python Quest!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As we reach the end of this exploration, the real adventure is just beginning! We encourage you to embark on your exciting journey into the realm of PDF processing and image extraction. Armed with newfound knowledge and a spirit of curiosity, the possibilities are limitless. Whether you’re a seasoned developer seeking to expand your skillset or a curious beginner venturing into uncharted territory, Python’s rich ecosystem offers a gateway to infinite possibilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ready to Code? Let’s Get Started!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s dive into some actual code! Here’s how to extract images and text from a PDF using PyMuPDF:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Image Extraction-&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import fitz
import io
import base64
from PIL import Image

def extract_images_from_pdf(pdf_path):
images = []
try:
pdf_document = fitz.open(pdf_path)
for page_num in range(len(pdf_document)):
page = pdf_document.load_page(page_num)
image_list = page.get_images(full=True)
for img_info in image_list:
xref = img_info[0]
base_image = pdf_document.extract_image(xref)
image_bytes = base_image[“image”]
img_pil = Image.open(io.BytesIO(image_bytes))
img_pil = img_pil.convert(‘RGB’)
img_byte_arr = io.BytesIO()
img_pil.save(img_byte_arr, format=’JPEG’)
img_base64 = base64.b64encode(img_byte_arr.getvalue()).decode(‘utf-8’)
images.append({‘page_number’: page_num, ‘image_data’: img_base64})
pdf_document.close()
except Exception as e:
print(f”Error: {str(e)}”)
return images

pdf_path = ‘path/to/your/pdf/document.pdf’
extracted_images = extract_images_from_pdf(pdf_path)
for image in extracted_images:
print(f”Page {image[‘page_number’]}: {image[‘image_data’][:50]}…”)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Text Extraction&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import fitz

def extract_text_from_pdf(pdf_path):
text = “”
try:
pdf_document = fitz.open(pdf_path)
for page_num in range(len(pdf_document)):
page = pdf_document.load_page(page_num)
text += page.get_text()
pdf_document.close()
except Exception as e:
print(f”Error: {str(e)}”)

pdf_path = ‘path/to/your/pdf/document.pdf’
extracted_text = extract_text_from_pdf(pdf_path)
print(extracted_text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;References:&lt;br&gt;
-PyMuPDF Documentation: &lt;a href="https://pymupdf.readthedocs.io/en/latest/"&gt;https://pymupdf.readthedocs.io/en/latest/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>pdf</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>2024: A Glimpse into the Future of Living with Technology</title>
      <dc:creator>Diya Malhotra</dc:creator>
      <pubDate>Sun, 17 Mar 2024 07:34:23 +0000</pubDate>
      <link>https://forem.com/diya23/2024-a-glimpse-into-the-future-of-living-with-technology-2kep</link>
      <guid>https://forem.com/diya23/2024-a-glimpse-into-the-future-of-living-with-technology-2kep</guid>
      <description>&lt;p&gt;While 2024 might not hold the same historical weight as other pivotal years, it marked a significant turning point in our relationship with technology. Advancements in several key areas, including Apple’s rumored Vision Pro VR headset, generative AI, and text-to-video technology, began to reshape the way we work, interact, and consume content.&lt;/p&gt;

&lt;p&gt;Immersive Experiences with Vision Pro:&lt;br&gt;
Apple’s much-anticipated Vision Pro VR headset promises to revolutionize how we experience the world. With its high-resolution displays and advanced tracking technology, Vision Pro could transform various sectors:&lt;/p&gt;

&lt;p&gt;Entertainment: Imagine indulging in truly immersive gaming experiences or attending virtual concerts where you feel like you’re part of the action.&lt;br&gt;
Education: Imagine attending virtual field trips to historical landmarks or exploring the human body in 3D, fostering deeper engagement and understanding.&lt;br&gt;
Work: Imagine collaborating with colleagues in virtual workspaces regardless of physical location, leading to increased productivity and global collaboration possibilities.&lt;br&gt;
However, concerns remain about the potential for motion sickness and the need for robust content creation for an impactful VR experience.&lt;/p&gt;

&lt;p&gt;The Rise of the Creative Machines: Generative AI and Text-to-Video:&lt;/p&gt;

&lt;p&gt;Beyond VR, 2024 witnessed significant advancements in AI, specifically in the realms of generative AI and text-to-video technology. These advancements promise to reshape the landscape of content creation:&lt;/p&gt;

&lt;p&gt;Generative AI: Tools like Apple’s Runway allow users to create new content based on text prompts. This empowers individuals to generate marketing materials, social media posts, or even personalized artwork with relative ease.&lt;br&gt;
Text-to-Video Technology: This technology, while still in its early stages, enables users to generate videos based on text descriptions. Imagine quickly creating explainer videos or product demos, potentially increasing content creation efficiency.&lt;br&gt;
However, concerns exist regarding the potential impact on jobs in creative fields like graphic design, copywriting, and video editing. Additionally, ensuring responsible use of these technologies to avoid the spread of misinformation is crucial.&lt;/p&gt;

&lt;p&gt;A New Era for Content Creation: Adaptation and Opportunity&lt;/p&gt;

&lt;p&gt;While generative AI and text-to-video technology might automate certain aspects of content creation, they are unlikely to completely replace human involvement. Instead, they are more likely to:&lt;/p&gt;

&lt;p&gt;Shift the skillset requirements: The focus will likely shift towards higher-level creative thinking, expertise in utilizing these new tools, and the ability to curate and edit AI-generated content.&lt;br&gt;
Create new job opportunities: New roles will emerge in areas like developing, maintaining, and managing these AI tools, ensuring responsible content creation, and designing user experiences for these platforms.&lt;/p&gt;

&lt;p&gt;Therefore, the impact on content creation will likely be multifaceted. While some jobs might evolve or even become obsolete, new opportunities will arise, requiring adaptation and upskilling within the workforce.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Streamlining PDF: Merging PDF Pages into One Seamless Page</title>
      <dc:creator>Diya Malhotra</dc:creator>
      <pubDate>Sat, 16 Mar 2024 07:12:13 +0000</pubDate>
      <link>https://forem.com/diya23/streamlining-pdf-merging-pdf-pages-into-one-seamless-page-1kgn</link>
      <guid>https://forem.com/diya23/streamlining-pdf-merging-pdf-pages-into-one-seamless-page-1kgn</guid>
      <description>&lt;p&gt;In various scenarios, such as data analysis, report generation, or document management, you might need to merge multiple PDF pages into a single, continuous page. While there are paid solutions available, leveraging free and open-source libraries can achieve the same result. &lt;br&gt;
In this article, we'll explore a Python-based approach using the PyPDF3 library to merge PDF pages into one long page without the need for paid libraries.&lt;/p&gt;

&lt;p&gt;Why Merge PDFs into One Long Page?&lt;/p&gt;

&lt;p&gt;Merging PDF pages into a single, extended page offers several advantages:&lt;/p&gt;

&lt;p&gt;**1. Simplified Viewing: **It provides a seamless viewing experience by eliminating the need to navigate through multiple pages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Enhanced Analysis:&lt;/strong&gt; For data-intensive documents, consolidating all information onto one page can facilitate comprehensive analysis.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Presentation Purposes:&lt;/strong&gt; A single-page PDF is ideal for presentations or sharing visualizations that span across multiple pages.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Streamlined Data Extraction:&lt;/strong&gt; With all PDF pages merged into one elongated page, the consistent coordinate system simplifies data extraction tasks. This facilitates efficient image extraction, text recognition, and other analytical processes, enhancing automation and accuracy in document processing workflows.&lt;/p&gt;

&lt;p&gt;We'll utilize the PyPDF3 library, a powerful Python tool for manipulating PDF files. &lt;/p&gt;

&lt;p&gt;The approach involves:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reading the input PDF file.&lt;/li&gt;
&lt;li&gt;Calculating the total height required for the merged page.&lt;/li&gt;
&lt;li&gt;Creating a new, blank page with increased height.&lt;/li&gt;
&lt;li&gt;Placing the content of each page onto the merged page, and adjusting the vertical position accordingly.&lt;/li&gt;
&lt;li&gt;Writing the merged PDF to an output file.
Implementation:
Let's dive into the code snippet to demonstrate how to merge PDF pages into one big page using PyPDF3:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import PyPDF3
def merge_pages(input_pdf_path, output_pdf_path):
 with open(input_pdf_path, 'rb') as input_pdf:

 pdf_reader = PyPDF3.PdfFileReader(input_pdf)
 pdf_writer = PyPDF3.PdfFileWriter()
 first_page = pdf_reader.getPage(0)

total_height = sum(page.mediaBox.getUpperRight_y() for page in pdf_reader.pages)
merged_page = PyPDF3.pdf.PageObject.createBlankPage(width=first_page.mediaBox.getUpperRight_x(), height=total_height)

current_y = 0
for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
page_height = page.mediaBox.getUpperRight_y()

merged_page.mergeTranslatedPage(page, 0, total_height - current_y - page_height)

current_y += page_height

pdf_writer.addPage(merged_page)

with open(output_pdf_path, 'wb') as output_pdf:
pdf_writer.write(output_pdf)

merge_pages('/content/page.pdf', 'output.pdf')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We open the input PDF file and create a PDF reader object.&lt;/li&gt;
&lt;li&gt;The height of the merged page is calculated by summing up the heights of all pages in the input PDF.&lt;/li&gt;
&lt;li&gt;A new blank page is created with the calculated height.&lt;/li&gt;
&lt;li&gt;Each page from the input PDF is placed onto the merged page, adjusting the vertical position.&lt;/li&gt;
&lt;li&gt;The merged PDF is then written to an output file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By leveraging PyPDF3, we can merge multiple PDF pages into a single, continuous page without resorting to paid solutions. This approach provides a cost-effective and straightforward method for handling PDF manipulation tasks. Whether for data analysis, presentations, or document management, merging PDF pages into one big page offers practical benefits and streamlines various workflows. Try out this approach in your projects to simplify PDF handling and enhance user experience.&lt;/p&gt;

</description>
      <category>python</category>
      <category>pdf</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
