Scale and position image in Scribus - python

I am customizing the built-in CalendarWizard Python script in Scribus to add birthdays with pictures. I have the profile pictures in a folder for each person and I would like to save scale and position information for these pictures, so they are automatically applied when the calendar is generated.
The image box is created like:
kep = createImage(self.marginl + colCnt * self.colSize,
self.calHeight + szovegsor + rowCnt * self.rowSize,
self.colSize, kepsor)
Then I fill the box with the photo:
szkep = 'C:\\profilepics\\' + sznapos + '.jpg'
kkep = loadImage(szkep,kep)
The sznapos variable contains the name of the current birthday person. So far we don't have multiple birthdays.
The next action would be to scale the loaded image. I can fit to the box:
setScaleImageToFrame(1, 1, kep)
This works.
But what I would like is to scale the image by a given value, but I am not sure how.
Tried, first with static values 2 ways:
setImageScale(0.1,0.1,kep)
scaleImage(0.1, 0.1, kep)
Expected to scale the image to 10%, but remains at 100%. No error is raised, just nothing happens. Can somebody please tell me what am I doing wrong?
Edit:
I tried to shift the image to filter out other possible issues and this works as expected:
setImageOffset(10,10,kep)
The image is shifted with 10 points both directions. Only scaling doesn't work.

Finally, I found a workaround here:
http://forums.scribus.net/index.php?topic=94.0
In my case:
setProperty(kep, 'imageXScale', xscale)

Related

Programming a picture maker template in Python possible?

I'm looking for a library that enables to "create pictures" (or even videos) with the following functions:
Accepting picture inputs
Resizing said inputs to fit given template / scheme
Positioning the pictures in pre-set up layers or coordinates
A rather schematic approach to look at this:
whereas the red spots are supposed to represent e.g. text, picture (or if possible video) elements.
The end goal would be to give the .py script multiple input pictures and the .py creating a finished version like mentioned above.
Solutions I tried were looking into Python PIL, but I wasn't able to find what I was looking for.
Yes, it is possible to do this with Python.
The library you are looking for is OpenCV([https://opencv.org][1]/).
Some basic OpenCV python tutorials (https://docs.opencv.org/master/d9/df8/tutorial_root.html).
1) You can use imread() function to read images from files.
2) You can use resize() function to resize the images.
3) You can create a empty master numpy array matching the size and depth(color depth) of the black rectangle in the figure you have shown, resize your image and copy the contents into the empty array starting from the position you want.
Below is a sample code which does something close to what you might need, you can modify this to suit your actual needs. (Since your requirements are not clear I have written the code like this so that it can at least guide you.)
import numpy as np
import cv2
import matplotlib.pyplot as plt
# You can store most of these values in another file and load them.
# You can modify this to set the dimensions of the background image.
BG_IMAGE_WIDTH = 100
BG_IMAGE_HEIGHT = 100
BG_IMAGE_COLOR_DEPTH = 3
# This will act as the black bounding box you have shown in your figure.
# You can also load another image instead of creating empty background image.
empty_background_image = np.zeros(
(BG_IMAGE_HEIGHT, BG_IMAGE_WIDTH, BG_IMAGE_COLOR_DEPTH),
dtype=np.int
)
# Loading an image.
# This will be copied later into one of those red boxes you have shown.
IMAGE_PATH = "./image1.jpg"
foreground_image = cv2.imread(IMAGE_PATH)
# Setting the resize target and top left position with respect to bg image.
X_POS = 4
Y_POS = 10
RESIZE_TARGET_WIDTH = 30
RESIZE_TARGET_HEIGHT = 30
# Resizing
foreground_image= cv2.resize(
src=foreground_image,
dsize=(RESIZE_TARGET_WIDTH, RESIZE_TARGET_HEIGHT),
)
# Copying this into background image
empty_background_image[
Y_POS: Y_POS + RESIZE_TARGET_HEIGHT,
X_POS: X_POS + RESIZE_TARGET_WIDTH
] = foreground_image
plt.imshow(empty_background_image)
plt.show()

Fastest way to detect time taken for change to be applied on image Preview which is on screen using python

I am editing the image , by changing Brightness , Contrast , Saturation .
I wanted to calculate the the time it takes to apply these changes on Preview of image .
I mean , when I change Brightness from 0 to 10 (Using the automation script): So Brightness in the original image must increases.
So I need to calculate the time, it takes to change Original image preview to the Changed image Preview .
One way I know to do this is :
We need to take the screen shot of the original image Preview
Make change in brightness and start timer
Take screenshot Preview and Continuously Compare with the original image till we find the difference .
Stop the timer .
So the Time taken will be difference between the start time and Stop time .
But here the problem is we will not see the accuracy , as there is sometime between one screenshot to another due to compare of original and Changed Preview image .
Can someone help me over here to find the accuracy .
Thanks!
I'd do something like this:
from time import sleep, time
from os import stat
filename = 'image.jpeg'
last = None
while True:
s = stat(filename)
if last and last != s:
print(time(), s)
last = s
sleep(0.01)
i.e. use the standard file system stat to look for changes to the file 100 times a second…
seems to work when I've played

get_y() value of the image bottom in fpdf in python

I'm using Python FPDF to generate pdf. The pdf generally contains text followed by images followed by text and so on. But the cells that contains text are overlapping with the images above them. I tried to calculate image length and pass that to set_y for next cell to avoid overlapping, still no luck. So I tried using get_y() but it returns the 'y' value of previous text cell instead of the bottom of the image.
So how can I get the 'y-coordinate' of the bottom of the image ?
I was facing the same issue and I resolved it by using the pillow library.
The Pillow library will give us the height and width of the image in pixels. And if you divide it by 10 it will work as we needed.
After this, I am abled to get the y coordinate exactly below the inserted image. I inserted another image exactly below the previous image.
y = pdf.get_y()
img_height = Image.open(imagePath).height/10
y = y + img_height + 10
pdf.set_y(y)
That's how you will get it and to set the y coordinate I used pdf.set_y(y).

Overlaying coloured pixels on top of an image using Python Imaging Library

Okay, first thing first. This is a near duplicate of this question.
However, the issue I am facing is slightly different in a critical way.
In my application, I read a generic file name in, load said image, and display it. Where it gets tricky is I have overlay the appearance of being 'highlighted'. To do this, I was using the Image.blend() function, and blending it with a straight yellow image.
However, when dealing with blend, I was fighting the error that the two images are not compatible to be blended. To solve this, I opened the sample image I had in paint, and just pasted yellow over the whole thing, and saved it as a copy.
It just occurred to me that this will fail when a different type of image is read in by file name. Remember this needs to be generic.
So my question is: Instead of making a copy of the image manually, can I get python to generate one by copying the image and modifying it so it is solid yellow? Note: I do not need to save it after, so just making it happen is enough.
Unfortunately, I am not allowed to share my code, but hopefully the following will give an idea of what I need:
from PIL import Image
desiredWidth = 800
desiredHeight = 600
primaryImage = Image.open("first.jpg").resize((desiredWidth, desiredHeight), Image.ANTIALIAS)
# This is the thing I need fixed:
highlightImage = Image.open("highlight.jpg").resize((desiredWidth, desiredHeight), Image.ANTIALIAS)
toDisplay = Image.blend(primaryImage, highlightImage, 0.3)
Thanks to anyone who can help.
Sounds like you want to make a new image:
fill_color = (255,255,0) #define the colour as (R,G,B) tuple
highlightImage = Image.new(primaryImage.mode, #same mode as the primary
primaryImage.size, #same size as the primary
fill_color)#and the colour defined above
this creates a new image with the same mode and size as the already opened image, but with a solid colour. Cheers.
Also if you are using Pillow instead of original PIL you can even get the color by name:
from PIL.ImageColor import getcolor
overlay = 'yellow'
fill_color = getcolor(overlay, primaryImage.mode)

opencv zoom function strange results

i am trying to write a zoom function which looks something like this:
centre = ((im.width-1)/2, (im.height-1)/2)
width = int(im.width/(2.0*level))
height = int(im.height/(2.0*level))
rect = (centre[0]-width, centre[1]-height, width*2, height*2)
dst = cv.GetSubRect(im, rect)
cv.Resize(dst, im)
when I use exactly what is written above, I get an odd result where the bottom half of the resultant image is distorted and blurry. However when I replace the line cv.Resize(dst, im) with
size = cv.CloneImage(im)
cv.Resize(dst, size)
im = size
it works fine. Why is this? is there something fundamentally wrong with the way i am performing the zoom?
cv.Resize requires source and destination to be separate memory locations.
Now in the first snippet of your code, you are using cv.GetSubRect to generate an object pointing to area of image which you wish to zoom in. Here the new object is NOT pointing to a new memory location. It is pointing to a memory location which is a subset of original object.
Since cv.Resize requires both the memory locations to be different, what you are getting is a result of undefined behavior.
In the second part of your code you are fulfilling this criteria by using cv.CloneImage.
you are first creating a copy of im (i.e. size. however you could have used a blank image aswell) and then you are using cv.Resize to resize dst and write the resulting image in size.
My advice is to go through the function documentation before using them.

Categories