Rotating an image after being added in a PDF using Reportlab - image

I'm trying to create a PDF document with an image that will be rotated.
I can successfully create the PDF document, add the image and save it, but as soon as I try rotating it, I'm having many issues.
One thing I'm trying to understand is where is the axe for the rotation, is it at 0,0 (bottom left for PDF) or somewhere else?
Here's my current running code:
output = BytesIO()
# create a new PDF with Reportlab
c = canvas.Canvas(output)
c.saveState()
c.translate(X?, Y?) # TODO find this !
c.rotate(45)
c.drawImage('path/to/image.png', position_left, position_top, width=img_width, height=img_height, mask='auto')
c.restoreState()
c.save()
(Since PDF documents (0,0) point is at bottom right, I have position_left and position_top that refers to the top left point of the document, where I want to place the image).
My issue here is that I don't know how what values to put on c.translate(X?, Y?) to make the image rotate on its center axis, i.e. stays at the same position on the document, but rotate on itself from its center point.
Is using c.translate(X?, Y?) would work or do I need to use advanced mechanisms to rotate "just" the image on the PDF document? If so, can you point me to the right track?
Thank you for your help.

You can use the technique mentioned in below SO Thread
A simple method for rotate images in reportlab
from reportlab.platypus.flowables import Image
class RotatedImage(Image):
def wrap(self,availWidth,availHeight):
h, w = Image.wrap(self,availHeight,availWidth)
return w, h
def draw(self):
self.canv.rotate(90)
Image.draw(self)
I = RotatedImage('../images/somelogo.gif')

Related

Place image at bottom left corner of pdf with iText

I'm having some trouble placing an image in the bottom-left corner of a PDF document.
Here's my code:
PdfReader reader = new PdfReader("source.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(pdfTarget));
Image qrImg = Image.getInstance("qrcode.png");
qrImg.setAbsolutePosition(0,0);
// place the image at the i-th page
PdfContentByte content = stamper.getOverContent(i);
content.addImage(qrImg);
This works for almost every pdf document I tried unless a single one that you can find here: https://ufile.io/50016
For this document the bottom left corner starts at (50,50) so the absolute position should be (50,50) that is incorrect for all the others pdfs.
I can't find a way to place the image at (0,0) or any other fixed absolute position that results in placing it always at the bottom left corner.
Any advice?
Pdf documents describe the page with a key called MediaBox. This is the raw size of the page. There's another key called CropBox that defines the visible area of the page. In your document the cropbox start at 54,55.4 and that's the offset you'll have to apply to the image. Check PdfReader.getCropBox() to get the dimensions.

RMagick: Rotate image with origin (matrix transform)

tl;dr: How can I use RMagick to rotate an image at a given point.
I created a website which allows user to manipulate 2 images online and composite these images on the server side to a single image.
I use regular css transform: rotate() for rotation on the client side.
On the server side I'm rotating the image using RMagick's rotate! method but the results differ from the web version.
(Presumably because of an origin issue (e.g. at which point of the image the rotation takes place)).
The web version rotates at the center of the image (transform-origin: 50% 50%). Unfortunately RMagick doesn't by default.
I read through the RMagick docs and found affine_transform which accepts a matrix and transforms the image. Is this the right method to use, if so how? I tried passing the css-matrix to that function but it doesn't work.
Somewhere in the RMagick documentation I read that Magick::Image#rotate accepts 3 parameters (degree, originX, originY) but my version says that it only accepts 2 parameters (and actually requires the second parameter to be a string...).
My code:
require 'rmagick'
include Magick
#label = Label.last
image = #label.image
json = #label.processing
image.background_color = "none"
image.resize!(json["size"]["width"], json["size"]["height"])
# how can I set the rotation origin to the center of the image?
image.rotate!(json["rotation"].to_i)
overlay.composite!(image, json["position"]["x"], json["position"]["y"],
Magick::OverCompositeOp)
overlay.write("output5.png")
The output I'm currently getting is this. The blue square is actually imagein the code. The heart is overlay.
My desired output looks like this: (Ignore background, border and controls)
If I don't use rotation at all, both images are identically. That's why I assume it's an rotation issue. Both images are equally in width and height.
edit: Apparently only Magick::RVG::Image accepts the originX & originY parameters I mentioned above. Still not able to transform the current image into a RVG Image. It might solve the issue if I can transform my Magick::Image into Magick::RVG::Image.
Okay I've found a solution for this problem. I have to use RVG which is a module to create vector graphics.
The #rotate(degree, originX, originY) method is defined in the RVG::Image class so I had to wrap my Magick::Image object with:
require 'rvg/rvg'
image = RVG::Image.new(magick_image, width, height, x, y)
canvas = RVG.new(width, height)
canvas.use image
overlay.composite!(canvas.draw, ...)
Writing this on mobile, I will add a detailed answer asap.

keep new image when drawing lines by dragging the mouse in matlab

I am new in matlab GUI. I want to drawing lines by dragging the mouse. I found this issue but I want save new image with drawn lines. If i run this line, it will show me the same image.
imshow(im);
How can I get new image with drawn lines and for example show it?
You can also grab the image data directly in the Command Prompt. Once you draw all of your lines on your figure window, you can use getframe, which takes a snapshot of the current frame in focus. In this case, this should be your image with the drawn lines. When you call getframe, this will give you a structure with an element called cdata. This will give you an RGB array of what was seen in the figure (without the menu bars... just the figure data itself).
Example:
im = imread('cameraman.tif');
imshow(im);
h = getframe;
out = h.cdata;
figure;
imshow(out); %// Should give you the same image as the figure
You can use print to print the figure to some file. Not sure if you want to have exact reproduction of your line and image. In this case the best way probably would be to store the coordinates and properties of the line and use that to draw it on the image when you want to display it again later.

Remove imellipse after double click MATLAB

Just wondering how I can get rid of the imellipse after calling it in MATLAB. Currently I call it, double click it to plot the ellipse on my image, then I want to remove the ellipse tool.
I have a GUI, which I click a pushbutton to make an imellipse. After double clicking the imellipse, an outline is plotted onto my image and the dimensions saved to some matrix.
After double clicking it, resulting in a plot onto my image, I would like the ellipse ring (created from the function) to disappear. It just seems to stick around on the image (is this normal, or should it disappear?).
I can include my code, it just seems irrelevant to the problem (remove imellipse after double clicking).
I hope this is more clear!
Thanks!
imellipse creates a ROI on the figure, which can be removed if you delete the associated ROI object.
Let's suppose you have used imellipse like this -
Lesion = imellipse(handles.axes1);
Then, get all the information that you need from Lesion and then delete it. For example, if you need the mask information from it, store it somewhere.
LesionMask = Lesion.createMask();
Now, delete the ROI object which is Lesion.
delete(Lesion); %// Deletes the ROI related to imellipse
Read more about how to handle ROIs at Region-of-interest (ROI) base class Documentation

How do you programatically change the source of an image whose name is ambiguous?

How do you say or achieve container.child.source = image.png?
I have a hexagon map built by specifying width and height. I draw a wireframe and place a base image for each hex into a canvas. Next, the canvas listens for a mouse click. I then run a calculation to determine which hex the mouse click was closest to. My intent is to change the source of the image that the user clicked on.
I know that mapSlate.getChildByName(mapProperties[closestHex]['baseName']) is the intended hex but I can't quite get to the point of doing a .source as Flex doesn't know that the selected object is an image.
If u are sure that mapSlate.getChildByName(mapProperties[closestHex]['baseName']) is the intended hex and that it is in fact an Image, can't you cast it into Image and change the source like:
Image(mapSlate.getChildByName(
mapProperties[closestHex]['baseName'])).source = "image.png";
or
(mapSlate.getChildByName(
mapProperties[closestHex]['baseName']) as Image).source = "image.png";

Resources