예제 #1
0
    def converter(self):
        if not os.path.exists(self.path):
            os.mkdir(self.path)

        numero_de_paginas = len(self.arquivo.sequence)
        self.detalhes.append("Iniciando importação ...")

        try:
            for pagina in range(numero_de_paginas):
                nome_arquivo = "%s[%d]" % (self.arquivo_pdf, pagina)
                img = Image(filename=nome_arquivo, resolution=150)
                img.compression_quality = 99
                nome_arquivo = "pagina%d.jpg" % (pagina + 1)
                self.detalhes.append("\tPágina %d importada" % (pagina + 1))
                img.save(filename=self.path + nome_arquivo)
                self.lista_de_imagens.append(nome_arquivo)
                img.close()

            self.detalhes.append("Importação concluída.\n")
            time.sleep(1)
            self.arquivo.close()

        except BlobError:
            self.detalhes.append("Erro na importação!!!")
            time.sleep(1)
            self.detalhes.append("Fim.")
            self.arquivo.close()
            os.rmdir(self.path)
예제 #2
0
def convert_pdf_to_img(blob, img_type="jpg", quality=75, resolution=200):
    """
    Converts PDF with multiple pages into one image.
    It needs the file content NOT the filename or ioBytes and returns the image content.
    Note: It has memory leak!!
    http://stackoverflow.com/a/26233785/1497443

    Example:

    with open('my.pdf', "r") as f:
        file_content = f.read()

    # import ipdb
    # ipdb.set_trace()
    hh = convert_pdf_to_jpg(file_content)

    with open('my.jpg', 'wb') as f:
        f.write(hh)
    """
    from wand.image import Image as WandImage
    from wand.color import Color as WandColor

    pdf = WandImage(blob=blob, resolution=resolution)

    pages = len(pdf.sequence)

    wimage = WandImage(width=pdf.width, height=pdf.height * pages, background=WandColor("white"))

    for i in xrange(pages):
        wimage.composite(pdf.sequence[i], top=pdf.height * i, left=0)

    if img_type == "jpg":
        wimage.compression_quality = quality

    return wimage.make_blob(img_type)
예제 #3
0
def deepfry(img: Image):
    resize(img)
    img.format = "jpeg"
    img.compression_quality = 1
    img.modulate(saturation=700)

    return img
예제 #4
0
def save_file():
    event_uuid = str(uuid.uuid1())
    event_path = get_event_dir(event_uuid)

    if request.headers['Content-Type'].startswith('image/'):
        os.makedirs(event_path)

        img_filename = os.path.join(
            event_path, request.headers['Content-Type'].replace('/', '.'))

        with open(img_filename, 'wb') as img_file:
            img_bytes = BytesIO(request.data)
            img_file.write(img_bytes.read())
    elif request.headers['Content-Type'] == 'application/pdf':
        os.makedirs(event_path)

        with Image(file=BytesIO(request.data), resolution=900) as pdfdoc:
            with pdfdoc.convert('jpeg') as png_pages:
                for idx, png in enumerate(png_pages.sequence):
                    img = Image(png)
                    img.compression_quality = 100

                    img_filename = os.path.join(event_path,
                                                'image{}.jpg'.format(idx + 1))
                    img.save(filename=img_filename)

                print '{} images extracted from PDF'.format(
                    len(png_pages.sequence))
    else:
        return 'Not a valid file', 400

    return event_uuid, 200
예제 #5
0
def _resize(image_obj, width, height, **options):
    img_format = options['format']
    upscale = options['upscale']
    quality = options['quality']

    img_file = io.BytesIO()

    img = Image(file=image_obj)
    original_width, original_height = img.size
    new_width, new_height = width, height
    # Stretchs or fits the width with desired width
    if width < original_width or upscale:
        new_width = width
    # Stretchs or fits the height with desired height
    if (height < original_height) or upscale:
        new_height = height
    # Preserves the aspect ratio fit to new height
    if width == 0:
        new_width = int(original_width * (new_height / original_height))
    # Preserves the aspect ratio fit to new width
    if height == 0:
        new_height = int(original_height * (new_width / original_width))
    img.resize(new_width, new_height)
    img.format = img_format
    img.compression_quality = quality
    img.save(file=img_file)
    img_file.seek(0)
    return img_file
예제 #6
0
    def _convert_image(self, file_path: str, preview_dims: ImgDims) -> Image:
        """
        refer: https://legacy.imagemagick.org/Usage/thumbnails/
        like cmd: convert -layers merge  -background white -thumbnail widthxheight \
        -auto-orient -quality 85 -interlace plane input.jpeg output.jpeg
        """

        img = Image(filename=file_path)
        resize_dim = compute_resize_dims(dims_in=ImgDims(width=img.width,
                                                         height=img.height),
                                         dims_out=preview_dims)

        img.auto_orient()
        img.iterator_reset()
        img.background_color = Color("white")
        img.merge_layers("merge")

        if self.progressive:
            img.interlace_scheme = "plane"

        img.compression_quality = self.quality

        img.thumbnail(resize_dim.width, resize_dim.height)

        return img
예제 #7
0
def convert2pdf(path, images):
    img = Image()
    for image in images:
        img.read(filename=os.path.join(path, image))
    img.compression_quality = 75
    img.save(filename=os.path.join(path, 'exported.pdf'))
    return path
예제 #8
0
	def resize_image(self, img_path, size_config, size_name, dest_path, compress=False):
		#open image
		img = Image(filename=img_path)
		#transform using resize config
		img.transform(resize=size_config)
		'''
		exProcessor = ExifData()
		#rotate image if necessary
		if exProcessor.check_orientation(img) in [6, 7]:
			img.rotate(90)
			img.metadata['exif:Orientation'] = 1
		if exProcessor.check_orientation(img) in [6, 8]:
			img.rotate(-90)
			img.metadata['exif:Orientation'] = 1
		if exProcessor.check_orientation(img) in [3, 4]:
			img.rotate(180) 
			img.metadata['exif:Orientation'] = 1
		'''

		# Detect file extention
		fileName, fileExtension = os.path.splitext(img_path)

		#save img
		fileName = ''.join([fileName, '_', size_name, fileExtension])
		fileName = os.path.basename(fileName)
		fileName = os.path.abspath(os.path.join(dest_path, fileName))

		# Compress option?
		if compress:
			img.compression_quality = 70

		img.save(filename=fileName)

		return fileName
예제 #9
0
파일: pdfE.py 프로젝트: bigV4/skBatEditor
def _run_convert_all(filename, res=120):
    '''把pdf所有页面转化为图片'''
    # 由于每次转换的时候都需要重新将整个PDF载入内存,所以这里使用内存缓存
    pdfile, f = get_pdf_reader(filename), open(filename, "rb")
    # pdfile,f = get_pdf_reader2(filename)
    for i in range(0, pdfile.getNumPages()):
        temp_time = time.time() * 1000
        pageobj = pdfile.getPage(i)
        dst_pdf = PdfFileWriter()
        dst_pdf.addPage(pageobj)

        pdf_bytes = io.BytesIO()
        dst_pdf.write(pdf_bytes)
        pdf_bytes.seek(0)

        img = Image(file=pdf_bytes, resolution=res)
        img.format = 'png'
        img.compression_quality = 90
        img.background_color = Color("white")
        # 保存图片
        img_path = 'dest/%s_pg%d.png' % (filename[:filename.rindex('.')],
                                         i + 1)
        img.save(filename=img_path)
        img.destroy()
        img, pdf_bytes, dst_pdf = None, None, None
        print('convert page %d cost time %dms' %
              (i + 1, (time.time() * 1000 - temp_time)))
    f.close()
예제 #10
0
파일: app.py 프로젝트: fyttyf/tikz_WEB
def _run_convert(filename, page, res=120):
    idx = page + 1
    temp_time = time.time() * 1000
    # 由于每次转换的时候都需要重新将整个PDF载入内存,所以这里使用内存缓存
    pdfile = getPdfReader(filename)
    pageObj = pdfile.getPage(page)
    dst_pdf = PdfFileWriter()
    dst_pdf.addPage(pageObj)

    pdf_bytes = io.BytesIO()
    dst_pdf.write(pdf_bytes)
    pdf_bytes.seek(0)

    img = Image(file=pdf_bytes, resolution=res)
    img.format = 'png'
    img.compression_quality = 90

    img.background_color = Color('white')
    img.alpha_channel = 'remove'
    #img.channels = 'rgb_channels'
    # 保存图片
    #filename[:filename.rindex('.')]
    img_path = './image/%s.png' % (filename[filename.rindex('/pdf/') +
                                            5:filename.rindex('.pdf')])
    #)
    img.save(filename=img_path)
    img.destroy()
    img = None
    pdf_bytes = None
    dst_pdf = None
예제 #11
0
파일: pdfE.py 프로젝트: bigV4/skBatEditor
def _run_convert(filename, page, res=120):
    '''把pdf指定页码转化为图片'''
    pdfile, f = get_pdf_reader(filename), open(filename, "rb")
    # pdfile,f = get_pdf_reader2(filename)
    if page <= pdfile.getNumPages():
        idx = page + 1
        temp_time = time.time() * 1000
        # 由于每次转换的时候都需要重新将整个PDF载入内存,所以这里使用内存缓存
        pageobj = pdfile.getPage(page)
        dst_pdf = PdfFileWriter()
        dst_pdf.addPage(pageobj)

        pdf_bytes = io.BytesIO()
        dst_pdf.write(pdf_bytes)
        pdf_bytes.seek(0)

        img = Image(file=pdf_bytes, resolution=res)
        img.format = 'png'
        img.compression_quality = 90
        img.background_color = Color("white")
        # 保存图片
        img_path = 'dest/%s_pg%d.png' % (filename[:filename.rindex('.')], idx)
        img.save(filename=img_path)
        img.destroy()
        img, pdf_bytes, dst_pdf = None, None, None
        print('convert page %d cost time %dms' %
              (idx, (time.time() * 1000 - temp_time)))
    else:
        print("pg%r list index out of range" % page)
    f.close()
예제 #12
0
def _thumbnail(image_obj, width, height, **options):
    img_format = options['format']
    quality = options['quality']

    img_file = io.BytesIO()

    img = Image(file=image_obj)
    original_width, original_height = img.size
    new_width, new_height = width, height
    # Preserves the aspect ratio fit to new height
    if width == 0:
        new_width = int(original_width * (new_height / original_height))
    # Preserves the aspect ratio fit to new width
    if height == 0:
        new_height = int(original_height * (new_width / original_width))
    # If desired width and/or height is larger than original ones, shrinks it
    # If not, enlarges it
    if (new_width > original_width) or (new_height > original_height):
        img.transform(str(new_width) + 'x' + str(new_height) + '<')
    else:
        img.transform(str(new_width) + 'x' + str(new_height) + '>')
    img.format = img_format
    img.compression_quality = quality
    img.save(file=img_file)
    img_file.seek(0)
    return img_file
예제 #13
0
def _run_convert(filename, page, res=120):
    idx = page + 1
    temp_time = time.time() * 1000
    # 由于每次转换的时候都需要重新将整个PDF载入内存,所以这里使用内存缓存
    pdfile = getPdfReader(filename)
    pageObj = pdfile.getPage(page)
    dst_pdf = PdfFileWriter()
    dst_pdf.addPage(pageObj)

    pdf_bytes = io.BytesIO()
    dst_pdf.write(pdf_bytes)
    pdf_bytes.seek(0)

    img = Image(file=pdf_bytes, resolution=res)
    img.format = 'png'
    img.compression_quality = 90
    img.background_color = Color("white")
    # 保存图片
    img_path = '%s%d.png' % (filename[:filename.rindex('.')], idx)
    img.save(filename=img_path)
    img.destroy()
    img = None
    pdf_bytes = None
    dst_pdf = None
    print('convert page %d cost time %d' % (idx, (time.time() * 1000 - temp_time)))
def convertPdfToImage(file_loc):
	new_dir = file_loc.split(".")[0]
	if not os.path.exists(new_dir):
		os.makedirs(new_dir)
	doc = Image(filename=file_loc,resolution=300)
	doc.compression_quality = 20
	for i, page in enumerate(doc.sequence):
		with Image(page) as page_image:
			page_image.alpha_channel = False
			page_image.background_color = Color('white')
			save_location =  new_dir+"/img-{}.jpg".format(str(i).zfill(4))
			page_image.save(filename=save_location)
예제 #15
0
def convertPdfToImage(file_loc):
    new_dir = file_loc.split(".")[0]
    if not os.path.exists(new_dir):
        os.makedirs(new_dir)
    doc = Image(filename=file_loc, resolution=300)
    doc.compression_quality = 20
    for i, page in enumerate(doc.sequence):
        with Image(page) as page_image:
            page_image.alpha_channel = False
            page_image.background_color = Color('white')
            save_location = new_dir + "/img-{}.jpg".format(str(i).zfill(4))
            page_image.save(filename=save_location)
예제 #16
0
def _rotate(image_obj, degree, **options):
    img_format = options['format']
    quality = options['quality']

    img_file = io.BytesIO()

    img = Image(file=image_obj)
    img.rotate(degree)
    img.format = img_format
    img.compression_quality = quality
    img.save(file=img_file)
    img_file.seek(0)
    return img_file
예제 #17
0
def _flip(image_obj, direction, **options):
    img_format = options['format']
    quality = options['quality']

    img_file = io.BytesIO()

    img = Image(file=image_obj)
    if direction == 'v':
        img.flip()
    if direction == 'h':
        img.flop()
    img.format = img_format
    img.compression_quality = quality
    img.save(file=img_file)
    img_file.seek(0)
    return img_file
예제 #18
0
def writeImageNoWarp(pdf, out, resolution=pdfDense, trim=True):
    with Image(filename=pdf, resolution=resolution) as pdf:

        pages = len(pdf.sequence)
        height = 0
        width = 0
        for j in range(pages):
            ## Go through the sequence and trim them individually
            with pdf.sequence.index_context(j):
                if trim:
                    pdf.trim()
                    height += pdf.height + 10
                    if (width < pdf.width):
                        width = pdf.width
                else:
                    width = pdf.width
                    height = pdf.height

        i = Image(width=width, height=height)

        height = [r.height for r in pdf.sequence]
        height = height[::-1]
        height.append(0)
        height = height[::-1]
        width = [math.floor((width - r.width) / 2) for r in pdf.sequence]

        for j in range(pages):
            i.composite(pdf.sequence[j], top=height[j] + j * 10, left=width[j])
        i.background_color = Color('white')  # Set white background.
        i.alpha_channel = 'remove'
        if trim:  ## This is necessary because of stacked charts (above)
            i.trim()
        i.sharpen(radius=5.0, sigma=5.0)
        i.normalize()
        i.quantize(16, dither=False)
        i.compression_quality = 00
        i.save(filename=out.replace(".png", ".png8"))
        tmp = out.replace(".png", "") + "*"
        #out = glob.glob(tmp)
        if (os.system("rename .png8 .png %s*" % tmp)):
            print("Error in saving file.")
        if (os.system("optipng -quiet %s*" % tmp)):
            print("Error in optipng.")
        commstr = 'cwebp -quiet -lossless -z 9 -metadata exif %s -o %s' % (
            out, out.replace(".png", ".webp"))
        if (os.system(commstr)):
            print("Error in cwebp")
예제 #19
0
def convert(filename, page=0, res=120):
    reader = PdfFileReader(filename, strict=False)
    page_obj = reader.getPage(page)
    dst_pdf = PdfFileWriter()
    dst_pdf.addPage(page_obj)

    pdf_bytes = io.BytesIO()
    dst_pdf.write(pdf_bytes)
    pdf_bytes.seek(0)

    img = Image(file=pdf_bytes, resolution=res)
    img.format = 'png'
    img.compression_quality = 90
    img.background_color = Color('white')
    img_path = filename.replace('pdf', 'png')
    img.save(filename=img_path)
    img.destroy()
예제 #20
0
def convert(filename, res=120):
    pdf_file = PdfFileReader(open(filename, "rb"))
    pageObj = pdf_file.getPage(0)
    dst_pdf = PdfFileWriter()
    dst_pdf.addPage(pageObj)

    pdf_bytes = io.BytesIO()
    dst_pdf.write(pdf_bytes)
    pdf_bytes.seek(0)

    img_filename = filename.replace('old', 'img').replace('pdf', 'png')
    img = Image(file=pdf_bytes, resolution=res)
    img.format = 'png'
    img.compression_quality = 120
    img.background_color = Color("white")
    img.save(filename=img_filename)
    img.destroy()
예제 #21
0
    def _convert_pdf2img(self, file, store_path):
        pdf_file = PdfFileReader(file, strict=False)
        pageObj = pdf_file.getPage(0)
        dst_pdf = PdfFileWriter()
        dst_pdf.addPage(pageObj)
        pdf_bytes = io.BytesIO()
        dst_pdf.write(pdf_bytes)
        pdf_bytes.seek(0)

        img = Image(file=pdf_bytes, resolution=200)
        img.format = 'jpg'
        img.compression_quality = 90
        img.background_color = Color("white")
        img_path = store_path + '%s.jpg' % (uuid.uuid3(uuid.uuid1(), file))
        print(img_path)
        img.save(filename=img_path)
        img.destroy()
        return img_path
예제 #22
0
def convertPdfToImage(file_loc,doc_id):
	print doc_id
	new_dir, file_ending = file_loc.split(".")
	if file_ending == 'pdf':
		if not os.path.exists(new_dir):
			os.makedirs(new_dir)
		doc = Image(filename=file_loc,resolution=300)
		doc.compression_quality = 20
		for i, page in enumerate(doc.sequence):
			with Image(page) as page_image:
				page_image.alpha_channel = False
				page_image.background_color = Color('white')
				save_location =  new_dir+"/img-{}.jpg".format(str(i).zfill(4))
				page_image.save(filename=save_location)
		t1 = threading.Thread(target=extractTextInImage,args=(file_loc,doc_id))
		t1.start()
		t2 = threading.Thread(target=updateImgMatchesForNewDoc,args=(doc_id,))
		t2.start()
예제 #23
0
def _run_convert(pdfile, savedfilename, page_index, index, res=120):
    pageObj = pdfile.getPage(page_index)  #获取pdf的第page_index页
    dst_pdf = PdfFileWriter()
    dst_pdf.addPage(pageObj)
    pdf_bytes = io.BytesIO()
    dst_pdf.write(pdf_bytes)
    pdf_bytes.seek(0)
    img = Image(file=pdf_bytes, resolution=res)

    img.format = 'png'

    img.compression_quality = 90
    img.background_color = Color("white")

    img_path = '%s%04d.jpg' % (savedfilename, index)
    img.save(filename=img_path)
    print(img_path)
    img.destroy()
예제 #24
0
def _run_convert(filename, page, res=120):
    idx = page + 1
    pdfile = getPdfReader(filename)
    pageObj = pdfile.getPage(page)
    dst_pdf = PdfFileWriter()
    dst_pdf.addPage(pageObj)

    pdf_bytes = io.BytesIO()
    dst_pdf.write(pdf_bytes)
    pdf_bytes.seek(0)

    img = Image(file=pdf_bytes, resolution=res)
    img.format = 'jpg'
    img.compression_quality = 90
    img.background_color = Color("white")
    img_path = '%s%d.jpg' % (filename[:filename.rindex('.')], idx)
    img.save(filename=img_path)
    img.destroy()
예제 #25
0
def picture_save_api(request_file,
                     original_file_size=(0, 0),
                     original_file_type='jpeg',
                     compress_file_size=(0, 0),
                     compress_file_type='jpeg'):
    pic = Picture()
    pic.save()

    original_image = Image(file=urlopen(request_file))
    compress_image = original_image.clone()

    original_image.format = original_file_type
    compress_image.format = compress_file_type
    w, h = original_image.size

    original_width = w
    compress_width = w
    original_height = h
    compress_height = h

    if original_file_size[0] > 0:
        original_width = original_file_size[0]
    if original_file_size[1] > 0:
        original_height = original_file_size[1]

    if compress_file_size[0] > 0:
        compress_width = compress_file_size[0]
    if compress_file_size[1] > 0:
        compress_height = compress_file_size[1]

    original_image.resize(original_width, original_height)
    original_image.compression_quality = 60

    compress_image.resize(compress_width, compress_height)
    compress_image.compression_quality = 60

    pic.original_image.save(
        str(pic.id) + u'_o.' + original_file_type,
        ContentFile(original_image.make_blob()))
    pic.compress_image.save(
        str(pic.id) + u'_c.' + compress_file_type,
        ContentFile(compress_image.make_blob()))

    return pic
예제 #26
0
    def set_image(self, binary):
        self.binary = binary
        with Image(blob=self.binary, resolution=150) as img:
            self.extension = img.format.lower()
            flattened = Image(background=Color("white"),
                height=img.height, width=img.width)
            flattened.composite(img, left=0, top=0)
            flattened.format = "jpeg"
            flattened.compression_quality = 50

            thumbnail = flattened.clone()
            thumbnail.transform(resize='150x200>')
            self.thumbnail = thumbnail.make_blob()

            preview = flattened.clone()
            preview.gaussian_blur(radius=1, sigma=0.5)
            preview.transform(resize='612x792>')
            self.preview = preview.make_blob()
        self.save()
예제 #27
0
def pdf_to_image(pdf, page=1, size=800, file_format='jpeg', quality=80):
    """Creates a image file from pdf file"""
    try:
        pdf.open()
    except Exception as e:
        raise e
    else:
        # do this while the pdf is open
        reader = PyPDF2.PdfFileReader(pdf, strict=False)
        writer = PyPDF2.PdfFileWriter()
        page_content = reader.getPage(page - 1)
        box = page_content.cropBox
        dims = [float(a - b) for a, b in zip(box.upperRight, box.lowerLeft)]
        scaleby = size / max(dims)
        dims = [int(d * scaleby) for d in dims]
        # resize_page(page_content, size)
        writer.addPage(page_content)
        outputStream = BytesIO()
        writer.write(outputStream)
        outputStream.seek(0)
    finally:
        pdf.close()
    # put content of page in a new image
    foreground = WandImage(
        blob=outputStream,
        format='pdf',
        resolution=int(1.6 * 72 * scaleby),
    )
    # make sure the color space is correct.
    # this prevents an occational bug where rgb colours are inverted
    foreground.type = 'truecolormatte'
    foreground.resize(*dims, 25)
    # white background
    background = WandImage(
        width=foreground.width,
        height=foreground.height,
        background=Color('white')
    )
    background.format = file_format
    background.composite(foreground, 0, 0)
    background.compression_quality = quality
    return background
예제 #28
0
    def pages(self):
        for page in range(self.file.numPages):
            img = WandImage(filename=self.path + ('[%s]' % page),
                resolution=self.config['wand_resolution'])
            img.compression_quality = self.config['wand_compression_quality']
            temp = NamedTemporaryFile(suffix='.jpg')
            # Passing temp as file kwargs does not work for some reason.
            # So we just pass the filename.
            img.save(filename=temp.name)

            # Reopen the image file as PIL object
            img = Image.open(temp.name)

            # Run tesseract
            tr = Tesseract()
            result = tr.ocr_image(img)

            temp.close()

            yield result
예제 #29
0
def convertPdfToImage(file_loc, doc_id):
    print doc_id
    new_dir, file_ending = file_loc.split(".")
    if file_ending == 'pdf':
        if not os.path.exists(new_dir):
            os.makedirs(new_dir)
        doc = Image(filename=file_loc, resolution=300)
        doc.compression_quality = 20
        for i, page in enumerate(doc.sequence):
            with Image(page) as page_image:
                page_image.alpha_channel = False
                page_image.background_color = Color('white')
                save_location = new_dir + "/img-{}.jpg".format(str(i).zfill(4))
                page_image.save(filename=save_location)
        t1 = threading.Thread(target=extractTextInImage,
                              args=(file_loc, doc_id))
        t1.start()
        t2 = threading.Thread(target=updateImgMatchesForNewDoc,
                              args=(doc_id, ))
        t2.start()
예제 #30
0
    def run_convert(filename, res):
        pdfile = getPdfReader(filename)
        pages = pdfile.getNumPages()

        for page in range(pages):
            print('正在解析%d页' % (page+1))
            pageObj = pdfile.getPage(page)
            dst_pdf = PdfFileWriter()
            dst_pdf.addPage(pageObj)

            pdf_bytes = io.BytesIO()
            dst_pdf.write(pdf_bytes)
            pdf_bytes.seek(0)

            img = Image(file=pdf_bytes, resolution=res)
            img.compression_quality = 90
            img.background_color = Color("white")
            # img.save(filename = 'C:\\Users\\SXL47\\Desktop\\tupian.jpg')
            blob = img.make_blob('jpg')
            img.destroy()
            yield blob
예제 #31
0
def pdf_to_image(pdf, page=1, size=800, file_format='jpeg', quality=80):
    """Creates a image file from pdf file"""
    try:
        pdf.open()
    except Exception as e:
        raise e
    else:
        # do this while the pdf is open
        reader = PyPDF2.PdfFileReader(pdf, strict=False)
        writer = PyPDF2.PdfFileWriter()
        page_content = reader.getPage(page - 1)
        box = page_content.cropBox
        dims = [float(a - b) for a, b in zip(box.upperRight, box.lowerLeft)]
        scaleby = size / max(dims)
        dims = [int(d * scaleby) for d in dims]
        # resize_page(page_content, size)
        writer.addPage(page_content)
        outputStream = BytesIO()
        writer.write(outputStream)
        outputStream.seek(0)
    finally:
        pdf.close()
    # put content of page in a new image
    foreground = WandImage(
        blob=outputStream,
        format='pdf',
        resolution=int(1.6 * 72 * scaleby),
    )
    # make sure the color space is correct.
    # this prevents an occational bug where rgb colours are inverted
    foreground.type = 'truecolormatte'
    foreground.resize(*dims, 25)
    # white background
    background = WandImage(width=foreground.width,
                           height=foreground.height,
                           background=Color('white'))
    background.format = file_format
    background.composite(foreground, 0, 0)
    background.compression_quality = quality
    return background
예제 #32
0
    def convert_scanned_pdf_to_png(self):
        """
        Resolution = 300 and compression_quality = 99 are optimal to assure the image's quality.
        :return: file converted to JPG
        """

        img = Img(filename=self.file, resolution=300) ### Opens scanned pdf file
        img.compression_quality = 99 ### Sets compression quality to 72

        ### Reassign self.img_file according to self.file
        try:
            self.img_file = self.file.split('/')[-1]
            self.img_file = self.img_file.replace('.pdf', '.png')
            self.img_file = os.path.join(ParseScannedPdf.temp_folder, self.img_file)
        except Exception as e:
            print 'Be sure to use forward slash when assigning the path of the pdf or img file.'
            print e.args
            sys.exit()

        self.type_pdf = True
        img.save(filename=self.img_file)  ### Converts the file to png
        time.sleep(2)
예제 #33
0
    def resize_image(self,
                     img_path,
                     size_config,
                     size_name,
                     dest_path,
                     compress=False):
        #open image
        img = Image(filename=img_path)
        #transform using resize config
        img.transform(resize=size_config)
        '''
		exProcessor = ExifData()
		#rotate image if necessary
		if exProcessor.check_orientation(img) in [6, 7]:
			img.rotate(90)
			img.metadata['exif:Orientation'] = 1
		if exProcessor.check_orientation(img) in [6, 8]:
			img.rotate(-90)
			img.metadata['exif:Orientation'] = 1
		if exProcessor.check_orientation(img) in [3, 4]:
			img.rotate(180) 
			img.metadata['exif:Orientation'] = 1
		'''

        # Detect file extention
        fileName, fileExtension = os.path.splitext(img_path)

        #save img
        fileName = ''.join([fileName, '_', size_name, fileExtension])
        fileName = os.path.basename(fileName)
        fileName = os.path.abspath(os.path.join(dest_path, fileName))

        # Compress option?
        if compress:
            img.compression_quality = 70

        img.save(filename=fileName)

        return fileName
예제 #34
0
def _run_convert(pdfile, savedfilename, page_index, index, res=240):
    # http://www.imooc.com/wenda/detail/600387
    # 在Python中从PDF提取图像而无需重新采样?
    #
    pageObj = pdfile.getPage(page_index)  #获取pdf的第page_index页
    dst_pdf = PdfFileWriter()
    dst_pdf.addPage(pageObj)
    pdf_bytes = io.BytesIO()
    dst_pdf.write(pdf_bytes)
    pdf_bytes.seek(0)
    img = Image(file=pdf_bytes, resolution=res)
    img.format = 'png'
    img.compression_quality = 100
    img.background_color = Color("white")
    number = index + 1
    img_path = '%s页面_%02d.jpg' % (savedfilename, number)
    #-------------------------------------------------------------------------------------------------------------------
    # Save Images
    #
    img.save(filename=img_path)
    print("extract " + img_path)
    img.destroy()
예제 #35
0
    def run_convert(self, filename, page, res=120):
        # idx = page + 1
        pdfile = getPdfReader(filename)
        pageObj = pdfile.getPage(page)
        dst_pdf = PdfFileWriter()
        dst_pdf.addPage(pageObj)

        pdf_bytes = io.BytesIO()
        dst_pdf.write(pdf_bytes)
        pdf_bytes.seek(0)

        img = Image(file=pdf_bytes, resolution=res)
        img.format = 'png'
        img.compression_quality = 100
        img.background_color = Color("white")
        filename = filename.split("/")[-1]
        img_path = 'resource/%s%s.png' % (filename[:filename.rindex('.')],
                                          "temp")
        delete_file("temp", root='resource/')
        img.save(filename=img_path)
        img.destroy()

        return img_path
예제 #36
0
def test_wand():
    image_data = Image(blob=s)
    image_data.compression_quality = 70
    is_gallery_card = False
    image_data_size = (image_data.width, image_data.height)

    image_size = (720, 720)
    if is_gallery_card:
        image_size = (720, 1120)

    if image_size != image_data_size:
        result = image_data.resize(image_size[0], image_size[1])

    with image_data.clone() as img:
        result = img = crop_image(img, (720, 472))

        # 공감전용 카드의 경우 댓글용 이미지를 생성하지 않는다.
        if not is_gallery_card:
            result = img.resize(460, 310)

    result = image_data.resize(360, 360)
    result = image_data.resize(132, 132)
    return result
예제 #37
0
def convert_pdf_to_img(blob, img_type="jpg", quality=75, resolution=200):
    """
    Converts PDF with multiple pages into one image.
    It needs the file content NOT the filename or ioBytes and returns the image content.
    Note: It has memory leak!!
    http://stackoverflow.com/a/26233785/1497443

    Example:

    with open('my.pdf', "r") as f:
        file_content = f.read()

    # import ipdb
    # ipdb.set_trace()
    hh = convert_pdf_to_jpg(file_content)

    with open('my.jpg', 'wb') as f:
        f.write(hh)
    """
    from wand.image import Image as WandImage
    from wand.color import Color as WandColor

    pdf = WandImage(blob=blob, resolution=resolution)

    pages = len(pdf.sequence)

    wimage = WandImage(width=pdf.width,
                       height=pdf.height * pages,
                       background=WandColor("white"))

    for i in xrange(pages):
        wimage.composite(pdf.sequence[i], top=pdf.height * i, left=0)

    if img_type == "jpg":
        wimage.compression_quality = quality

    return wimage.make_blob(img_type)
예제 #38
0
파일: utils.py 프로젝트: lexsos/avia35inner
def make_pages(request, document_pk):
    document = Document.objects.get(pk=document_pk)
    clear_pages(document)

    pdf_file_name = document.document_file._get_path()
    pdf_file = Image(filename=pdf_file_name, resolution=CONFIG['RESOLUTION'])
    tmp_dir = tempfile.mkdtemp()

    for page in pdf_file.sequence:
        page_number = page.index + 1
        page_img = Image(page)
        page_img.compression_quality = CONFIG['COMPRESSION_QUALITY']
        page_img_file_name = tmp_dir + \
            '/page{0}-{1}.jpg'.format(document_pk, page_number)
        page_img.save(filename=page_img_file_name)
        create_page(document, page_number, page_img_file_name)

    shutil.rmtree(tmp_dir)

    messages.add_message(
        request,
        messages.INFO,
        'Make pages for "{0}"'.format(document)
    )
예제 #39
0
def picture_save_api(request_file, original_file_size=(0, 0), original_file_type='jpeg', compress_file_size=(0, 0), compress_file_type='jpeg'):
    pic = Picture()
    pic.save()

    original_image = Image(file=urlopen(request_file))
    compress_image = original_image.clone()

    original_image.format = original_file_type
    compress_image.format = compress_file_type
    w, h = original_image.size

    original_width = w
    compress_width = w
    original_height = h
    compress_height = h

    if original_file_size[0] > 0:
        original_width = original_file_size[0]
    if original_file_size[1] > 0:
        original_height = original_file_size[1]

    if compress_file_size[0] > 0:
        compress_width = compress_file_size[0]
    if compress_file_size[1] > 0:
        compress_height = compress_file_size[1]

    original_image.resize(original_width, original_height)
    original_image.compression_quality = 60

    compress_image.resize(compress_width, compress_height)
    compress_image.compression_quality = 60

    pic.original_image.save(str(pic.id) + u'_o.' + original_file_type, ContentFile(original_image.make_blob()))
    pic.compress_image.save(str(pic.id) + u'_c.' + compress_file_type, ContentFile(compress_image.make_blob()))

    return pic
예제 #40
0
			   boxWidth > minFontWidth and boxWidth < maxFontWidth:
				digit.resize(boxWidth, boxHeight, 'cubic')
				draw.composite(operator=operator, left=boxLeft, top=boxTop, width=boxWidth, height=boxHeight, image=digit)
			else:
				digit.resize(int(round(fontWidth*defaultFontScale)), int(round(fontHeight*defaultFontScale)), 'cubic')
				draw.composite(operator=operator, left=round((boxLeft+boxRight-fontWidth*defaultFontScale)/2.0), 
					       top=round((boxTop+boxBottom-fontHeight*defaultFontScale)/2.0), width=fontWidth*defaultFontScale, 
					       height=fontHeight*defaultFontScale, image=digit)

		
		characterIndex += 1
		col += 1
		if (octalDigitIndex % 6) == 4:
			col += 1
		if (octalDigitIndex % 6) == 5:
			col += 7
			characterIndex += 1
		boxIndex += 1
	# Next row, please
	row += 1
	if (index % 4) == 3:
		row += 1.2
draw(img)

# Create the output image.
img.format = 'jpg'
img.compression_quality = 25
img.save(filename=outImage)
print 'output =', outImage