How to convert latex to image in python - python

In a part of my project for creating a dataset, I have a text file containing a list of a bunch of latex equations . Now I want to convert them into images through python in diffrent font sizes. But i dont know how to do it. Please help.
This is the list of latex symbols I am using:- https://docs.mathpix.com/#vocabulary

#
import shutil
import os
from pdflatex import PDFLaTeX
from pdf2image import convert_from_path
from PIL import Image
def crop(file):
img = Image.open(file)
area = (300,300, 800, 800)
cropped_img = img.crop(area)
cropped_img.save(file)
def save_images(images_names,pdf_path,images_path=""):
# Store Pdf with convert_from_path function
images = convert_from_path(pdf_path)
if len(images_names)==0:
print("names is empty")
return
i=0
for img in images:
img.save(images_path+"/"+images_names[i]+".jpg", 'JPEG')
crop(images_path+"/"+images_names[i]+".jpg")
i+=1
print("Successfully converted")
def create_image_from_latex(image_name,latex):
if "rough" not in os.listdir():
os.mkdir("rough")
if "images_from_latex" not in os.listdir():
os.mkdir("images_from_latex")
f=open("rough/a.tex","w+")
f.write("\\documentclass{article}\n\\usepackage{chemfig}\n\\begin{document}\n")
f.write(latex+"\n")
f.write(r"\end{document}")
f.close()
#print(os.getcwd()+"/a.tex")
#tex="/a.tex"
pdfl = PDFLaTeX.from_texfile('rough/a.tex')
pdf, log, completed_process = pdfl.create_pdf(keep_pdf_file=True, keep_log_file=False)
f=open("rough/a.pdf","wb")
f.write(pdf)
f.close()
save_images([image_name],"a.pdf","images_from_latex")
os.remove("rough/a.pdf")
shutil.rmtree("rough")
#create_image_from_latex("new_image",lat)
def create_images_from_text_file_with_latexes(text_file):
with open(text_file) as f:
latexes=f.readlines()
ind=1
for lat in latexes:
create_image_from_latex("%0.3d_"%ind,lat)
ind+=1
#

Related

from single jpg to web conversion to all files in dir

i made python code to convert jpg into webp
from PIL import Image
import PIL
import os
import glob
# Open The Image:
image_path = "D:/..../image_name.jpg"
image = Image.open(image_path)
# Convert the image to RGB Colour:
image = image.convert('RGB')
# Save The Image as webp:
new_image_path = "D:/.../converted_image_name.webp"
image.save(new_image_path, 'webp')
#open converted image for inspection
new_image = Image.open(new_image_path)
new_image.show()
Don't know how to build a function that does the conversion for all files in folder, keeping the orginial file_names.
Here is a working solution to your problem:
from pathlib import Path
from PIL import Image
import PIL
import os
import glob
# the path to the directory containing the images
directory_path = 'D:\PATH'
def convert_images(directory_path):
for image_path in os.listdir(directory_path):
if image_path.endswith(".jpg"):
image = Image.open(os.path.join(directory_path, image_path))
# get image filename without an extension
name = Path(image_path).stem
rgb_image = image.convert('RGB')
new_image_path = f"{name}.webp"
rgb_image.save(os.path.join(directory_path, new_image_path), 'webp')
new_image = Image.open(new_image_path)
new_image.show()
continue
else:
continue
convert_images(directory_path)
final code
from pathlib import Path
from PIL import Image
import PIL
import os
import glob
# the path to the directory containing the images
directory_path = 'D:/Lokale schijf/SS2023/Python_API_imports/'
def convert_images(directory_path):
for image_path in os.listdir(directory_path):
if image_path.endswith(".jpg"):
image = Image.open(os.path.join(directory_path, image_path))
# get image filename without an extension
name = Path(image_path).stem
rgb_image = image.convert('RGB')
new_image_path = f"{name}.webp"
rgb_image.save(os.path.join(directory_path, new_image_path), 'webp')
new_image = Image.open(new_image_path)
new_image.show()
continue
convert_images(directory_path)

how to process all images on a folder with python

I have a python script to procces jpeg on a folder and overwrite the names after done
from PIL import Image, ImageOps, JpegImagePlugin, ImageEnhance
#import jpegs from Repaired folder
im = Image.open('photo_2022-06-09_19-58-19.JPG')
# Crop
im = ImageOps.crop(im, border=17)
# Constract
im = ImageOps.autocontrast(im, cutoff = 1)
#sharpness
enhancer = ImageEnhance.Sharpness(im).enhance(3)
im = ImageOps.posterize(im, bits=8)
#color
enhancer = ImageEnhance.Color(im).enhance(3)
#save all jpegs and overwrite original file names
im.save("photo_2022-06-09_19-58-19.JPG", quality="maximum")
How can i process all jpeg files in that dir have a folder "repaired" and Overwrite the name of each processed file with the original file name?
I would use glob and do something like this:
import glob
from PIL import Image, ImageOps, JpegImagePlugin, ImageEnhance
for fname in glob.glob('./repaired/*.JPG'):
im = Image.open(fname)
im = ImageOps.crop(im, border=17)
im = ImageOps.autocontrast(im, cutoff = 1)
enhancer = ImageEnhance.Sharpness(im).enhance(3)
im = ImageOps.posterize(im, bits=8)
enhancer = ImageEnhance.Color(im).enhance(3)
im.save(fname, quality="maximum")
You can print fname to see what it looks like, and process that string if you need to get the filename out, e.g. to write the file into a different folder.
So it should be like this
import os
from PIL import Image, ImageOps, JpegImagePlugin, ImageEnhance
#import jpegs from Repaired folder
for filename in os.listdir():
if filename.endswith(".jpg"):
im = Image.open(filename)
# Crop
im = ImageOps.crop(im, border=17)
# Constract
im = ImageOps.autocontrast(im, cutoff = 1)
#sharpness
enhancer = ImageEnhance.Sharpness(im).enhance(3)
im = ImageOps.posterize(im, bits=8)
#color
enhancer = ImageEnhance.Color(im).enhance(3)
#save all jpegs and overwrite original file names
im.save(filename, quality="maximum")
else:
continue

How to downgrade a pdf version from 1.7 (Acrobat 8.x) to 1.4 (Acrobat 5.x) in Python

I have a script that modifies pdf files so that they comply with some specifications that are required to be uploaded to some other app (grayscale or black and white, 300 dpi, letter sized, etc.). I'm using pdf2image, img2pdf. PIL.Image and fitz.
The problem is that when I'm done modifying the files, the pdf version upgrades from 1.4 to 1.7 and I need it to be specifically 1.4. After reading online, I found out that PyPDF2 automatically converts pdf files to 1.3. I tried that thinking that 1.3 could work, but to my surprise it did not. It HAS to be 1.4. Here is my code if it helps:
import os
from os.path import join
from tempfile import TemporaryDirectory
from pdf2image import convert_from_path
from img2pdf import convert
import PIL.Image as Image
import fitz
from PyPDF2 import PdfFileWriter, PdfFileReader
#Here's where the source pdf is located.
pdf_input = os.path.join("PDF")
#Converting pdf to images
with TemporaryDirectory() as temp_dir:
for file in os.listdir(pdf_input):
pdfName = os.fsdecode(file)
pdf_to_open = os.path.join(pdf_input, pdfName)
images = convert_from_path(
pdf_to_open,
dpi=282, #For some reason, if I put 300dpi I end up with 325 dpi.
output_folder=temp_dir,
grayscale=True,
fmt="png",
thread_count=4
)
#Iterating through images
image_list = list()
for page_number in range(1, len(images) + 1):
path = join(temp_dir, "page_" + str(page_number) + ".png")
image_list.append(path)
images[page_number-1].save(path, "PNG")
#Converting to Black and WHite
image_file = Image.open(path)
image_file = image_file.convert('1')
image_file.save(path)
#Converting images to pdf
if not os.path.exists(pdf_input):
os.mkdir(pdf_input)
pdfPath = os.path.join(pdf_input, pdfName)
with open(pdfPath, "bw") as gray_pdf:
gray_pdf.write(convert(image_list))
#Changing pdf size
src = fitz.open(gray_pdf)
doc = fitz.open()
for ipage in src:
fmt = fitz.paper_rect("Letter")
page = doc.new_page(width=fmt.width, height=fmt.height)
page.show_pdf_page(page.rect, src, ipage.number)
src.close()
doc.save(gray_pdf)
#Downgrading with PyPDF2
infile = PdfFileReader(pdfPath, 'rb')
output = PdfFileWriter()
for i in range(infile.getNumPages()):
p = infile.getPage(i)
output.addPage(p)
with open(pdfPath, 'wb') as f:
output.write(f)
I managed to do it using ghostsrcipt. I have no idea how ghostscript works, but this code worked just fine:
import sys
import ghostscript
pdfPath = "path/pdfName.pdf"
newPdfPath = "path/NEW_pdfName.pdf"
args = [
"downgradePDF",
"-sDEVICE=pdfwrite", "-dCompatibilityLevel=1.4", "-dNOPAUSE", "-dQUIET", "-dBATCH",
"-sOutputFile=" + newPdfPath, pdfPath
]
ghostscript.Ghostscript(*args)

Converting directory with images to csv file in python

Im trying to copy the pixel information of a group of images to a CSV, however when I try the following code, Im unable to create the csv. Is there any other way to copy the pixels to a csv?
from PIL import Image
import numpy as np
import sys
import os
import csv
# default format can be changed as needed
def createFileList(myDir, format='.png'):
fileList = []
print(myDir)
for root, dirs, files in os.walk(myDir, topdown=False):
for name in files:
if name.endswith(format):
fullName = os.path.join(root, name)
fileList.append(fullName)
return fileList
# load the original image
myFileList = createFileList('/test/surprise')
for file in myFileList:
print(file)
img_file = Image.open(file)
# img_file.show()
# get original image parameters...
width, height = img_file.size
format = img_file.format
mode = img_file.mode
# Make image Greyscale
img_grey = img_file.convert('L')
#img_grey.save('result.png')
#img_grey.show()
# Save Greyscale values
value = np.asarray(img_grey.getdata(), dtype=np.int).reshape((img_grey.size[1], img_grey.size[0]))
value = value.flatten()
print(value)
with open("imagenes.csv", 'a') as f:
writer = csv.writer(f)
writer.writerow(value)
I would like to generate a csv with the pixel information similar to FER2013.csv as can be see in the following image.

Take an image directory and resize all images in the directory

I am trying to convert high resolution images to something more manageable for machine learning. Currently I have the code to resize the images to what ever height and width I want however I have to do one image at a time which isn't bad when I'm only doing a 12-24 images but soon I want to scale up to do a few hundred images.
I am trying to read in a directory rather than individual images and save the new images in a new directory. Initial images will vary from .jpg, .png, .tif, etc. but I would like to make all the output images as .png like I have in my code.
import os
from PIL import Image
filename = "filename.jpg"
size = 250, 250
file_parts = os.path.splitext(filename)
outfile = file_parts[0] + '_250x250' + file_parts[1]
try:
img = Image.open(filename)
img = img.resize(size, Image.ANTIALIAS)
img.save(outfile, 'PNG')
except IOError as e:
print("An exception occured '%s'" %e)
Any help with this problem is appreciated.
Assuming the solution you are looking for is to handle multiple images at the same time - here is a solution. See here for more info.
from multiprocessing import Pool
def handle_image(image_file):
print(image_file)
#TODO implement the image manipulation here
if __name__ == '__main__':
p = Pool(5) # 5 as an example
# assuming you know how to prepare image file list
print(p.map(handle_image, ['a.jpg', 'b.jpg', 'c.png']))
You can use this:
#!/usr/bin/python
from PIL import Image
import os, sys
path = "\\path\\to\\files\\"
dirs = os.listdir( path )
def resize():
for item in dirs:
if os.path.isfile(path+item):
im = Image.open(path+item)
f, e = os.path.splitext(path+item)
imResize = im.resize((200,100), Image.ANTIALIAS)
imResize.save(f+'.png', 'png', quality=80)
resize()
You can loop over the contents of a directory with
import os
for root, subdirs, files in os.walk(MY_DIRECTORY):
for f in files:
if f.endswith('png'):
#do something
You can run through all the images inside the directory using glob. And then resize the images with opencv as follows or as you have done with PIL.
import glob
import cv2
import numpy as np
IMG_DIR='home/xx/imgs'
def read_images(directory):
for img in glob.glob(directory+"/*.png"):
image = cv2.imread(img)
resized_img = cv2.resize(image/255.0 , (250 , 250))
yield resized_img
resized_imgs = np.array(list(read_images(IMG_DIR)))
I used:
from PIL import Image
import os, sys
path = os.path.dirname(os.path.abspath(__file__))
dirs = os.listdir( path )
final_size = 244
print(dirs)
def resize_aspect_fit():
for item in dirs:
if ".PNG" in item:
print(item)
im = Image.open(path+"\\"+item)
f, e = os.path.splitext(path+"\\"+item)
size = im.size
print(size)
ratio = float(final_size) / max(size)
new_image_size = tuple([int(x*ratio) for x in size])
im = im.resize(new_image_size, Image.ANTIALIAS)
new_im = Image.new("RGB", (final_size, final_size))
new_im.paste(im, ((final_size-new_image_size[0])//2, (final_size-new_image_size[1])//2))
print(f)
new_im.save(f + 'resized.jpg', 'JPEG', quality=400)# png
resize_aspect_fit()
You can use this code to resize multiple images and save them after conversion in the same folder for let's say dimensions of (200,200):
import os
from PIL import Image
f = r' ' #Enter the location of your Image Folder
new_d = 200
for file in os.listdir(f):
f_img = f+'/'+file
try:
img = Image.open(f_img)
img = img.resize((new_d, new_d))
img.save(f_img)
except IOError:
pass
you can try to use the PIL library to resize images in python
import PIL
import os
import os.path
from PIL import Image
path = r'your images path here'
for file in os.listdir(path):
f_img = path+"/"+file
img = Image.open(f_img)
img = img.resize((100, 100)) #(width, height)
img.save(f_img)
from PIL import Image
import os
images_dir_path=' '
def image_rescaling(path):
for img in os.listdir(path):
img_dir=os.path.join(path,img)
img = Image.open(img_dir)
img = img.resize((224, 224))
img.save(img_dir)
image_rescaling(images_dir_path)

Categories

Resources