python fpdf add an image in cell - fpdf

I am generating a PDF file using FPDF in Python. Below is a code snippet, where I am creating a table using cell. The table consists of 3 columns. In the last cell of each row, I need to insert an image with a QR code. I don't know how to insert an image inside a cell.
#Create table header
height = 5
pdf.cell(w = 50, h = height, txt = 'Product', align = 'L', border = 'B', ln = 0)
pdf.cell(w = 120, h = height, txt = 'Instructioon', align = 'L', border = 'B', ln = 0)
pdf.cell(w = 20, h = height, txt = 'Video', align = 'L', border = 'B', ln = 1)
#Create the content in the first table row
pdf.cell(w = 50, h = height, txt = 'Product 1', align = 'L', ln = 0)
pdf.cell(w = 120, h = height, txt = 'some product descriptions', align = 'L', ln = 0)
#This is the cell I need to insert an image with a QR code, but I don't know how to insert the image inside a cell
pdf.cell(....)

Here is a workaround. Instead of creating the PDF directly with FPDF with limited functionality, you can create an HTML file, populate it with data, take a screen shot, and finally create a PDF.
See this Medium Article for reference: https://betterprogramming.pub/generating-stunning-pdf-reports-with-aws-and-python-a47274afe03d

Related

Text position based on another Text

I need to plot a vertical text (text 2) next to a horizontal text (text 1), if I use the the same position that I used to plot text1 they become superimposed.
I've tried to infer the second position based on the Extent property of text1, but I can't get the units right:
rec = txt.Extent;
pos_x = rec(1) + rec(3);
pos_y = rec(2);
text(pos_x,pos_y,txt2,'HorizontalAlignment','center','FontSize',sz,'Rotation',90,'Units','normalized');
I've tried also with units in pixels but that didn't worked either.
The figure is a time series, I couldn't find any convertion function.
From the documentation it seems to me that the Extent values are normalized but I dont know if anything else is needed to display the second position on those coordinates.
Does this look like what you want to do
plot with rotated text
Here is the code to make the above plot
plot(1:1.2)
h1 = text(1,1, 'Text String 1', 'verticalalignment', 'bottom');
rec = h1.Extent;
pos_x = rec(1) + rec(3);
pos_y = rec(2) + rec(4);
text(pos_x,pos_y,'Text String 2','HorizontalAlignment','left','FontSize',10,'Rotation',90,'verticalalignment', 'bottom');

Saving the image names and bounding box coordinates in different file formats in Matlab

I've written a Matlab code to crop an image with respect to a mask.
function Crop_Img = Crop_Xray(Img,Mask)
% Find borders
vertical_profile = sum(Mask,2);
horizontal_profile = sum(Mask);
indexes = find(vertical_profile >0);
upper = indexes(1);
lower = indexes(end);
indexes = find(horizontal_profile > 0);
left = indexes(1);
right = indexes(end);
% Crop Img
Crop_Img = Img(upper:lower, left:right);
end
I would like to save the individual image file names (*.png) and their respective bounding box co-ordinates [upper: lower, left: right] exactly in the format I have mentioned, in adjacent columns to a .xls, .txt, and .mat file. How should I do that?

How to avoid fliplr in the below code?

I am trying to split a region in an image into left and right. But I am avoiding a certain percentage of columns in the center from each side.
So,
I have to get the keep indexes for both left and right.
I am using fliplr to reverse array indexes of right side,
get (1:n_indices),
then again fliplr back to normal.
Can I avoid fliplr in the below code:
img1 = imread('sample4.png');
keepPercent = 0.9; %90 on both sides
columnsWithAllZeros = all(img1 == 0);
left_idx = find(~columnsWithAllZeros,1,'first');
right_idx = find(~columnsWithAllZeros,1,'last');
cent_idx = floor(mean([left_idx,right_idx]));
left_to_cent_idxs = left_idx:cent_idx;
cent_to_right_idxs = cent_idx+1:right_idx;
cent_to_right_idxs = fliplr(cent_to_right_idxs); % flip
num_leftKeep_idxs = floor(keepPercent *length(left_to_cent_idxs));
num_rightKeep_idxs = floor(keepPercent *length(cent_to_right_idxs));
right_keepImg_idxs = left_to_cent_idxs(1:num_leftKeep_idxs);
left_keepImg_idxs = cent_to_right_idxs(1:num_rightKeep_idxs);
left_keepImg_idxs = fliplr(left_keepImg_idxs); %flip back This is not needed I Know
[leftBrain_img, rightBrain_img] = deal(zeros(nrow, ncol, 'logical'));
leftBrain_img(:,left_keepImg_idxs) = img1(:,left_keepImg_idxs);
rightBrain_img(:,right_keepImg_idxs) = img1(:,right_keepImg_idxs);
rightBrain_img = cast(rightBrain_img,'uint16') .*img1;
leftBrain_img = cast(leftBrain_img,'uint16') .*img1;
figure,
subplot(131), imshow(img1,[])
subplot(132), imshow(rightBrain_img,[])
subplot(133), imshow(leftBrain_img,[])
The sample image is available here
Thanks,
Gopi
That could be done, just like #rahnema1 said. But the question is why even do it when it could be done in a much faster & simpler way!
Have a look at this code-
img1 = imread('sample4.png');
keepPercent = 0.9; %90 on both sides
columnsWithAllZeros = all(img1 == 0);
leavepercent=1-keepPercent;
idx=minmax(find(columnsWithAllZeros==0));
cent_idx = floor(mean(idx));
left_keepImg_idxs1=idx(1):cent_idx-floor(leavepercent*(cent_idx-idx(1)+1));
right_keepImg_idxs1=cent_idx+1+floor(leavepercent*(idx(2)-cent_idx+1)):idx(2);
[leftBrain_img, rightBrain_img] =deal(zeros(512, 512, 'logical'));
leftBrain_img(:,left_keepImg_idxs1) = img1(:,left_keepImg_idxs1);
rightBrain_img(:,right_keepImg_idxs1) = img1(:,right_keepImg_idxs1);
rightBrain_img = cast(rightBrain_img,'uint16') .*img1;
leftBrain_img = cast(leftBrain_img,'uint16') .*img1;
figure,
subplot(131), imshow(img1,[])
subplot(132), imshow(rightBrain_img,[])
subplot(133), imshow(leftBrain_img,[])

How to center text on a line

I would like to have a string of text that's centered on a line. I've tried this:
figure
axis([0,10,0,10])
d = 2.81;
center = 5;
line([center - d,center + d],[5,5])
th = text(center,4.9,'mmmmmmmmmmmmmmmmmmmmmm');
set(th,'HorizontalAlignment','center')
The text is aligned with the line on the right but not on the left. The above image is a screen shot. I did not consistently have this problem in saved versions of the figure.
Is there a way to center text on a line? I am not concerned about resizing the figure right now, but I would like to use the default font.
It seems that it's not possible to position text arbitrarily precise. I tried getting size of text and drawing line and re-positioning text accordingly. More about text properties here.
str1 = 'mmmmmmmmmmmmmmmmmmmmmm';
center = 5;
text_line_spacing = 0.2;
figure
axis([0,10,0,10])
% Set text initialy
th = text(0,0,str1);
% Get size of text
ext = get(th, 'Extent');
% text_width = ext(3);
% text_height = ext(4);
% Draw appropriate line
left = center - ext(3)/2;
right = center + ext(3)/2;
line([left right], [5 5])
% Reposition original text
set(th, 'Position', [left 5+text_line_spacing]);

How to put show an outline for each chart series in a RadChart?

I'm trying to customize the appearance of an ASP Web Forms RadChart where the series have FillType = Solid while still allowing visibility of the lesser point values behind larger ones. What I really want is each series to have an outline placed on top of all the fills (basically the effect of having a ChartSeriesType.Line on top of a ChartSeriesType.Area).
I've experimented with setting LineAppearance.Shadow, color transparency, and combinations of those but it's still too difficult to discern background series values.
Is there not a simple way to turn on an outline for each series when using a solid fill? OR to set the transparency of the background fill only, not the edges?
Note the transparency in the above image. I'm afraid allowing more transparency improves the visibility of background values but results in a horrible collection of pastel colors.
I suggest you to go through telerik documentation page RadControls for ASP.NET AJAX Documentation -Styling Chart Elements and this nice example Creating RadChart Programmatically - more complex example.
Code snippet:
// Define chart and titleRadChart radChart = new RadChart();
radChart.ChartTitle.TextBlock.Text = "My RadChart";
radChart.ChartTitle.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Blue;
// Define chart series
ChartSeries chartSeries = new ChartSeries();
chartSeries.Appearance.LabelAppearance.Visible = false;
chartSeries.Name = "GDP";
chartSeries.Type = ChartSeriesType.Line;
chartSeries.Appearance.LineSeriesAppearance.Color = System.Drawing.Color.BlueViolet;
// Define the items in the series
chartSeries.AddItem(1);
chartSeries.AddItem(1.5);
chartSeries.AddItem(2.0);
chartSeries.AddItem(2.5);
chartSeries.AddItem(3.5);
// visually enhance the datapoints
chartSeries.Appearance.PointMark.Dimensions.AutoSize = false;
chartSeries.Appearance.PointMark.Dimensions.Width = 5;
chartSeries.Appearance.PointMark.Dimensions.Height = 5;
chartSeries.Appearance.PointMark.FillStyle.MainColor = System.Drawing.Color.Black;
chartSeries.Appearance.PointMark.Visible = true;
// Define chart series
ChartSeries chartSeries2 = new ChartSeries();
chartSeries2.Appearance.LabelAppearance.Visible = false;
chartSeries2.Name = "GNP";
chartSeries2.Type = ChartSeriesType.Line;
chartSeries2.Appearance.LineSeriesAppearance.Color = System.Drawing.Color.Green;
// Define the items in the series
chartSeries2.AddItem(2);
chartSeries2.AddItem(3);
chartSeries2.AddItem(3.5);
chartSeries2.AddItem(4);
chartSeries2.AddItem(4.5);
// visually enhance the data points
chartSeries2.Appearance.PointMark.Dimensions.AutoSize = false;
chartSeries2.Appearance.PointMark.Dimensions.Width = 5;
chartSeries2.Appearance.PointMark.Dimensions.Height = 5;
chartSeries2.Appearance.PointMark.FillStyle.MainColor = System.Drawing.Color.Black;
chartSeries2.Appearance.PointMark.Visible = true;
// set the plot area gradient background fill
radChart.PlotArea.Appearance.FillStyle.FillType = Telerik.Charting.Styles.FillType.Gradient;
radChart.PlotArea.Appearance.FillStyle.MainColor = System.Drawing.Color.FromArgb(65, 201, 254);
radChart.PlotArea.Appearance.FillStyle.SecondColor = System.Drawing.Color.FromArgb(0, 107, 186);
// Set text and line for X axis
radChart.PlotArea.XAxis.AxisLabel.TextBlock.Text = "Years";
radChart.PlotArea.XAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Red;
radChart.PlotArea.XAxis.Appearance.Width = 3;
radChart.PlotArea.XAxis.Appearance.Color = System.Drawing.Color.Red;
// Set text and line for Y axis
radChart.PlotArea.YAxis.AxisLabel.TextBlock.Text = "%";
radChart.PlotArea.YAxis.AxisLabel.TextBlock.Appearance.TextProperties.Color = System.Drawing.Color.Red;
radChart.PlotArea.YAxis.Appearance.Width = 3;
radChart.PlotArea.YAxis.Appearance.Color = System.Drawing.Color.Red;
// Add the series to the chart, chart to page.radChart.Series.Add(chartSeries);radChart.Series.Add(chartSeries2);this.Page.Controls.Add(radChart)
Hope this help..