How do I efficiently add random vertical segments into a numpy array? - python

I am trying to simulate rain using NumPy, they say an image is more than a thousand words so here is a description longer than two thousand words:
I already wrote the code, but I think my implementation is inefficient, so I want to know if NumPy has any builtin functions that can speed up the process:
import numpy as np
from PIL import Image
from random import random, randbytes
def rain(width, strikes=360, color=True, lw=3):
assert not width % 16
height = int(width * 9 / 16)
img = np.zeros((height, width, 3), dtype=np.uint8)
half = height / 2
for i in range(strikes):
x = round(random() * width)
y = round(height - random() * half)
x1 = min(x + lw, width - 1)
if color:
rgb = list(randbytes(3))
else:
rgb = (178, 255, 255)
img[0:y, x:x1] = rgb
return img
img1 = Image.fromarray(rain(1920))
img1.show()
img1.save('D:/rain.jpg', format='jpeg', quality=80, optimize=True)
img2 = Image.fromarray(rain(1920, color=False))
img2.show()
img2.save('D:/rain_1.jpg', format='jpeg', quality=80, optimize=True)

I was able to improve by 2 to 4 times faster.
Since raindrops do not stop in the upper half of the image, the upper half can be stretched out from the lower half after all strikes end.
Since broadcasting tuples is relatively slow, use 32-bit format color instead.
def rain(width=1920, strikes=360, color=True, lw=3):
assert not width % 16
height = int(width * 9 / 16)
img = np.zeros((height, width), dtype=np.uint32)
half = height / 2
upper_bottom = int(half) - 1
alpha = 255 << 24
# Paint background.
# The upper half will always be overwritten and can be skipped.
img[upper_bottom:] = alpha
for i in range(strikes):
x = round(random() * width)
y = round(height - random() * half)
x1 = min(x + lw, width - 1)
if color:
# Pack color into int. See below for details.
rgb = int.from_bytes(randbytes(3), 'big') + alpha
else:
# This is how to pack color into int.
r, b, g = 178, 255, 255
rgb = r + (g << 8) + (b << 16) + alpha
# Only the lower half needs to be painted in this loop.
img[upper_bottom:y, x:x1] = rgb
# The upper half can simply be stretched from the lower half.
img[:upper_bottom] = img[upper_bottom]
# Unpack uint32 to uint8 x4 without deep copying.
img = img.view(np.uint8).reshape((height, width, 4))
return img
Note:
Endianness is ignored. May not work on some platforms.
Performance is greatly degraded if the image width is very large.
If you are going to convert img to PIL.Image, compare its performance too as it is also improved.
Because of the rain overlaps each other (which makes removing for-loop hard) and because the strikes are not so many (which makes the room for improvement small), I find it difficult to optimize further. Hopefully this is enough.

So the easiest way to speed up code using NumPy is to utilize broadcasting and element-by-element operations, so that less efficient for-loops can be avoided. Below is a performance comparison between my algorithm (rain2) and OP’s (rain1):
import numpy.random as npr
from random import random, randbytes
from PIL import Image
import profile
def rain1(width, strikes=360, color=True, lw=3):
assert not width % 16
height = int(width * 9 / 16)
img = np.zeros((height, width, 3), dtype=np.uint8)
half = height / 2
for i in range(strikes):
x = round(random() * width)
y = round(height - random() * half)
x1 = min(x + lw, width - 1)
if color:
rgb = list(randbytes(3))
else:
rgb = (178, 255, 255)
img[0:y, x:x1] = rgb
return img
def rain2(width, strikes=360, color=True, lw=3):
assert not width % 16
height = width*9//16
[inds,] = np.indices((width,))
img = np.zeros((height, width, 4), dtype=np.uint8)
img[:,:,3] = 255
half = height/2
# randint from numpy.random lets you
# define a lower and upper bound,
# and number of points.
x = list(set(npr.randint(0, width-lw-1, (strikes,))))
x = np.sort(x)
y = npr.randint(half, height, (len(x),))
if color:
rgb = npr.randint(0, 255, (len(x), 3), dtype=np.uint8)
else:
rgb = np.array([178, 255, 255], dtype=np.uint8)
for offset in range(lw):
img[:,x+offset,3] = 0
img[:,x+offset,:3] = rgb
for xi, yi in zip(x, y):
img[0:yi,xi:xi+lw,3] = 255
return img
def example_test_old(strikes, disp_im=True):
img1 = Image.fromarray(rain1(1920, strikes=strikes))
if disp_im: img1.show()
img1.save('rain1.jpg', format='jpeg', quality=80, optimize=True)
img2 = Image.fromarray(rain1(1920, strikes=strikes, color=False))
if disp_im: img2.show()
img2.save('rain1.jpg', format='jpeg', quality=80, optimize=True)
def example_test_new(strikes, disp_im=True):
img1 = Image.fromarray(rain2(1920, strikes=strikes))
if disp_im: img1.show()
img1.save('rain2.png', format='png', quality=80, optimize=True)
img2 = Image.fromarray(rain2(1920, strikes=strikes, color=False))
if disp_im: img2.show()
img2.save('rain2.png', format='png', quality=80, optimize=True)
if __name__ == "__main__":
# Execute only if this module is not imported into another script
example_test_old(360)
example_test_new(360)
profile.run('example_test_old(100000, disp_im=False)')
profile.run('example_test_new(100000, disp_im=False)')
On my PC this speeds it up by a factor of 14.5!
Hope this helps.

Related

Setting Mandelbrot Python Image Background Color to Cyan

How do I set the Mandelbrot Set background to cyan? I don't understand the code.
Here's the code:
# Python code for Mandelbrot Fractal
# Import necessary libraries
from PIL import Image
from numpy import complex, array
import colorsys
# setting the width of the output image as 1024
WIDTH = 1024
# a function to return a tuple of colors
# as integer value of rgb
def rgb_conv(i):
color = 255 * array(colorsys.hsv_to_rgb(i / 255.0, 1.0, 0.5))
return tuple(color.astype(int))
# function defining a mandelbrot
def mandelbrot(x, y):
c0 = complex(x, y)
c = 0
for i in range(1, 1000):
if abs(c) > 2:
return rgb_conv(i)
c = c * c + c0
return (0, 0, 0)
# creating the new image in RGB mode
img = Image.new('RGB', (WIDTH, int(WIDTH / 2)))
pixels = img.load()
for x in range(img.size[0]):
# displaying the progress as percentage
print("%.2f %%" % (x / WIDTH * 100.0))
for y in range(img.size[1]):
pixels[x, y] = mandelbrot((x - (0.75 * WIDTH)) / (WIDTH / 4),
(y - (WIDTH / 4)) / (WIDTH / 4))
# to display the created fractal after
# completing the given number of iterations
img.show()
I would like to set the background color to cyan. More Info needs to be entered here in order for me to post but I have no more info.
Thanks.
Neo
try to change "mandelbrot" function to
def mandelbrot(x, y):
c0 = complex(x, y)
c = 0
for i in range(1, 1000):
if abs(c) > 2:
return (0, 0, 0)
c = c * c + c0
return (0, 255, 255)
Final return statement is a background color

Animate static image

I have a static image that I would like to animate to appear like this (except starting from a black image, not a white image):
(image is from this post: Create animated gif from static image)
Here is the code:
import random
import imageio
import numpy as np
from PIL import Image
img = Image.open('/Users/tom/Desktop/sink.jpeg')
pixels = img.load()
width, height = img.size
img2 = Image.new('RGB', img.size, color='black')
pixels2 = img2.load()
coord = []
for x in range(width):
for y in range(height):
coord.append((x, y))
images = []
while coord:
x, y = random.choice(coord)
pixels2[x, y] = pixels[x, y]
coord.remove((x, y))
if len(coord) % 500 == 0:
images.append(np.array(img2))
imageio.mimsave('/Users/tom/Desktop/sink.gif', images)
When I run the code, the script never stops/outputs anything. Anyone know why?
Your code works, it is just very slow. If you are okay with a transparent background you can do something like this:
import numpy as np
import imageio.v3 as iio
rng = np.random.default_rng()
px_per_iter = 1000
img = iio.imread("imageio:chelsea.png")
n_pixels = img.shape[0] * img.shape[1]
batches = int(np.ceil(n_pixels / px_per_iter)) # number of frames
pixels = rng.permutation(n_pixels) # order in which pixels are revealed
frames = np.zeros((batches + 1, *img.shape[:2], 4), dtype=np.uint8)
for batch_idx in range(batches):
idx_batch = pixels[px_per_iter*batch_idx:px_per_iter*(batch_idx+1)]
y_idx, x_idx = np.unravel_index(idx_batch, img.shape[:2])
frame = frames[batch_idx+1]
frame[y_idx, x_idx, :3] = img[y_idx, x_idx]
frame[y_idx, x_idx, 3] = 255 # make added pixels non-transparent
iio.imwrite("fancy.gif", frames, loop=True)
(500kb GIF)
If you need the black background, you can use something like this; however, be aware that it will produce larger files:
import numpy as np
import imageio.v3 as iio
rng = np.random.default_rng()
px_per_iter = 1000
img = iio.imread("imageio:chelsea.png")
n_pixels = img.shape[0] * img.shape[1]
batches = int(np.ceil(n_pixels / px_per_iter)) # number of frames
pixels = rng.permutation(n_pixels) # order in which pixels are revealed
frames = np.zeros((batches + 1, *img.shape), dtype=np.uint8)
for batch_idx in range(batches):
idx_batch = pixels[px_per_iter*batch_idx:px_per_iter*(batch_idx+1)]
y_idx, x_idx = np.unravel_index(idx_batch, img.shape[:2])
frame = frames[batch_idx+1]
frame[:] = frames[batch_idx]
frame[y_idx, x_idx] = img[y_idx, x_idx]
iio.imwrite("fancy.gif", frames)
(result exceeds 2MB, which is SO's limit)

Crop entire image with the same cropping size with PIL in python

I have some problem with my logic on PIL python. My goal is to crop one image entirely in 64x64 size from the left-top corner to botom-right corner position. I can do one time cropping operation, but when I tried to crop an image entirely with looping, I am stuck with the looping case in the middle.
In the first looping, I can crop ((0, 0, 64, 64)). But then I cannot figure the looping part to get the next 64x64s to the left and to the bottom with PIL. As the first 2-tuple is the origin position point, the next tuple is for the cropping size.
any help will be really appreciated as I am starting to learn python.
import os
from PIL import Image
savedir = "E:/Cropped/OK"
filename = "E:/Cropped/dog.jpg"
img = Image.open(filename)
width, height = img.size
start_pos = start_x, start_y = (0,0)
cropped_image_size = w, h = (64, 64)
frame_num = 1
for col_i in range (width):
for row_i in range (height):
x = start_x + col_i*w
y = start_y + row_i*h
crop = img.crop((x, y, x+w*row_i, y+h*col_i))
save_to= os.path.join(savedir, "counter_{:03}.jpg")
crop.save(save_to.format(frame_num))
frame_num += 1
You can use the range() function to do the stepping for you (in blocks of 64 in this case), so that your cropping only involves simple expressions:
import os
from PIL import Image
savedir = "E:/Cropped/OK"
filename = "E:/Cropped/dog.jpg"
img = Image.open(filename)
width, height = img.size
start_pos = start_x, start_y = (0, 0)
cropped_image_size = w, h = (64, 64)
frame_num = 1
for col_i in range(0, width, w):
for row_i in range(0, height, h):
crop = img.crop((col_i, row_i, col_i + w, row_i + h))
save_to= os.path.join(savedir, "counter_{:03}.jpg")
crop.save(save_to.format(frame_num))
frame_num += 1
Other than that, your code works as expected.

How to resize text for cv2.putText according to the image size in OpenCV, Python?

fontScale = 1
fontThickness = 1
# make sure font thickness is an integer, if not, the OpenCV functions that use this may crash
fontThickness = int(fontThickness)
upperLeftTextOriginX = int(imageWidth * 0.05)
upperLeftTextOriginY = int(imageHeight * 0.05)
textSize, baseline = cv2.getTextSize(resultText, fontFace, fontScale, fontThickness)
textSizeWidth, textSizeHeight = textSize
# calculate the lower left origin of the text area based on the text area center, width, and height
lowerLeftTextOriginX = upperLeftTextOriginX
lowerLeftTextOriginY = upperLeftTextOriginY + textSizeHeight
# write the text on the image
cv2.putText(openCVImage, resultText, (lowerLeftTextOriginX, lowerLeftTextOriginY), fontFace, fontScale, Color,
fontThickness)
It seems fontScale does not scale text according to the image width and height because the text is almost in the same size for different sized images. So how can I resize the text according to the image size so that all the text could fit in the image?
Here is the solution that will fit the text inside your rectangle. If your rectangles are of variable width, then you can get the font scale by looping through the potential scales and measuring how much width (in pixels) would your text take. Once you drop below your rectangle width you can retrieve the scale and use it to actually putText:
def get_optimal_font_scale(text, width):
for scale in reversed(range(0, 60, 1)):
textSize = cv.getTextSize(text, fontFace=cv.FONT_HERSHEY_DUPLEX, fontScale=scale/10, thickness=1)
new_width = textSize[0][0]
if (new_width <= width):
print(new_width)
return scale/10
return 1
for this worked!
scale = 1 # this value can be from 0 to 1 (0,1] to change the size of the text relative to the image
fontScale = min(imageWidth,imageHeight)/(25/scale)
just keep in mind that the font type can affect the 25 constant
Approach
One way to approach this is to scale the font size proportionally to the size of the image. In my experience, more natural results are obtained when applying this not only to fontScale, but also to thickness. For example:
import math
import cv2
FONT_SCALE = 2e-3 # Adjust for larger font size in all images
THICKNESS_SCALE = 1e-3 # Adjust for larger thickness in all images
img = cv2.imread("...")
height, width, _ = img.shape
font_scale = min(width, height) * FONT_SCALE
thickness = math.ceil(min(width, height) * THICKNESS_SCALE)
Example
Let's take this free-to-use stock photo as an example. We create two versions of the base image by rescaling to a width of 2000px and 600px (keeping the aspect ratio constant). With the approach above, text looks appropriately sized to the image size in both cases (here shown in an illustrative use case where we label bounding boxes):
2000px
600px
Full code to reproduce (but note: input images have to be preprocessed):
import math
import cv2
FONT_SCALE = 2e-3 # Adjust for larger font size in all images
THICKNESS_SCALE = 1e-3 # Adjust for larger thickness in all images
TEXT_Y_OFFSET_SCALE = 1e-2 # Adjust for larger Y-offset of text and bounding box
img_width_to_bboxes = {
2000: [
{"xywh": [120, 400, 1200, 510], "label": "car"},
{"xywh": [1080, 420, 790, 340], "label": "car"},
],
600: [
{"xywh": [35, 120, 360, 155], "label": "car"},
{"xywh": [325, 130, 235, 95], "label": "car"},
],
}
def add_bbox_and_text() -> None:
for img_width, bboxes in img_width_to_bboxes.items():
# Base image from https://www.pexels.com/photo/black-suv-beside-grey-auv-crossing-the-pedestrian-line-during-daytime-125514/
# Two rescaled versions of the base image created with width of 600px and 2000px
img = cv2.imread(f"pexels-kaique-rocha-125514_{img_width}.jpg")
height, width, _ = img.shape
for bbox in bboxes:
x, y, w, h = bbox["xywh"]
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(
img,
bbox["label"],
(x, y - int(height * TEXT_Y_OFFSET_SCALE)),
fontFace=cv2.FONT_HERSHEY_TRIPLEX,
fontScale=min(width, height) * FONT_SCALE,
thickness=math.ceil(min(width, height) * THICKNESS_SCALE),
color=(0, 255, 0),
)
cv2.imwrite(f"pexels-kaique-rocha-125514_{img_width}_with_text.jpg", img)
if __name__ == "__main__":
add_bbox_and_text()
If you take fontScale = 1 for images with size approximately 1000 x 1000, then this code should scale your font correctly.
fontScale = (imageWidth * imageHeight) / (1000 * 1000) # Would work best for almost square images
If you are still having any problem, do comment.
I implemented a function to find best fitted centered location for text.
Take a look if these codes help you.
def findFontLocate(s_txt, font_face, font_thick, cv_bgd):
best_scale = 1.0
bgd_w = cv_bgd.shape[1]
bgd_h = cv_bgd.shape[0]
txt_rect_w = 0
txt_rect_h = 0
baseline = 0
for scale in np.arange(1.0, 6.0, 0.2):
(ret_w, ret_h), tmp_bsl = cv2.getTextSize(
s_txt, font_face, scale, font_thick)
tmp_w = ret_w + 2 * font_thick
tmp_h = ret_h + 2 * font_thick + tmp_bsl
if tmp_w >= bgd_w or tmp_h >= bgd_h:
break
else:
baseline = tmp_bsl
txt_rect_w = tmp_w
txt_rect_h = tmp_h
best_scale = scale
lt_x, lt_y = round(bgd_w/2-txt_rect_w/2), round(bgd_h/2-txt_rect_h/2)
rb_x, rb_y = round(bgd_w/2+txt_rect_w/2), round(bgd_h/2+txt_rect_h/2)-baseline
return (lt_x, lt_y, rb_x, rb_y), best_scale, baseline
Note that, the function accept four arguments: s_txt(string to render), font_face, font_thick and cv_bgd(background image in ndarray format)
When you putText(), write codes as following:
cv2.putText(
cv_bgd, s_txt, (lt_x, rb_y), font_face,
best_scale, (0,0,0), font_thick, cv2.LINE_AA)
You can use get_optimal_font_scale function as bellow, to adjust font size according to the image size:
def get_optimal_font_scale(text, width):
for scale in reversed(range(0, 60, 1)):
textSize = cv2.getTextSize(text, fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=scale/10, thickness=1)
new_width = textSize[0][0]
if (new_width <= width):
return scale/10
return 1
fontScale = 3*(img.shape[1]//6)
font_size = get_optimal_font_scale(text, fontScale)
cv2.putText(img, text, org, font, font_size, color, thickness, cv2.LINE_AA)
You can change fontScale for your image.
It`s work for me.
double calc_scale_rectbox(const char *txt, int box_width, int box_height,
cv::Size &textSize, int &baseline)
{
if (!txt) return 1.0;
double scale = 2.0;
double w_aprx = 0;
double h_aprx = 0;
do
{
textSize = cv::getTextSize(txt, FONT_HERSHEY_DUPLEX, scale, 2,
&baseline);
w_aprx = textSize.width * 100 / box_width;
h_aprx = textSize.height * 100 / box_height;
scale -= 0.1;
} while (w_aprx > 50 || h_aprx > 50);
return scale;
}
......
cv::Size textSize;
int baseline = 0;
double scale = calc_scale_rectbox(win_caption.c_str(), width,
height, textSize, baseline);
cv::putText(img, win_caption, Point(width / 2 - textSize.width / 2,
(height + textSize.height - baseline + 2) / 2),
FONT_HERSHEY_DUPLEX, scale, CV_RGB(255, 255, 255), 2);
A simple utility function:
def optimal_font_dims(img, font_scale = 2e-3, thickness_scale = 5e-3):
h, w, _ = img.shape
font_scale = min(w, h) * font_scale
thickness = math.ceil(min(w, h) * thickness_scale)
return font_scale, thickness
Usage:
font_scale, thickness = optimal_font_dims(image)
cv2.putText(image, "LABEL", (x, y), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255,0,0), thickness)

How to keep image alignment when cropping in Python?

I'm trying to crop and resize images in Python, and I want them to be in a fixed format
afterwards (47x62 Pixels). However, if the original image is in landscape, my algorithm doesn't work, there are blank areas.
import Image, sys
MAXSIZEX = 47
MAXSIZEY = 62
im = Image.open(sys.argv[1])
(width, height) = im.size
ratio = 1. * MAXSIZEX / MAXSIZEY
im = im.crop((0, 0, int(width*ratio), int(height*ratio)))
im = im.resize((MAXSIZEX, MAXSIZEY), Image.ANTIALIAS)
im.save(sys.argv[2])
I want the resized image to be fully 47x62 - there should be no empty area visible.
You should first check if MAXSIZEX is greater then the width or the MAXSIZEY is greater than the height. If they are first rescale the image and then do the cropping:
MAXSIZEX = 64
MAXSIZEY = 42
width, height = im.size
xrat = width / float(MAXSIZEX)
yrat = height / float(MAXSIZEY)
if xrat < 1 or yrat < 1:
rat = min(xrat, yrat)
im = im.resize((int(width / rat), int(height / rat)))
res = im.crop((0, 0, MAXSIZEX, MAXSIZEY))
res.show()
Chosing x/y as the scaling is an implicit assumption that your source's y dimension will always be smaller relative to your target resolution than your source's x dimension. First, figure out which dimension to scale on, then crop:
width_count = float(width) / MAXSIZEX
height_count = float(height) / MAXSIZEY
if width_count == height_count:
pass
elif width_count < height_count:
im = im.crop(0, 0, width, int(width_count * height / height_count))
else:
im = im.crop(0, 0, int(height_count * width / width_count), height)
Now you know you have the largest subimage from the original that matches your target aspect ratio, so you can resize without warping the image.

Categories

Resources