prthm11 commited on
Commit
f193879
·
verified ·
1 Parent(s): bff294b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -2095,17 +2095,27 @@ def similarity_matching(sprites_data: str, project_folder: str) -> str:
2095
  # # logger.info(f"🎉 Final project saved: {project_json_path}")
2096
  # return project_json_path
2097
 
2098
- def convert_bytes_to_image(pdf_bytes: bytes, dpi: int):
2099
- images = convert_from_bytes(pdf_bytes, dpi=dpi, poppler_path=poppler_path)
2100
- # Save each page to an in-memory BytesIO and return a list of BytesIOs
2101
- buffers = []
2102
- for img in images:
2103
- buf = BytesIO()
2104
- img.save(buf, format="PNG")
2105
- buf.seek(0)
2106
- buffers.append(buf)
2107
- return buffers
2108
-
 
 
 
 
 
 
 
 
 
 
2109
 
2110
  def delay_for_tpm_node(state: GameState):
2111
  logger.info("--- Running DelayForTPMNode ---")
@@ -2347,9 +2357,14 @@ def process_pdf():
2347
  # img_b64 = base64.b64encode(img_bytes).decode("utf-8")
2348
  #image_paths = await convert_pdf_to_images_async(saved_pdf_path)
2349
 
2350
- images = convert_bytes_to_image(pdf_stream, dpi=250)
2351
- print("PDF converted to images:", images)
2352
-
 
 
 
 
 
2353
  #updating logic here [Dev Patel]
2354
  initial_state_dict = {
2355
  "project_json": project_skeleton,
 
2095
  # # logger.info(f"🎉 Final project saved: {project_json_path}")
2096
  # return project_json_path
2097
 
2098
+ # def convert_bytes_to_image(pdf_bytes: bytes, dpi: int):
2099
+ # images = convert_from_bytes(pdf_bytes, dpi=dpi, poppler_path=poppler_path)
2100
+ # # Save each page to an in-memory BytesIO and return a list of BytesIOs
2101
+ # buffers = []
2102
+ # for img in images:
2103
+ # buf = BytesIO()
2104
+ # img.save(buf, format="PNG")
2105
+ # buf.seek(0)
2106
+ # buffers.append(buf)
2107
+ # return buffers
2108
+ def convert_pdf_stream_to_images(pdf_stream: io.BytesIO, dpi=300):
2109
+ # Ensure we are at the start of the stream
2110
+ pdf_stream.seek(0)
2111
+
2112
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_pdf:
2113
+ tmp_pdf.write(pdf_stream.read())
2114
+ tmp_pdf_path = tmp_pdf.name
2115
+
2116
+ # Now use convert_from_path on the temp file
2117
+ images = convert_from_path(tmp_pdf_path, dpi=dpi)
2118
+ return images
2119
 
2120
  def delay_for_tpm_node(state: GameState):
2121
  logger.info("--- Running DelayForTPMNode ---")
 
2357
  # img_b64 = base64.b64encode(img_bytes).decode("utf-8")
2358
  #image_paths = await convert_pdf_to_images_async(saved_pdf_path)
2359
 
2360
+ # images = convert_bytes_to_image(pdf_stream, dpi=250)
2361
+ # print("PDF converted to images:", images)
2362
+
2363
+ if isinstance(pdf_stream, io.BytesIO):
2364
+ images = convert_pdf_stream_to_images(pdf_stream, dpi=300)
2365
+ else:
2366
+ images = convert_from_path(pdf_stream, dpi=300)
2367
+
2368
  #updating logic here [Dev Patel]
2369
  initial_state_dict = {
2370
  "project_json": project_skeleton,