how can i save multiple images into separate cells in csv? - python-3.x

I have an issue with saving my data which are converted to np arrays.
I wanna save each image one cell of CSV with the (writerows) function but it says it should be iterable, and when I use the (writerow) function it saves all of the images without any separation.
for each_image in raw_data:
image_file = Image.open(each_image)
each_file_path = image_file.filename
image = cv2.imread(each_file_path)
grey_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image_from_array = Image.fromarray(grey_image)
width, height = image_from_array.size
format = image_from_array.format
mode = image_from_array.mode
img_grey = image_from_array.convert('L')
value = np.asarray(img_grey.getdata(), dtype=np.int).reshape((img_grey.size[1], img_grey.size[0]))
value = value.flatten()
print(value)
this is my code and any help with saving in CSV separately would be appreciated!

just use newline = '' when u open your CSV file.

Related

Improve resolution ImageMagick

I'm converting PDF's into JPG's. But the quality decreases a lot whereby I can't extract text out of these images. I know there's something as resolution, but where do I need to place it in this code.
def pdf_to_image(filename):
for pdf_file in pdf_files:
pdf = wi(filename=pdf_file)
converted = pdf.convert("jpg")
base_file_name, _ = os.path.splitext(pdf_file)
i = 1
for img in converted.sequence:
page = wi(image=img)
page_file_name = f"{base_file_name}_{i}.jpg"
page.save(filename=page_file_name)
i = i + 1

How to save all figures in pdf file in python created from seaborn style & dataframe?

This code gives me output of grid as 1 with style background.
def plot(grid):
cmap = sns.light_palette("red", as_cmap=True)
figure = pd.DataFrame(grid)
figure = figure.style.background_gradient(cmap=cmap, axis=None)
display(figure)
I wanted to store multiples images such as 1 in a single pdf file generated by Fun 'plot'.In case of matplotlib
from matplotlib.backends.backend_pdf import PdfFile,PdfPages
pdfFile = PdfPages("name.pdf")
pdfFile.savefig(plot)
pdfFile.close()
can do this. but for this case I am facing issues because it is dataframe or I am using searborn background_style.
could you please suggest to store output of above in single pdf file or png or jpg.
Here is my code to save all open figures to a pdf, it saves each plot to a separate page in the pdf.
from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('C:\path\filename.pdf') #path to where you want to save the pdf
figNum = plt.get_fignums() #creates a list of all figure numbers
for i in range(len(figNum)): #loop to add each figure to pdf
pp.savefig(figNum[i]) #uses the figure number in the list to save it to the pdf
pp.close() #closes the opened file in memory
We can creat folder name 'image' and store all images of code output in png format.we will have to use dataframe image for that.
import dataframe_image as dfi
from PIL import Image
def plot(grid):
cmap = sns.light_palette("red", as_cmap=True)
figure = pd.DataFrame(grid)
figure = figure.style.background_gradient(cmap=cmap, axis=None)
dfi.export(figure, f'image\df_styled.png, max_cols=-1)

Writing from a list to a Word document

I have written code (shown below) that reads in a word document, and writes each "run" with formatting into a list. Thus creating a list of formatted runs, so that I may use some or all at a later time. Next, I have written code to write said runs from said list to a new document. The issue is I keep getting an error stating " "Run" object is not iterable". I don't know if the issue is that writing all of the paragraph information to a list that can be recalled can not be done this way, or if I am trying to write it to the document the wrong way.
import tkinter as tk
from tkinter.filedialog import askopenfilename
from docx import Document # Invokes Document command from docx
def get_para_data(output_doc_name, paragraph):
"""
Write the run to the new file and then set its font, bold, alignment, color etc. data.
"""
output_run = []
output_para = output_doc_name.add_paragraph()
for run in paragraph.runs:
if paragraph:
output_run = output_para.add_run(run.text)
# Run's bold data
output_run.bold = run.bold
# Run's italic data
output_run.italic = run.italic
# Run's underline data
output_run.underline = run.underline
# Run's color data
output_run.font.color.rgb = run.font.color.rgb
# Run's font data
output_run.style.name = run.style.name
# Paragraph's alignment data
output_para.paragraph_format.alignment = paragraph.paragraph_format.alignment
else:
output_run = []
return output_run
# IMPORT WORD DOCUMENT
root = tk.Tk()
root.withdraw()
# returns the file path as variable for future use.
doc_path = askopenfilename(title="Choose Word File")
# Imports Word Document to Modify.
document = Document(doc_path)
# Number of paragraphs in document.
t = len(document.paragraphs)
# Preallocation of list.
output_paragraph = [None]*t
result = Document()
# Begin loop to create list of paragraph data using function created above.
i = 0
for para in document.paragraphs:
output_paragraph[i] = get_para_data(result, document.paragraphs[i])
# Write desired portion of document into a new document.
document_new = Document()
new_line = []
a = 0
for out in output_paragraph:
# Check to Verify it is not a blank line/return.
if output_paragraph:
new_line[a] = document_new.add_paragraph(output_paragraph[a])
a += 1
# if it is a blank line/return write blank line return.
else:
document_new.add_paragraph(text='\\r', style=None)
a += 1
My expected results were that the text in the new document was the same as in the previous document, but in an order i choose. Similar to a copy and paste, but I wanted the ability to choose which portions I "pasted" and when.

How to get an image (inlineshape) from paragraph python docx

I want to read the docx document paragraph by paragraph and if there is a picture (InlineShape), then process it with the text around it. The function Document.inline_shapes will give the list of all inline shapes in the document. But I want to get the one, that appears exactly in the current paragraph if exists...
An example of code:
from docx import Document
doc = Document("test.docx")
blip = doc.inline_shapes[0]._inline.graphic.graphicData.pic.blipFill.blip
rID = blip.embed
document_part = doc.part
image_part = document_part.related_parts[rID]
fr = open("test.png", "wb")
fr.write(image_part._blob)
fr.close()
(this is how I want to save these pictures)
Assume your paragraph is par, you may use the following code to find the images
import xml.etree.ElementTree as ET
def hasImage(par):
"""get all of the images in a paragraph
:param par: a paragraph object from docx
:return: a list of r:embed
"""
ids = []
root = ET.fromstring(par._p.xml)
namespace = {
'a':"http://schemas.openxmlformats.org/drawingml/2006/main", \
'r':"http://schemas.openxmlformats.org/officeDocument/2006/relationships", \
'wp':"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"}
inlines = root.findall('.//wp:inline',namespace)
for inline in inlines:
imgs = inline.findall('.//a:blip', namespace)
for img in imgs:
id = img.attrib['{{{0}}}embed'.format(namespace['r'])]
ids.append(id)
return ids

np.savetxt tuple converted into array

I am trying to pull out a .wav file into a txt file for analysis but I am getting a single value in Audio.txt in the end instead of the all the data points(value). I unable to figure out where I am going wrong in the piece of code given below:
import wave, struct
import numpy as np
waveFile = wave.open('test1.wav', 'r')
length = waveFile.getnframes()
for i in range(0,length):
waveData = waveFile.readframes(1)
data = struct.unpack("<h", waveData)
data_x = np.array(int(data[0])) #saving all value in a single array
#print(int(data[0]))
np.savetxt("Audio.txt", data_x.reshape(1,),delimiter=",")
Any help ??
Thank You.

Resources