Coder Social home page Coder Social logo

Comments (12)

DannyAziz avatar DannyAziz commented on August 28, 2024

Hey, so AWS lambda comes with built-in unless you need Imagemagick 7 or another delegate

from imagemagick-aws-lambda-2.

WaterKnight1998 avatar WaterKnight1998 commented on August 28, 2024

Hey, I read that. I decided to create my own layer as follows.

  1. Launched an EC2 Tiny with Ubuntu server 18.04.
  2. I executed the following commands:
    cd $HOME mkdir -p temp/python cd temp/python pip install Wand -t . cd .. zip -r9 ../wand.zip .
  3. I uploaded this zip as a layer.

Then when I run next lambda code:

import json
import boto3
from PyPDF2 import PdfFileReader, PdfFileWriter
import io
import os
from wand.image import Image


def lambda_handler(event, context):

    s3 = boto3.client('s3')
    
    #Accedo a un fichero del s3, lo leeo entero y lo transformo en un objeto de tipo BytesIO para que lo pueda leer el pdf File reader
    stream = io.BytesIO(s3.get_object(Bucket=os.environ["bucket"], Key=os.environ["rutaArchivo"])['Body'].read())
    
    pdf = PdfFileReader(stream)
    pages = pdf.getNumPages()
    print("number of pages: %i" % pages)
    
    for i in range(pdf.getNumPages()):
        
        output = PdfFileWriter()
        output.addPage(pdf.getPage(i))
        
        tmp = io.BytesIO()
        output.write(tmp)
        tmp.seek(0)
        
        img = Image(file = tmp, resolution = 72)
        img.convert("png")
        
        nombre="paginas/pagina"+str(i)+".png"
        s3.upload_fileobj(img, os.environ["bucket"],nombre )
    
    return {
        'statusCode': 200,
        'body': json.dumps(pages)
    }

As you can see, I was trying to split a pdf from a S3 bucket into pages in PNG format

I get this error:

Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 29, in lambda_handler
    img = Image(file = tmp, resolution = 72)
  File "/opt/python/wand/image.py", line 8207, in __init__
    self.read(file=file, resolution=resolution, units=units)
  File "/opt/python/wand/image.py", line 8686, in read
    self.raise_exception()
  File "/opt/python/wand/resource.py", line 240, in raise_exception
    raise e
wand.exceptions.MissingDelegateError: no decode delegate for this image format `' @ error/blob.c/BlobToImage/365

END RequestId: 8a4b5fee-721e-4ab7-b882-c8dd8dd786f0
REPORT RequestId: 8a4b5fee-721e-4ab7-b882-c8dd8dd786f0	Duration: 2681.02 ms	Billed Duration: 2700 ms 	Memory Size: 128 MB	Max Memory Used: 101 MB	

So as I saw in a website it looks like no Imagemagick was provided by AWS Lambda.

What could be causing this error?

from imagemagick-aws-lambda-2.

DannyAziz avatar DannyAziz commented on August 28, 2024

So the error you are getting is an issue with the PDF format not being supported

In my opinion, the imagemagick that's bundled into Lambda is the one that is being called with your code

from imagemagick-aws-lambda-2.

DannyAziz avatar DannyAziz commented on August 28, 2024

@stojanovic @simalexan It would be cool if there was a version of this layer that gave a way for us to use ImageMagick's API's with tools like Wand

from imagemagick-aws-lambda-2.

WaterKnight1998 avatar WaterKnight1998 commented on August 28, 2024

So the error you are getting is an issue with the PDF format not being supported

In my opinion, the imagemagick that's bundled into Lambda is the one that is being called with your code

What can I do for solving the problem? Any idea?

from imagemagick-aws-lambda-2.

DannyAziz avatar DannyAziz commented on August 28, 2024

So you need to compile ImageMagick into static binaries and then you need to point wand to those static binaries by using MAGICK_HOME

However, I have been trying this the last few days and have not really got anywhere...

from imagemagick-aws-lambda-2.

WaterKnight1998 avatar WaterKnight1998 commented on August 28, 2024

I haved read that too.
I have tried right now the next code:

with Image() as pdf:
        pdf.format='pdf'
        pdf.read(blob=stream)
    #pdf = Image(format="pdf",blob=stream,resolution=300)
        pdfImage = pdf.convert("jpeg")
        i=1
        for img in pdfImage.secuence:
            page = Image(image=img)
            
            tmp = io.BytesIO()
            tmp.seek(0)
            page.save(file=tmp)
            
            nombre="paginas/pagina"+str(i)+".jpeg"
            s3.upload_fileobj(tmp, os.environ["bucket"],nombre )

And throws me the next error:

Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 37, in lambda_handler
    pdf.format='pdf'
  File "/opt/python/wand/image.py", line 1733, in format
    raise ValueError(repr(fmt) + ' is unsupported format')
ValueError: 'pdf' is unsupported format

END RequestId: 81892afc-a49d-4634-9d99-385ef21b0a93
REPORT RequestId: 81892afc-a49d-4634-9d99-385ef21b0a93	Duration: 2367.02 ms	Billed Duration: 2400 ms 	Memory Size: 128 MB	Max Memory Used: 100 MB	

I have read in Imagemagick website that it needs ghostscript for reading pdfs.

EDIT

I Executed next command on aws lambda:
identify -list format
Which list formats accepted by Imagemagick in that system. PDF format is not present!

from imagemagick-aws-lambda-2.

gojko avatar gojko commented on August 28, 2024

@WaterKnight1998 this version of imagemagick does not come with ghostscript, and imagemagick needs ghostscript for PDF support. you will need to modify the makefile to compile and include ghostscript libraries if you want pdf support. alternatively, skip over imagemagick, wand and the whole stack and just install ghostscript directly to your Lambda, then convert PDFs using a subprocess

from imagemagick-aws-lambda-2.

WaterKnight1998 avatar WaterKnight1998 commented on August 28, 2024

@WaterKnight1998 this version of imagemagick does not come with ghostscript, and imagemagick needs ghostscript for PDF support. you will need to modify the makefile to compile and include ghostscript libraries if you want pdf support. alternatively, skip over imagemagick, wand and the whole stack and just install ghostscript directly to your Lambda, then convert PDFs using a subprocess

How can i include ghostscript libraries on aws lambda? I found layer for nodejs. However i need it for python3!

What should i modify from ImageMagick´s makefile to include ghostscript?

from imagemagick-aws-lambda-2.

gojko avatar gojko commented on August 28, 2024

the layer for nodejs will work for python as well, it's just a linux binary

from imagemagick-aws-lambda-2.

WaterKnight1998 avatar WaterKnight1998 commented on August 28, 2024

So, which layers do i need to make imagemagick work with ghostscript?

How can i use a subprocess to invoke ghostscript? Can you show me any example, pls?

from imagemagick-aws-lambda-2.

gojko avatar gojko commented on August 28, 2024

@WaterKnight1998 i'm posting this so you know I'm not ignoring you, but you're asking for something requiring far more time than I have to field community github requests. maybe someone else has more time to help, sorry

from imagemagick-aws-lambda-2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.