libvips perlin to rgb - vips

Using NetVips,
using var per = Image.Perlin(800,800,33,true,3434);
using var per2 = Image.Perlin(800,800,13,true,3435);
using var per3 = Image.Perlin(800,800,13,true,3435);
using var comp = per.Composite(new []{per2, per3},new []{Enums.BlendMode.ColourBurn, Enums.BlendMode.Lighten},compositingSpace:Enums.Interpretation.Rgb);
yeilds
Exception has occurred: CLR/NetVips.VipsException
An unhandled exception of type 'NetVips.VipsException' occurred in NetVips.dll: 'unable to call composite
vips_colourspace: no known route from 'b-w' to 'rgb''
not sure how to get image from perlin, b-w to rgb basically.

Try using sRGB instead of RGB, you should get an image.
Though it'll be black and white since you are compositing black and white images. Did you want to join the three perlin images together as RGB? You need bandjoin, eg. in Python:
#!/usr/bin/python3
import pyvips
per1 = pyvips.Image.perlin(800, 800, cell_size=33, uchar=True, seed=3434)
per2 = pyvips.Image.perlin(800, 800, cell_size=13, uchar=True, seed=3435)
per3 = pyvips.Image.perlin(800, 800, cell_size=13, uchar=True, seed=3435)
# join the three one-band images up as a three-band image, tag as sRGB
image = per1.bandjoin([per2, per3]).copy(interpretation="srgb")
image.write_to_file("x.png")
To make:

Related

vips thumbnail transparent issue for svg image

I have one transparent SVG image, I am trying to generate the thumbnail of that image. It generates a thumbnail of the image properly, but it removes the transparency of the image and adds a black background to the image. I want to generate thumbnail without losing the transparency of the image.
Note - The same problem with jpg and jpeg images also, but I added white background to the image in svg case it's not make the transparent background
I am using PHP vips library.
Here is my code for that
$im = Vips\Image::thumbnail($filename, 180, ['height' => 225, 'size' => 'both']);
$alpha = $im->hasAlpha();
if($alpha) {
$im = $im->flatten(['background' => [255, 255, 255]]);
}
Just write to a format that supports transparency, like PNG.
For example:
$im = Vips\Image::thumbnail("x.svg", 180, ['height' => 225, 'size' => 'both']);
$im->WriteToFile("x.png");
WEBP, GIF, HEIC, AVIF, TIFF etc. also support transparency. JPEG does not.

Tkinter Background not showing transparent

Hey im a newbie in python in my code there is a animated gif that play whenever i run the code. The gif image is actually transparent when open in adobe etc. but the problem is when i run the code.The frame which is the color gray included in the gif.I want to remove the color gray so that the only thing that can see is my only animated gif
This is my code:
# mimic an animated GIF displaying a series of GIFs
# an animated GIF was used to create the series of GIFs
# with a common GIF animator utility
import time
from Tkinter import *
root = Tk()
imagelist = ["dog001.gif","dog002.gif","dog003.gif"]
# extract width and height info
photo = PhotoImage(file=imagelist[0])
width = photo.width()
height = photo.height()
canvas = Canvas(width=width, height=height)
canvas.create_line(0,240,640,240, fill='blue')
canvas.pack()
# create a list of image objects
giflist = []
for imagefile in imagelist:
photo = PhotoImage(file=imagefile)
giflist.append(photo)
# loop through the gif image objects for a while
for k in range(0, 10):
for gif in giflist:
canvas.delete(ALL)
canvas.create_image(width/2.0, height/2.0, image=gif)
canvas.update()
time.sleep(0.1)
root.mainloop()[![enter image description here][1]][1]
dog001.gif
dog002.gif
dog003.gif
As described here, making a Tkinter window transparent is possible, but depends on your OS.
If you are on Windows, just add the following lines after creating the root:
root = Tk()
# Hide the root window drag bar and close button
root.overrideredirect(True)
# Make the root window always on top
root.wm_attributes("-topmost", True)
# Define a transparent color
root.wm_attributes("-transparentcolor", '#eeefff')
Then set your canvas' background color as the transparent color defined above:
canvas = Canvas(width=width, height=height, bg='#eeefff', highlightthickness=0)

Wand Rounded Edges on Images

I've been scratching my head for a few days on how to complete the task of making the edges rounded on an image taken from picamera using python-wand. I have it setup now to where it grabs the image and composites it over the banner/background image with the following:
img = Image(filename=Picture)
img.resize(1200, 800)
bimg = Image(filename=Background)
bimg.composite(img, left=300, top=200)
bimg.save(filename=BPicture)
Any help is appreciated!
You can use wand.drawing.Drawing.rectangle to generate rounded corners, and overlay it with composite channels.
from wand.image import Image
from wand.color import Color
from wand.drawing import Drawing
with Image(filename='rose:') as img:
img.resize(240, 160)
with Image(width=img.width,
height=img.height,
background=Color("white")) as mask:
with Drawing() as ctx:
ctx.fill_color = Color("black")
ctx.rectangle(left=0,
top=0,
width=mask.width,
height=mask.height,
radius=mask.width*0.1) # 10% rounding?
ctx(mask)
img.composite_channel('all_channels', mask, 'screen')
img.save(filename='/tmp/out.png')
Now if I understand your question, you can apply the same technique, but composite Picture in the drawing context.
with Image(filename='rose:') as img:
img.resize(240, 160)
with Image(img) as nimg:
nimg.negate() # For fun, let's negate the image for the background
with Drawing() as ctx:
ctx.fill_color = Color("black")
ctx.rectangle(left=0,
top=0,
width=nimg.width,
height=nimg.height,
radius=nimg.width*0.3) # 30% rounding?
ctx.composite('screen', 0, 0, nimg.width, nimg.height, img)
ctx(nimg)
nimg.save(filename='/tmp/out2.png')

Both input images must have CV_8UC1 in function error

I plan to do a stereo image from the tutorial here, but the compiler reports errors with cv2.createStereoBM, I found out it was the problem of OpenCV version.
I followed this to change cv2.createStereoBM into cv2.StereoBM. It works well, but the following code:
disparity = stereo.compute(frame0,frame1)
shows error:
Both input images must have CV_8UC1 in function cv::findStereoCorrespondenceBM
Can anyone help me with this?
The environment is Python 2.7, OpenCV 2.4.11.
My code is:
cap0 = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
while (cap0.isOpened() and cap1.isOpened()):
ret0, frame0 = cap0.read()
frame0_new=cv2.cvtColor(frame0, cv2.COLOR_BGR2GRAY)
ret1, frame1 = cap1.read()
frame1_new=cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
stereo = cv2.StereoBM(cv2.STEREO_BM_BASIC_PRESET,ndisparities=16, SADWindowSize=15)
disparity = stereo.compute(frame0,frame1)
You should use your frames converted to single channel, i.e. of type CV_8UC1:
disparity = stereo.compute(frame0_new, frame1_new)

Fish swim in pygame

I want to use the python pygame module to make a 2D "fishtank".
Basically I will load a jpg image as background and load a gif animated image depicting fish swimming and make it move.
I know how to make an static image move but how to make a gif image move while the image itself is animated.
I have PIL installed, do sure if need to use it.
If that does not work, I can also break apart the gif file into several static frames and cyclically make each of them show on screen. When the latter one is posted, the former one needs to be removed, how to remove it ?
Here is a class I used in my name to get PyGame animation to work:
class Animation(pygame.sprite.Sprite):
def __init__(self, img, fps = 6):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Slice source image to array of images
self.images = self.loadSliced(img)
# Track the time we started, and the time between updates.
# Then we can figure out when we have to switch the image.
self._start = pygame.time.get_ticks()
self._delay = 1000 / fps
self._last_update = 0
self._frame = 0
self.image = self._images[self._frame]
def loadSliced(w, h, filename):
"""
Pre-conditions:
Master can be any height
Sprites frames must be the same width
Master width must be len(frames)*frames.width
Arguments:
w -- Width of a frame in pixels
h -- Height of a frame in pixels
filename -- Master image for animation
"""
images = []
if fileExists( img, "Animation Master Image"):
master_image = pygame.image.load(filename).convert_alpha()
master_width, master_height = master_image.get_size()
for i in range(int(master_width/w)):
images.append(master_image.subsurface((i*w, 0, w, h)))
return images
def update(self, t):
# Note that this doesn't work if it's been more than self._delay
# time between calls to update(); we only update the image once
# then. but it really should be updated twice
if t - self._last_update > self._delay:
self._frame += 1
if self._frame >= len(self._images):
self._frame = 0
self.image = self._images[self._frame]
self._last_update = t
self.image = self._images[self._frame]
self._last_update = t
def getFrame(self, screen):
# Update Frame and Display Sprite
self.update(pygame.time.get_ticks())
return self.image