def create_gif(images, base_size=1000, quality=30): # Add new frames into sequance for i in range(len(images)): ph = Image.open('photo/{}.jpg'.format(i)) ratio = max(ph.size) / base_size ph = ph.resize((round(ph.size[0] / ratio), round(ph.size[1] / ratio))) ph.save('photo/{}{}.jpg'.format(i, i), quality=quality) with wandImage() as img: # Add new frames into sequance for i in range(len(images)): with wandImage(filename='photo/{}{}.jpg'.format(i, i), format='jpg') as ph: img.sequence.append(ph) # Create progressive delay for each frame for cursor in range(len(images)): with img.sequence[cursor] as frame: frame.delay = 30 # Set layer type img.type = 'optimize' img.loop = 0 img.save(filename='photo/erj.gif')
def save_thumbnail(data, local_path): image = wandImage(blob=data, resolution=300, format="pdf") if len(image.sequence) > 1: # Select the first page of the pdf image = wandImage(image.sequence[0]) image.transform(resize='640') image.crop(width=640, height=360) image.alpha_channel = 'remove' image.save(filename=local_path) return image.make_blob()
def getImageDetails(self): #REQUIRES: self.setImageLocation must already be called #MODIFIES: self #EFFECTS: Gets various details about the input image with wandImage(filename=self.location) as img: self.width = img.width self.height = img.height self.bitdepth = img.depth self.format = img.format self.colormode = img.type # using PIL, seek until EOF error is experienced, and set numImages to how many # iterations it got through before that #self.numImages = img.frame_num #really unsure about this one self.numImages = 0 tempImage = Image.open(self.location) try: while 1: tempImage.seek(self.numImages) self.numImages +=1 except EOFError: tempImage.seek(0) pass #tempImage.close() print "Width: " + str(self.width) print "Height: " + str(self.height) print "Bit depth: " + str(self.bitdepth) print "Format: " + str(self.format) print "Color mode: " + str(self.colormode) print "Frames: " + str(self.numImages)
def pdf_to_image(self, FileName, ImageName, ImageResolution): with wandImage(filename=FileName, resolution=ImageResolution) as img: with img.convert('png') as converted: converted.save(filename=ImageName) print("Pdf to Image:", FileName, ImageName)
def fetch_pic_txt(self, pageidx, ds=1024): """ split pdf file :param ds: (int) set ds = 1024 ~= 1MB output under my test :return: splited PNG image file """ if image_module == 'magick': image = PythonMagick.Image() image.density(str(ds)) image.read(str(self.i_file + '[%s]' % pageidx)) image.magick("PNG") pic_path = os.path.abspath( os.path.join(self.o_dire, "tmppicforpdf_{}.png".format(pageidx + 1))) image.write(pic_path) with Image.open(pic_path) as img_f: txt = recogniz(img_f) else: with wandImage( image=self.image_jpeg.sequence[pageidx]) as img_page: img = img_page.make_blob('jpeg') with Image.open(io.BytesIO(img)) as img_f: txt = recogniz(img_f) print "Got OCR result from page", pageidx + 1 return txt
def getImageDetails(self): #REQUIRES: self.setImageLocation must already be called #MODIFIES: self #EFFECTS: Gets various details about the input image with wandImage(filename=self.location) as img: self.width = img.width self.height = img.height self.bitdepth = img.depth self.format = img.format self.colormode = img.type # using PIL, seek until EOF error is experienced, and set numImages to how many # iterations it got through before that #self.numImages = img.frame_num #really unsure about this one self.numImages = 0 tempImage = Image.open(self.location) try: while 1: tempImage.seek(self.numImages) self.numImages += 1 except EOFError: tempImage.seek(0) pass #tempImage.close() print "Width: " + str(self.width) print "Height: " + str(self.height) print "Bit depth: " + str(self.bitdepth) print "Format: " + str(self.format) print "Color mode: " + str(self.colormode) print "Frames: " + str(self.numImages)
def pdf_jpg_converter(path): #a=a+1 print path try: with wandImage(filename=path) as converted: print 'jpg_pdf_converter- before save' print converted.size converted.save(filename= path + ".jpg") print 'jpg_pdf_converter- after save' print converted.size print type(converted) with wandImage(filename=path + ".jpg") as converted: print 'jpg_pdf_converter- before resize' converted.resize(300, 300) print 'jpg_pdf_converter- after resize' except Exception, e: print e
def ocrConvert(self): fileName = self.fileComboBox.currentText() if not fileName: self.logger.show.emit('请先选择要识别的Image或PDF文档') return self.logger.show.emit('开始OCR识别:{}'.format(fileName)) tool = pyocr.get_available_tools()[0] pdf_file=fileName req_image = [] ima_pdf = wandImage(filename=pdf_file, resolution=300) image_jpeg = ima_pdf.convert('jpeg') for img in image_jpeg.sequence: img_page = wandImage(image=img) img_page.type = 'grayscale' req_image.append(img_page.make_blob('jpeg')) page_number = 0 final_text="" for img in req_image: text = tool.image_to_string( pillowIMage.open(io.BytesIO(img)), lang='jpn+eng+chi_sim', builder=pyocr.builders.TextBuilder() ) page_number = page_number + 1 final_text=final_text+"Page-"+str(f'{page_number}')+"\r\n"+text+"\r\n" self.logger.show.emit("Page-"+str(f'{page_number}')+":"+text) final_text = romoverepetedlinebreak(final_text).replace('\n','\r\n') with open(f'{pdf_file}.txt', 'wb') as f: f.write(final_text.encode('utf-8')) self.logger.show.emit('OCR识别完成')
def render_wand_image(lang, text): with wandDrawing() as draw: with wandImage(width=400, height=75, background=wandColor('rgb(0, 140, 150)')) as img: draw.font = font draw.font_size = 30 draw.push() draw.fill_color = wandColor('rgb(255, 255, 255)') draw.text_alignment = 'center' draw.text(int(img.width / 2), int(img.height / 2 + 10), text) draw.pop() draw(img) img.save(filename=os.path.join(wand_render_dir, '{0}.png'.format(lang)))
def convertImage(self, fileIn, fileOut): """ Image conversion with wand. """ im = wandImage(filename=fileIn) # remove timestamp from png (keep checksum unchanged for test) im.artifacts['png:exclude-chunks'] = 'date,time' im.strip() # for (k, v) in im.artifacts.items(): # print(k, v) print(" Conversion from {} to {}.".format( os.path.splitext(fileIn)[1], os.path.splitext(fileOut)[1])) im.save(filename=fileOut) im.close()
def __init__(self, i_file, o_dire): """ init args :param i_file: (str) input pdf file's path (eg: "/home/file.pdf") :param o_dire: (str) output image directory (eg: "/home/") """ self.i_file = i_file self.o_dire = o_dire if image_module == 'magick': with file(self.i_file, "rb") as tmp_f: self.pages = PyPDF2.PdfFileReader(tmp_f).getNumPages() print( 'Totally get ***{0:^4}*** pages from "{1.i_file}", playpdf start......' .format(self.pages, self)) else: with wandImage(filename=i_file, resolution=300) as tmp_f: self.image_jpeg = tmp_f.convert('jpeg')
def pdftoimage(self, pdfpath): pdf = PyPDF2.PdfFileReader(open(pdfpath, "rb")) newpdf = PyPDF2.PdfFileWriter() if pdf.getNumPages() > 0: newpdf.addPage(pdf.getPage(0)) pdf_bytes = io.BytesIO() newpdf.write(pdf_bytes) pdf_bytes.seek(0) #- this function uses wand to change pdf to images try: with wandImage(file=pdf_bytes, resolution=400) as img: with img.convert('png') as converted: converted.save(filename='tempimg.png') except: print("error")
def pdf2jpg(filepath): """ Description: Convert a single/multi-page PDF document into JPEGs saved in the ./Original and ./Zoom directories. Parameters: 1 filepath: String, Path to PDF document Return: 0 -:- """ try: with wandImage(filename=filepath, resolution=300) as img: img.compression_quality = 99 img.save(filename="./Original/" + filepath[:-3] + "jpg") img.save(filename="./Zoom/" + filepath[:-3] + "jpg") except MissingDelegateError: print( "\n\nFile entered is not a pdf file!\nOr imagemagick is not installed: brew install imagemagick\n\n" )
def resize(x, y): """ Description: Resize all JPEGs currently in the ./Original and ./Zoom directories. Parameters: 2 x: INT, desired x dimension y: INT, desired y dimension Return: 0 -:- """ try: for file in listdir("./Original"): img = wandImage(filename="./Original/" + file, resolution=300) img.compression_quality = 99 img.resize(x, y) img.save(filename="./Original/" + file) img.save(filename="./Zoom/" + file) except: print("Error resizing.")
def zoom(zlevel): """ Description: Resize all JPEGs currently in the ./Zoom directory. Parameters: 3 zlevel: desired level of zoom between -9 and 9 x: INT, current x dimension y: INT, current y dimension Return: 0 -:- """ x = 1600 y = 2000 level = { -9: 0.1, -8: 0.2, -7: 0.3, -6: 0.4, -5: 0.5, -4: 0.6, -3: 0.7, -2: 0.8, -1: 0.9, 0: 1.0, 1: 1.1, 2: 1.2, 3: 1.3, 4: 1.4, 5: 1.5, 6: 1.6, 7: 1.7, 8: 1.8, 9: 1.9 } try: for file in listdir("./Original"): img = wandImage(filename="./Original/" + file, resolution=300) img.compression_quality = 99 img.resize(int(x * level[zlevel]), int(y * level[zlevel])) img.save(filename="./Zoom/" + file) return int(x * level[zlevel]), int(y * level[zlevel]) except: print("Error zooming.")
def merge_pdf(pages, target_pdf, img_format='png'): merged_pdf = PyPDF2.PdfFileWriter() log.debug('Merging {} into one PDF document'.format(len(pages))) for page in pages: # reset binary stream position page.seek(0) pdf_page_stream = BufferedRandom(BytesIO()) log.debug('Converting image to PDF') with wandImage(file=page, format=img_format) as image: img_converted = image.convert('pdf') img_converted.save(file=pdf_page_stream) img_converted.close() log.debug('Adding PDF page to merged document') pdf_page_stream.seek(0) merged_pdf.addPage(PyPDF2.PdfFileReader(pdf_page_stream).getPage(0)) merged_pdf.write(open(target_pdf, 'wb'))
def generate_profile_pic(save_path, name): words = name.split() letters = [word[0] for word in words] initials = "".join(letters) colors = ["#c75650", "#d67a27", "#7e6ccf", "#4eb331", "#2ea4ca"] with wandImage(width=756, height=756, background=Color(random.choice(colors))) as img: left, top, width, height = 200, 265, 340, 240 with Drawing() as context: font = Font(font_path, color="white") context(img) img.caption(initials, left=left, top=top, width=width, height=height, font=font, gravity="center") img.save(filename=save_path) return save_path
def split_pdf(src_filename, pdf_res, page_list=None, img_format='png'): pdf_source = PyPDF2.PdfFileReader(open(src_filename, 'rb')) logging.debug('Source PDF {} contains {} pages'.format( src_filename, pdf_source.getNumPages())) pages = [] if not page_list: log.debug('Extracting all pages from PDF') # no pages defined, so create a list of all page numbers page_list = list(range(pdf_source.getNumPages())) else: log.debug('Extracting {} page(s) from PDF'.format(len(page_list))) for page_nr in page_list: log.debug('Processing page {}'.format(page_nr)) page = pdf_source.getPage(page_nr) log.debug('Extracting page from source PDF') # extract single page and save it to a temporary stream pdf_writer = PyPDF2.PdfFileWriter() pdf_writer.addPage(page) pdf_page_stream = BufferedRandom(BytesIO()) pdf_writer.write(pdf_page_stream) # reset the binary stream's position to the beginning pdf_page_stream.seek(0) log.debug('Converting PDF page to image ({})'.format(img_format)) # Define the resolution when opening the intermediate PDF for better quality converted PNGs # http://stackoverflow.com/questions/17314382/improve-quality-of-wand-conversion with wandImage(file=pdf_page_stream, resolution=pdf_res) as pdf_page: image_page = pdf_page.convert(img_format) image_page_stream = BufferedRandom(BytesIO()) image_page.save(file=image_page_stream) pages.append(image_page_stream) return pages
def split_pdf(src_filename, pdf_res, page_list=None, img_format='png'): pdf_source = PyPDF2.PdfFileReader(open(src_filename, 'rb')) logging.debug('Source PDF {} contains {} pages'.format(src_filename, pdf_source.getNumPages())) pages = [] if not page_list: log.debug('Extracting all pages from PDF') # no pages defined, so create a list of all page numbers page_list = list(range(pdf_source.getNumPages())) else: log.debug('Extracting {} page(s) from PDF'.format(len(page_list))) for page_nr in page_list: log.debug('Processing page {}'.format(page_nr)) page = pdf_source.getPage(page_nr) log.debug('Extracting page from source PDF') # extract single page and save it to a temporary stream pdf_writer = PyPDF2.PdfFileWriter() pdf_writer.addPage(page) pdf_page_stream = BufferedRandom(BytesIO()) pdf_writer.write(pdf_page_stream) # reset the binary stream's position to the beginning pdf_page_stream.seek(0) log.debug('Converting PDF page to image ({})'.format(img_format)) # Define the resolution when opening the intermediate PDF for better quality converted PNGs # http://stackoverflow.com/questions/17314382/improve-quality-of-wand-conversion with wandImage(file=pdf_page_stream, resolution=pdf_res) as pdf_page: image_page = pdf_page.convert(img_format) image_page_stream = BufferedRandom(BytesIO()) image_page.save(file=image_page_stream) pages.append(image_page_stream) return pages
def fit_text(img, filename, text, name): text = "«" + text + "»" name = "— " + name with wandImage(blob=img) as canvas: text_left, text_top, text_width, text_height = 475, 50, 675, 410 name_left, name_top, name_width, name_height = 530, 460, 570, 50 with Drawing() as context: font = Font(font_path, color="white") context(canvas) canvas.caption(text, left=text_left, top=text_top, width=text_width, height=text_height, font=font, gravity="center") canvas.caption(name, left=name_left, top=name_top, width=name_width, height=name_height, font=font, gravity="center") canvas.save(filename=path + filename + ".jpg")
def get_img(filename): jpeg_name = file.split('.')[0]+'.png' with wandImage(filename=filename) as img: with img.convert('png') as converted: converted.save(filename=jpeg_name) return jpeg_name
import numpy from moviepy.editor import * from wand.image import Image as wandImage clips = [] # Length in seconds of every slide durations = [2, 27, 30, 23, 43, 50, 64, 51, 17, 108, 10, 10] with wandImage(filename="ML2-VDD.pdf", resolution=300) as img: # I think this is a feature, using [0] actualy converts all the sequence img.sequence[0].container.save(filename='slide.jpg') for i in img.sequence: clips.append( ImageClip('slide-%d.jpg' % i.index).set_duration( durations[i.index]).fadein(1).fadeout(1)) audio = AudioFileClip("talk.mp3") final = concatenate_videoclips(clips).set_audio(audio) final.write_videofile("movie.mp4", fps=24)
# orgPic = Image.open(os.path.join(prjPath, 'wikipedia.png')) # grayPic = orgPic.convert('L') # grayPic = grayPic.point(lambda x: 0 if x < 128 else 255, '1') # grayPic.save(os.path.join(prjPath, 'wikipediaGrayScale.png')) # # # importing the grayscal pic # grayPic = Image.open(os.path.join(prjPath, 'wikipediaGrayScale.png')) # # # converting the image to string # textFromImage = pyts.image_to_string(grayPic) # print(textFromImage) # converting pdf to image using wand fileToParse = 'RussianSample.pdf' fileName = fileToParse.split('.')[0] with(wandImage(filename=os.path.join(prjPath, fileToParse), resolution=500)) as source: images = source.sequence pages = len(images) print('Number of pages present in the {} is {}'.format(fileToParse, pages)) for ind in range(pages): wandImage(images[ind]).save(filename=os.path.join( prjPath, fileName + 'Page' + str(ind) + '.jpeg')) # importing the grayscal pic # using google translation translator = Translator() with open(os.path.join(prjPath, fileToParse.replace('pdf', 'txt')), 'w', encoding='utf8') as ParsedFile: for ind in range(pages): imageFile = Image.open(os.path.join(prjPath, fileName + 'Page' + str(ind) + '.jpeg')) parsedText = pyts.image_to_string(imageFile, lang='eng+rus') parsedTextToken = parsedText.split()